gpt4 book ai didi

php - mysql_fetch_array() & mysql_free_result() 错误

转载 作者:行者123 更新时间:2023-11-29 13:26:01 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 个回答)


8年前关闭。




我正在设计一个允许用户注册和登录的网站 - 登录后,用户会看到其他链接(仅对注册用户可见)。其中一个链接包括“我的书签”-“我的书签”(index.php)代码如下:

我的书签 (index.php) 代码:

<?php
session_start();
//check session first
if (!isset($_SESSION['email'])){
echo "You are not logged in!";
exit();
}
else{
//include the header
include ("../includes/header.php");
require_once ('../../mysql_connect.php');
echo ("<center>");
echo ("<h3>Bookmark</h3><p>");
echo ("<a href=add.php>Add a record</a><p>");
echo ("<a href=searchform.php>Search records</a><p>");
//Set the number of records to display per page
$display = 5;
//Check if the number of required pages has been determined
if(isset($_GET['p'])&&is_numeric($_GET['p'])){//Already been determined
$pages = $_GET['p'];
}
else{//Need to determine
//Count the number of records;
$query = "SELECT COUNT(id) FROM bookmark";
$result = @mysql_query($query);
$row = @mysql_fetch_array($result, MYSQL_NUM);
$records = $row[0]; //get the number of records
//Calculate the number of pages ...
if($records > $display){//More than 1 page is needed
$pages = ceil($records/$display);
}else{
$pages = 1;
}
}// End of p IF.

//Determine where in the database to start returning results ...
if(isset($_GET['s'])&&is_numeric($_GET['s'])){
$start = $_GET['s'];
}else{
$start = 0;
}

//Make the paginated query;
$query = "SELECT * FROM bookmark LIMIT $start, $display";
$result = @mysql_query ($query);

//Table header:
echo "<table cellpadding=5 cellspacing=5 border=1><tr>
<th>Title</th><th>Comment</th><th>URL</th><th>*</th><th>*</th></tr>";

//Fetch and print all the records...
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr><td>".$row['title']."</td>";
echo "<td>".$row['comment']."</td>";
echo "<td><a href=".$row['url']." target=_blank>".$row['url']."</a></td>";
echo "<td><a href=deleteconfirm.php?id=".$row['id'].">Delete</a></td>";
echo "<td><a href=updateform.php?id=".$row['id'].">Update</a></td></tr>";
} // End of While statement
echo "</table>";
mysql_free_result ($result); // Free up the resources.
mysql_close(); // Close the database connection.

//Make the links to other pages if necessary.
if($pages>1){
echo '<br/><table><tr>';
//Determine what page the script is on:
$current_page = ($start/$display) + 1;
//If it is not the first page, make a Previous button:
if($current_page != 1){
echo '<td><a href="index.php?s='. ($start - $display) . '&p=' . $pages. '"> Previous </a></td>';
}
//Make all the numbered pages:
for($i = 1; $i <= $pages; $i++){
if($i != $current_page){ // if not the current pages, generates links to that page
echo '<td><a href="index.php?s='. (($display*($i-1))). '&p=' . $pages .'"> ' . $i . ' </a></td>';
}else{ // if current page, print the page number
echo '<td>'. $i. '</td>';
}
} //End of FOR loop
//If it is not the last page, make a Next button:
if($current_page != $pages){
echo '<td><a href="index.php?s=' .($start + $display). '&p='. $pages. '"> Next </a></td>';
}

echo '</tr></table>'; //Close the table.
}//End of pages links
//include the footer
include ("../includes/footer.php");
}
?>

对于格式问题,我还没有完全弄清楚如何有效地复制和粘贴代码。

无论如何,我的问题源于我收到的两个错误,它们是:

错误一:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/kethcart/public_html/440/finalproject/htdocs/bookmark/index.php on line 50



错误二:

Warning: mysql_free_result() expects parameter 1 to be resource, boolean given in /home/kethcart/public_html/440/finalproject/htdocs/bookmark/index.php on line 58



我知道关于这个主题有足够多的已发布问题,但我似乎无法将它们应用于我的情况。如果有人可以提供有关如何解决此问题的建议,甚至可以提供指向另一个问题或解决方案的链接,那就太好了!

我感谢每个人的帮助和耐心——如果没有这个社区,我不知道该怎么办。

谢谢,

罗克曼杜

最佳答案

快速回答。您收到错误是因为您的查询有问题。使用 mysql_query 时,重要的是要注意,如果查询失败,你会得到一个 BOOLEAN FALSE的返回.这就是为什么你被告知... expects parameter 1 to be resource, boolean given...
为避免此类问题,请尝试使用 die声明以获得可能错误的“反馈”。就像是:

$query = "SELECT COUNT(id) FROM bookmark";
$result = mysql_query($query) or die ("Error in query: $query. <br />".mysql_error());

也可以看看:
  • info about your @ symbol
  • http://us3.php.net/
  • http://us3.php.net/die || http://us3.php.net/manual/en/function.exit.php
  • http://us3.php.net/mysql_query
  • 关于php - mysql_fetch_array() & mysql_free_result() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20130221/

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