将单个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>

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>

I do not have the ability to test this right now.

However, you have confirmed that $carid has no value (is null).

And you have confirmed the SQL statement returns correct values when run against the database. Therefore, a likely problem is with the query you are sending to the database with your code.

your query:

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

my suggestion to fix your query:

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

You could be sending the string literal "$carbrand" and not know it unless you printed out that SQL statement to check.

Have you tried that?

Also, please check the values of all your $_POST to be sure they are what you think they are. That is very important for security as well.

Get to those println statements and let me know what you find :-).

Print everything out and make sure your values are what you expect.

I'm 99% sure that's where the problem is.