在查询中php存储过程错误

时间:2022-06-01 17:44:11

Here's the code:

这是代码:

<?php
    $sql = mysql_query($db, "CALL selectproducts()");
    if( $sql === FALSE ) {
        die('Query failed returning error: '. mysql_error());
    } else {
        while($row=mysql_fetch_array($sql))
        {
            $id=$row['prodname'];
            $name=$row['proddescription'];
            $desc=$row['prodsupplier'];
            $supp=$row['proddate'];
            $date=$row['prodprice'];
            $qtyleft=$row['prodquantity'];

Getting this Error:

得到此错误:

Warning: mysql_query() expects parameter 2 to be resource, string given in C:\xampp\htdocs\inventory\tableedit.php on line 166

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\inventory\tableedit.php on line 170

Why is it has errors when in fact i have no parameters in call procedure?

为什么它实际上在调用过程中没有参数时有错误?

3 个解决方案

#1


0  

Should be:

mysql_query("CALL selectproducts()", $db);

Documentation

Note that the mysql_ functions are now depreciated.

请注意,mysql_函数现在已折旧。

#2


0  

Try this method:

试试这个方法:

<?php
$link = mysqli_init(); 
mysqli_options($link, MYSQLI_INIT_COMMAND, "SET AUTOCOMMIT=0"); 
mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 5); 

mysqli_real_connect($link, $hostname, $username, $password, $dbName); 

if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

$query = "CALL simpleproc()"; 
if (mysqli_real_query($link,$query)) { 
    if ($result2 = mysqli_store_result($link)) { 
        while ($row = mysqli_fetch_assoc($result2)) { 
            $q = $row["login"]; 

            echo $q; 
        } 
    } 
} 

mysqli_close($link); 
?>

#3


0  

I do believe you're getting your mysql_query arguments getting mixed up, where was $db is the second parameter, and the first is the MySQL query to be executed.

我相信你让你的mysql_query参数混淆了,其中$ db是第二个参数,第一个是要执行的MySQL查询。

Although furthermore, you're probably better off using mysqli instead for future proofing:

此外,您可能最好使用mysqli代替以后进行打样:

<?php

    $mysqli = new mysqli('username', 'username', 'password', 'database' );
    $result = $mysqli->query("CALL selectproducts()");

    if( !$result ) {

        die('Query failed returning error: '. $mysqli->connect_errno );

    } else {

        while( $row = $result->fetch_array(MYSQLI_ASSOC)) {
            $id       =  $row['prodname'];
            $name     =  $row['proddescription'];
            $desc     =  $row['prodsupplier'];
            $supp     =  $row['proddate'];
            $date     =  $row['prodprice'];
            $qtyleft  =  $row['prodquantity'];
        }
    }
?>

From checking the mysqli PHP Docs for calling stored procedure:

从检查mysqli PHP Docs调用存储过程:

<?php 

    /**   
     *  Prepare Stored Procedure 
    **/
    if (!($result = $mysqli->prepare("CALL selectproducts()"))) {
        echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }

    if (!$result->execute()) {
        echo "Execute failed: (" . $result->errno . ") " . $result->error;
    }

    /**   
     *  Iterate through each result
    **/
    do {
        if ($res = $result->get_result()) {
            printf("---\n");
            var_dump(mysqli_fetch_all($res));
            mysqli_free_result($res);
        } else {
            if ($result->errno) {
                echo "Store failed: (" . $result->errno . ") " . $result->error;
            }
        }
    } while ($result->more_results() && $result->next_result());

?>

#1


0  

Should be:

mysql_query("CALL selectproducts()", $db);

Documentation

Note that the mysql_ functions are now depreciated.

请注意,mysql_函数现在已折旧。

#2


0  

Try this method:

试试这个方法:

<?php
$link = mysqli_init(); 
mysqli_options($link, MYSQLI_INIT_COMMAND, "SET AUTOCOMMIT=0"); 
mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 5); 

mysqli_real_connect($link, $hostname, $username, $password, $dbName); 

if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

$query = "CALL simpleproc()"; 
if (mysqli_real_query($link,$query)) { 
    if ($result2 = mysqli_store_result($link)) { 
        while ($row = mysqli_fetch_assoc($result2)) { 
            $q = $row["login"]; 

            echo $q; 
        } 
    } 
} 

mysqli_close($link); 
?>

#3


0  

I do believe you're getting your mysql_query arguments getting mixed up, where was $db is the second parameter, and the first is the MySQL query to be executed.

我相信你让你的mysql_query参数混淆了,其中$ db是第二个参数,第一个是要执行的MySQL查询。

Although furthermore, you're probably better off using mysqli instead for future proofing:

此外,您可能最好使用mysqli代替以后进行打样:

<?php

    $mysqli = new mysqli('username', 'username', 'password', 'database' );
    $result = $mysqli->query("CALL selectproducts()");

    if( !$result ) {

        die('Query failed returning error: '. $mysqli->connect_errno );

    } else {

        while( $row = $result->fetch_array(MYSQLI_ASSOC)) {
            $id       =  $row['prodname'];
            $name     =  $row['proddescription'];
            $desc     =  $row['prodsupplier'];
            $supp     =  $row['proddate'];
            $date     =  $row['prodprice'];
            $qtyleft  =  $row['prodquantity'];
        }
    }
?>

From checking the mysqli PHP Docs for calling stored procedure:

从检查mysqli PHP Docs调用存储过程:

<?php 

    /**   
     *  Prepare Stored Procedure 
    **/
    if (!($result = $mysqli->prepare("CALL selectproducts()"))) {
        echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }

    if (!$result->execute()) {
        echo "Execute failed: (" . $result->errno . ") " . $result->error;
    }

    /**   
     *  Iterate through each result
    **/
    do {
        if ($res = $result->get_result()) {
            printf("---\n");
            var_dump(mysqli_fetch_all($res));
            mysqli_free_result($res);
        } else {
            if ($result->errno) {
                echo "Store failed: (" . $result->errno . ") " . $result->error;
            }
        }
    } while ($result->more_results() && $result->next_result());

?>