可能重复:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
每次我输入要搜索的关键字;它显示一个警告"警告:mysql_num_rows()期望参数1是资源,第134行和第34行给出布尔值。 php在输入时成功显示自动完成搜索,但在我提交"后会出现此错误。我的相关代码:
<?php
//allow sessions to be passed so we can see if the user is logged in
session_start();
//connect to the database so we can check, edit, or insert data to our users table
require_once("functions/connection.php");
//include out functions file giving us access to the protect() function made earlier
include "functions/functions.php";
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<link rel="shortcut icon" href="http://sifeiitd.org/favicon.ico">
<link rel="stylesheet" href="css/style.css" type="text/css" media="all">
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-30675532-1']);
_gaq.push(['_setDomainName', 'SIFE IIT Delhi']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script>
$(document).ready(function() {
$("#keywords").autocomplete({
source: keywordList,
minLength: 2,
});
});
</script>
<?php echo keywordArray(); ?>
<?php function keywordArray()
{
$rsKeywords = mysql_query("SELECT * FROM job");
$output = '<script>'."\n";
$output .= 'var keywordList = [';
while($row_rsKeywords = mysql_fetch_assoc($rsKeywords))
{
$output .= '"'.$row_rsKeywords['work'].'",';
}
$output = substr($output,0,-1); //Get rid of the trailing comma
$output .= '];'."\n";
$output .= '</script>';
return $output;
}
?>
</head>
<body>
<div id="container">
<!-- header -->
<header class="b_border">
<h1><a href="index.html"><img src="images/logo_page.png"></a></h1>
</header>
<!-- / header -->
<!-- content -->
<section class="content">
<?php
//if the login session does not exist therefore meaning the user is not logged in
if(strcmp($_SESSION['uid'],"") == 0){
//display and error message
echo "<center>You need to be logged in to user this feature!</center>";
}else{
//otherwise continue the page
//this is out update script which should be used in each page to update the users online time
$time = date('U')+50;
$update = mysql_query("UPDATE `employer` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");
$display_query = mysql_query("SELECT * FROM employer WHERE `id` = '".$_SESSION['uid']."'");
echo '<div class="col1 lfloat">';
echo "<table id='pageTable'><tbody><th>Your Details</th>";
echo "<tbody>";
while($row = mysql_fetch_array($display_query)){
echo "<tr><td>Name: </td><td>".$row['name']."</td><tr>";
$currentuser = $row['name'];
echo "<title>".$row['name']. "| SIFE IIT Delhi</title>";
echo "<tr><td>E-Mail ID: </td><td>".$row['email']."</td><tr>";
echo "<tr><td>Contact No.: </td><td>".$row['contact']."</td><tr>";
echo "<tr><td>Company: </td><td>".$row['company']."</td><tr>";
echo "<tr><td>Designation: </td><td>".$row['designation']."</td><tr>";
}
echo "</tbody>";
echo "</table>";
echo "<table><tr><td>";
echo '<div class="button"><a href="functions/logout.php">Logout</a></td></tr></table>';
echo '</div>';
echo '<div class="col1 rfloat">';
echo "Dear ".$currentuser." please input the type of job for a potential candidate";
echo '<form action="loggedin_employer.php" method="post" id="loginForm">';
echo '<table cellpadding="2" cellspacing="0" border="0"><tr><td>';
echo '<input class="input" id="keywords" name="keywords" type="text" ></td></tr>';
echo '<tr><td colspan="2" align="right"><input class="button" type="submit" name="submit" value="Search" /></td>
</tr></table>';
//make sure you close the check if they are online
if(!isset($_POST['submit'])){
echo "Your search was invalid";
exit;
}
$keyword = mysql_real_escape_string($_REQUEST['keywords']);
$sql = "SELECT * FROM job WHERE work=$keyword LIMIT 10";
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
echo "<table id='pageTable'><tbody><th>Your Details</th>";
echo "<tbody>";
if($numrows == 0){
echo "Sorry, your search did not return any results";
}
else{
$i = 0;
while($i < $numrows){
$row1 = mysql_fetch_array($result);
echo "<tr><td>Name: </td><td>".$row1['name']."</td><tr>";
echo "<tr><td>E-Age: </td><td>".$row1['age']."</td><tr>";
echo "<tr><td>Sex.: </td><td>".$row1['gender']."</td><tr>";
echo "<tr><td>Location: </td><td>".$row1['location']."</td><tr>";
$i++;
}
}
echo "</tbody>";
echo "</table>";
echo '</div>';
echo '</form>';
}
?>
</section>
<!-- / content -->
<!-- footer -->
<div class="footer" >
</div>
<!-- / footer -->
</div>
</body>
</html>
答案 0 :(得分:3)
只是不要将任何参数传递给mysql_num_rows()
函数。
虽然mysql_query()
在成功时返回资源,但在失败时返回FALSE,这会弄乱你的代码。
你想要的是捕获mysql_connect()
,的结果,或者根本不传递参数。
但需要的是使用mysql_*
以外的功能:
欢迎使用Stack Overflow!请不要使用
mysql_*
函数 新代码。它们已不再维护,社区已经开始 deprecation process。请参阅red box?相反,您应该了解prepared statements并使用其中之一 PDO或MySQLi。如果你 无法决定,this article将有助于选择。 如果您想学习,here is good PDO tutorial。