douhui8163 2016-01-01 20:39
浏览 131
已采纳

MySQL未定义的变量错误

I have a database that has columns such MyPrice, MyStock, etc. And several rows for each product - apple, peach, etc.

I'm trying to pull data about the price/stock status of several items using the following code, but I get erros such as:

Notice: Undefined variable: sql on line 11

Warning: mysqli::query(): Empty query on line 11

Notice: Undefined variable: prices on line 45

Notice: Trying to get property of non-object on line 45

$dbhost =   'zzz';
$dbuser =   'zzz'; 
$dbpwd  =   'zzz'; 
$dbname =   'zzz';

$conn = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );                
$res = $conn->query( $sql );

$item1 = "apple";
$item2 = "peach";

$items=array( $item1, $item2 );
$sql='select * from products where `Name` in ("'.implode('","',$items).'");'; //line 11

if( $res ){

    $i=0;/* counter for dynamic variables */
    $prices=new stdClass;

    while( $rs=$res->fetch_object() ){
        $i++;

        /* Populate an object with details for the product */
        $prices->{ strtolower( $rs->name ) }=(object)array( 'price'=>$rs->MyPrice, 'description'=>$rs->MyDescription, 'stock'=>$rs->MyStock );

    }
}   
?>  

echo 'Apple:'.$prices->apple->price . '<br />'; //line 45
echo 'Peach:'.$prices->peach->stock . '<br />';

What is causing the problems?

  • 写回答

4条回答 默认 最新

  • dongzhuang1923 2016-01-01 21:03
    关注

    I've debugged your code according to the errors you've got. Anyway, the messeges should lead you to the solution by your self, especially if they are as clear and obvious. If you always get your code debugged by someone else you won't learn anything.

    <?php
    
    $dbhost =   'zzz';
    $dbuser =   'zzz'; 
    $dbpwd  =   'zzz'; 
    $dbname =   'zzz';
    
    $conn = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );                
    
    
    $item1 = "apple";
    $item2 = "peach";
    
    $items=array( $item1, $item2 );
    $sql='select * from products where `Name` in ("'.implode('","',$items).'");';
    
    // here's the first two errors.
    // Notice: Undefined variable: sql on line 11
    // Warning: mysqli::query(): Empty query on line 11
    // You tried to query the db without defining $sql first (lines were mixed up) -> 
    $res = $conn->query( $sql );
    
    if ($res) {
        $prices=new stdClass();
        $i=0; /* counter for dynamic variables */
        while( $rs=$res->fetch_object() ){
            $i++;
    
            /* Populate an object with details for the product */
            $prices->{ strtolower( $rs->name ) }=(object)array( 'price'=>$rs->MyPrice, 'description'=>$rs->MyDescription, 'stock'=>$rs->MyStock );
    
        }
        // here's 3rd and 4th error:
        // Notice: Undefined variable: prices on line 45
        // only output if you got values, so put the output into if-condition
        echo 'Apple:'.$prices->apple->price . '<br />';
        echo 'Peach:'.$prices->peach->stock . '<br />';
    } else {
        // database didn't return anything, so tell the user or something with that information.
        echo 'no data found or an error occured<br />';
    }
    ?>  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?