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条)

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。