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 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码