gpt4 book ai didi

SQL查询后PHP var_dump NULL错误

转载 作者:行者123 更新时间:2023-11-30 22:10:21 27 4
gpt4 key购买 nike

您好,我有一个网站,当用户从网页中选择一个品牌后,该网站会查询 CarID。因此,将执行 SELECT 查询,然后将 CarID 值传递给 PHP 变量。然而,目前我用来调试问题的 var_dump 给出了一个 NULL 值,即使执行 SQL 语句没有错误。

数据库表:enter image description here

代码:

<?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 />';
?>

当前输出的结果:

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

看起来查询能够检索数据,但我不知道为什么该值会是 NULL。我已经尝试将 SQL 语句直接写入数据库并且它可以工作。

谢谢。

最佳答案

当您尝试使用 mysql_fetch_array 获取结果时,您的结果来自 mysqli_query。尝试使用 mysqli_fetch_array 获取结果。

但是,如果您使用您的数据库类(与您在 `$dbcnx->query 中使用的相同)来获取结果会更好,或者如果该类没有这样的方法,您可以添加它。

关于SQL查询后PHP var_dump NULL错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40278752/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com