duangaoe9401 2016-10-26 19:16
浏览 69
已采纳

将单个SQL SELECT语句查询结果分配给PHP变量

Hi there I have a website which takes in a Car brand and generates a Car Id result which corresponds to a database. Although there are many solutions available for storing a sql query result to a variable but in this case it does not seem to work.

I have already tried the sql statement in the database and it works with 1 result output which is right.

<?php 
session_start();
include "dbconn.php";


$carcat = $_SESSION['selectedcarcat'];
$carbrand = $_POST['carbrand'];
$userid = $_SESSION['loginid'];
$username = $_SESSION['loginname'];
$startdate = $_POST['date1'];
$enddate = $_POST['date2'];
$pick = $_POST['pickuploc'];
$return = $_POST['returnloc'];
$calqty = 0;


    $selcaridsql = "SELECT carid FROM cars WHERE brand='$carbrand' ";

    $caridresult = $dbcnx->query($selcaridsql);

    $caridrow = mysql_fetch_object($caridresult);

    $carid = $caridrow['carid'];

    if (!$caridresult) 
    {
        $errmessage = "Your carid select query failed.";
        echo "<script type='text/javascript'>alert('$errmessage');</script>";
    }


    echo '<br>Debug 1 ';
    echo '<br>The selected qty is '
        .$qtyresult1.'<br />';
    echo '<br>The calculated qty is '
        .$calqty.'<br />';
    echo '<br>The content carid is '
        .$carid.'<br />';
    echo '<br>The content userid is '
        .$userid.'<br />';
    echo '<br>The content start is '
        .$startdate.'<br />';
    echo '<br>The content end is '
        .$enddate.'<br />';
    echo '<br>The content pick is '
        .$pick.'<br />';
    echo '<br>The content return is '
        .$return.'<br />';
        echo '<br>The content carbrand is '
        .$carbrand.'<br />';
?>

The error occurs with a blank result shown at the "Content carid is". After going through the forums it seems that the variable $carid has no value which I may have inferred wrongly.

The echo results:

Debug 1 
The selected qty is 

The calculated qty is 0

The content carid is 

The content userid is 

The content start is 2016-10-28

The content end is 2016-10-29

The content pick is jurong

The content return is bishan

The content carbrand is Honda

</div>
  • 写回答

2条回答 默认 最新

  • doubi2014 2016-10-27 07:59
    关注

    Apparently after using var_dump to debug the problem as stated by Chris, the problem was narrowed down to the NULL value that was collected by the mysql_fetch_object($caridresult);. As the DB connection utilizes @$dbcnx = new mysqli('localhost','values','values','values'); the DB class is wrong for retrieving the array values. Therefore the solution would be mysqli_fetch_array.

    These codes might be helpful for those trying the debug:

    <?php // register.php
    session_start();
    include "dbconn.php";
    
    
    $carcat = $_SESSION['selectedcarcat'];
    $carbrand = $_POST['carbrand'];
    $userid = $_SESSION['loginid'];
    $username = $_SESSION['loginname'];
    $startdate = $_POST['date1'];
    $enddate = $_POST['date2'];
    $pick = $_POST['pickuploc'];
    $return = $_POST['returnloc'];
    $calqty = 0;
    
    
        $selcaridsql = "SELECT carid FROM cars WHERE brand='$carbrand' ";
        
        echo $selcaridsql."<br>";
    
        $caridresult = $dbcnx->query($selcaridsql);
    
        echo "<br>".var_dump($caridresult);
    
        if ($caridresult->num_rows >0 )
          {
            // if they are in the database register the user id
            echo '<br>Hello more than 1 <br>';  
          }
    
        else
        {
            echo '<br>Hello less than 1 <br>';
        }
    
    
        $caridrow = mysqli_fetch_array($caridresult);
    
        echo var_dump($caridrow)."<br>"; 
    
        $carid = $caridrow['carid'];
    
        echo var_dump($carid)."<br>"; 
        
        if (!$caridresult) 
        {
            $errmessage = "Your carid select query failed.";
            echo "<script type='text/javascript'>alert('$errmessage');</script>";
        }
    
        echo '<br>Debug 1 ';
        echo '<br>The selected qty is '
            .$qtyresult1.'<br />';
        echo '<br>The calculated qty is '
            .$calqty.'<br />';
        echo '<br>The content carid is '
            .$carid.'<br />';
        echo '<br>The content userid is '
            .$userid.'<br />';
        echo '<br>The content start is '
            .$startdate.'<br />';
        echo '<br>The content end is '
            .$enddate.'<br />';
        echo '<br>The content pick is '
            .$pick.'<br />';
        echo '<br>The content return is '
            .$return.'<br />';
            echo '<br>The content carbrand is '
            .$carbrand.'<br />';
    ?>

    Special Thanks to Chris for helping to find the error!

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)