gpt4 book ai didi

php - MySQL 查询失败

转载 作者:行者123 更新时间:2023-11-29 12:07:41 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource

(31 个回答)


6年前关闭。




我收到以下错误,因为我的 MySQL 查询失败,但我已经尝试过并且无法解决这个问题。

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in F:\xampp\htdocs\LMCS Incident Manager\New\edit.php on line 40 Error: Data not found..



PHP代码:
$con = mysql_connect("localhost", "root", "");

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("cad", $con);

$query = "SELECT id FROM cad";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))
{
$row['id'];
}

$id = $row['id'];

$result = mysql_query("SELECT * FROM `cad` WHERE `id` = $id");
$row = mysql_fetch_array($result);

if (!$result)
{
die("Error: Data not found..");
}

我认为问题在于我的第二个查询,
$result = mysql_query("SELECT * FROM `cad` WHERE `id` = $id");

失败了,它停止了。我认为虽然它无法获取我试图通过我的 MySQL 数据库检索的表行的“id”,但我不确定。

表.php代码:
<!-- Table -->

<form action="index.php" method="get" id="dispatch">



<table>
<thead>
<tr>
<th>Incident #</th>
<th>Town</th>
<th>Location</th>
<th>Incident Type</th>
<th>Time/Date</th>
<th>Admin</th>
<th>Edit Entry</th>
</tr>
</thead>
<tbody>
<?php


if( isset($_POST['town']) )
{
$town = $_POST['town'];
}

if( isset($_POST['location']) )
{
$location = $_POST['location'];
}

if( isset($_POST['incident_type']) )
{
$incident_type= $_POST['incident_type'];
}


if( isset($_POST['time_date']) )
{
$time_date= $_POST['time_date'];
}

if( isset($_POST['admin']) )
{
$admin = $_POST['admin'];
}

if( isset($_POST['id']) )
{
$id = $_POST['id'];
}



$db = mysqli_connect('localhost','root','') or die("Database error");
mysqli_select_db($db, 'cad');
$result= mysqli_query($db, "SELECT * FROM `cad` ORDER BY `time_date` DESC LIMIT 20");


while($row = mysqli_fetch_array($result))
{

$town = $row['town'];
$location = $row['location'];
$incident_type = $row['incident_type'];
$time_date = $row['time_date'];
$admin = $row['admin'];
$id = $row['id'];



/*


date_default_timezone_set('America/Chicago');

$timestamp = strtotime(date("Y-m-d H:i:s")) + 3600;

$time = date('Y-m-d H:i:s', $timestamp);





if ($time_date >= $timestamp)
echo "<tr class=\"tr-black\">";
else
echo "<tr class=\"tr-red\> */



echo "<tr>

<td class=\"id-center\">
".$id."
</td>
<td >
".$town."
</td>
<td>
".$location."
</td>
<td>
".$incident_type."
</td>

<td>
".$time_date."
</td>
<td >
".$admin."
</td>

<td>
<a id=\"edit-left\" href=\"edit.php?id=$id\" onclick=\"return confirm('Are you sure you want to edit this incident?');\" name=\"edit\" value=\"$id\" class=\"btn btn-primary btn-default center-1\"><span class=\"glyphicon glyphicon-edit\"></span></a>




<a id=\"edit-right\" href=\"delete.php?id=$id\" onclick=\"return confirm('Are you sure you want to delete this incident?');\" name=\"delete\" value=\"$id\" class=\"btn btn-primary btn-default center-1\"><span class=\"glyphicon glyphicon-trash\"></span></a>
</td>


</tr>";
}

mysqli_close($db);


?>

</tbody>
</table>
</form>

<!-- End -->

解释:这是我的 table.php 文件中的表: codebin.org/view/ca0998b1 .这就是显示信息的内容。从这里开始,当有人单击我制作的表格中的按钮时,我想编辑表格行。一旦他们这样做了,它就会把他们带到我的 edit.php 文件中: codebin.org/view/b30b7138 .基本上我在edit.php 文件中的问题是我需要从table.php 中获取特定的表行才能从我的数据库中编辑它。

最佳答案

问题是这段代码:

while($row = mysql_fetch_array($result)) 
{
$row['id'];
}
$id = $row['id'];

里面 while循环你没有对 $row['id'] 做任何事情.循环在 mysql_fetch_array() 时结束返回 false ,并在最后一次迭代中将其分配给 $row .所以在循环完成后,你正在做:
$id = false['id'];

这是没有意义的,设置 $idNULL .然后,当您尝试执行第二个查询时,它正在执行以下操作:
$result = mysql_query("SELECT * FROM `cad` WHERE `id` = ");

这是无效的 SQL,因此您会收到错误消息。

请参阅@Volkerk 的答案,了解如何正确编码所有内容。

关于php - MySQL 查询失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31196576/

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