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

相关推荐 更多相似问题