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

报告相同问题?

悬赏问题

  • ¥15 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序