-3

可能重复:
mysql_fetch_array() 期望参数 1 是资源,选择中给出的布尔值

每次输入关键字进行搜索;它显示警告“警告:mysql_num_rows() 期望参数 1 是资源,第 134 行给出的布尔值”。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&#58;&nbsp;</td><td>".$row['name']."</td><tr>";
                        $currentuser = $row['name'];
                        echo "<title>".$row['name']. "&#124; SIFE IIT Delhi</title>";
                        echo "<tr><td>E&#45;Mail ID&#58;&nbsp;</td><td>".$row['email']."</td><tr>";
                        echo "<tr><td>Contact No&#46;&#58;&nbsp;</td><td>".$row['contact']."</td><tr>";
                        echo "<tr><td>Company&#58;&nbsp;</td><td>".$row['company']."</td><tr>";
                        echo "<tr><td>Designation&#58;&nbsp;</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&#58;&nbsp;</td><td>".$row1['name']."</td><tr>";
                        echo "<tr><td>E&#45;Age&#58;&nbsp;</td><td>".$row1['age']."</td><tr>";
                        echo "<tr><td>Sex&#46;&#58;&nbsp;</td><td>".$row1['gender']."</td><tr>";
                        echo "<tr><td>Location&#58;&nbsp;</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>
4

1 回答 1

3

简答

只需不要将任何参数传递给mysql_num_rows()函数。

更长的答案

成功时mysql_query()返回资源,失败时返回 FALSE,这会弄乱您的代码。

您想要的是捕获 的结果mysql_connect()或者根本不传递参数

但是你需要的是使用函数以外的东西mysql_*

欢迎来到堆栈溢出!请不要将mysql_*函数用于新代码。它们不再被维护并且社区已经开始了弃用过程。看到红框了吗?相反,您应该了解准备好的语句并使用 PDOMySQLi。如果你不能决定,这篇文章将有助于选择。如果你想学习,这里有很好的 PDO 教程

于 2012-06-16T20:20:29.307 回答