Undefined variable errors with MySQL

2019-09-05 07:10发布

问题:

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?

回答1:

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 />';
}
?>  


回答2:

Move $res=$conn->query($sql); To line 12

Also remove the end ?> And place at end off file



回答3:

This is your first offender:

$sql='select * from products where `Name` in ("'.implode('","',$items).'");';

you should implode $items prior to trying to concatenating it into an sql statement.

Something like:

$item_text = implode('","',$items);
$sql='select * from products where `Name` in ("'.$item_text.'");';

Honestly, this code is a bit of a mess. You should use either PDO or mysql not both and clean this up.



回答4:

Here's a start

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

$conn = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );                

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

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

$res = $conn->query( $sql );

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 />';

?>  


标签: php mysql mysqli