Specs Specs - 1 month ago 19
MySQL Question

PHP var_dump NULL error after SQL Query

Hi there I have a website which query for the CarID after a user selects a BRAND from a webpage. Therefore a

SELECT
query would be executed and the CarID value would then be passed to a PHP variable. However currently the var_dump that I am using to debug the problem is giving a
NULL
value even though there is no error executing the SQL statement.

The database table: enter image description here

The codes:



<?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 )
{
echo '<br>Hello more than 1 <br>';
}

else
{
echo '<br>Hello less than 1 <br>';
}


$caridrow = mysql_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 />';
?>





The results that would be currently output:



SELECT carid FROM cars WHERE brand='Honda'
object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) }

Hello more than 1
NULL
NULL

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





It seems that the query is able to retrieve the data but I have no idea why the value would be a NULL. I have tried the SQL statement directly into the database and it works.

Thanks.

Answer

Your result is from mysqli_query while you are trying to fetch it using mysql_fetch_array. Try using mysqli_fetch_array for fetching the result.

But it will be a better if you use your DB class (the same that you are using for `$dbcnx->query) for fetching the result, or if this class doesn't have such method you can add it.

Comments