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?