mysqli_query()期望至少有2个参数,1给出?

时间:2014-05-06 00:38:48

标签: php html mysql

每次运行此php时,我都会遇到相同的3个错误。我不知道我做错了什么,有人可以帮忙吗?

以下是错误:

  

[05-May-2014 19:20:50 America / Chicago] PHP警告:mysqli_query()   期望至少有2个参数,1个给出   第10行/home/sagginev/public_html/Nutrifitness/search.php

     

[05-May-2014 19:20:50 America / Chicago] PHP警告:mysqli_num_rows()   期望参数1为mysqli_result,null中给出null   第11行/home/sagginev/public_html/Nutrifitness/search.php

     

[05-May-2014 19:20:50 America / Chicago] PHP警告:mysqli_num_rows()   期望参数1为mysqli_result,null中给出null   第16行/home/sagginev/public_html/Nutrifitness/search.php

这是我的代码

enter code here

    <?php
    $con=mysqli_connect('localhost','sagginev_rob','122989','sagginev_Nutrifitness');
    if (mysqli_connect_errno()) // Check connection
      {   echo "Failed to connect to MySQL: " . mysqli_connect_error();  }

        if(!isset($_POST['search'])) {
    header("Location:home.php");
    }
    $search_sql="Select * FROM Questions WHERE username LIKE '%".$_POST['search']."%' OR feedback LIKE '%".$_POST['search']."%'";
    $search_query=mysqli_query($search_sql);
    if(mysqli_num_rows($search_query)!=0) {
    $search_rs=mysqli_fetch_assoc($search_query);
    }
    ?>
    <H2> Search Results</H2>
    <?php if(mysqli_num_rows($search_query)!=0) {
     do { ?>
     <p><?php echo $search_rs['name']; ?> </p>
    <?php } while ($search_rs=mysqli_fetch_assoc($search_query));
    } else {
       echo "No results found";
    } ?>
    <form>
    <br>
    <input type="button" value="Go Back Home" onClick="parent.location='http://sagginevo.com/Nutrifitness/home.php'">
    </form>

3 个答案:

答案 0 :(得分:5)

错误信息非常清楚。 mysqli_query()需要两个参数。你只提供一个。当您看到类似这样的错误消息时,您需要做的是 go to the manual {{3}} 。如果你这样做,你会发现你必须提供你的MySQLi链接作为第一个参数:

$search_query=mysqli_query($con, $search_sql);

答案 1 :(得分:1)

您需要添加连接变量作为第一个参数,在本例中为$con

$search_query=mysqli_query($con, $search_sql);

答案 2 :(得分:0)

您需要更改

 $search_query=mysqli_query($search_sql);

为:

 $search_query=mysqli_query($con, $search_sql);

您需要第一个参数中的连接字符串,第二个是您的查询。手册说:

mysqli_query(connection,query,resultmode);

阅读more.