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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵