php mysqli_query() 需要至少 2 个参数,其中 1 个给定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23484158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:45:22  来源:igfitidea点击:

mysqli_query() expects at least 2 parameters, 1 given in?

phphtmlmysql

提问by user3606199

I keep getting the same 3 errors every time I run this php. I have no clue what I am doing wrong, can anyone help?

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

Here are the errors:

以下是错误:

[05-May-2014 19:20:50 America/Chicago] PHP Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/sagginev/public_html/Nutrifitness/search.php on line 10

[05-May-2014 19:20:50 America/Chicago] PHP Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/sagginev/public_html/Nutrifitness/search.php on line 11

[05-May-2014 19:20:50 America/Chicago] PHP Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/sagginev/public_html/Nutrifitness/search.php on line 16

[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,在第 11 行的 /home/sagginev/public_html/Nutrifitness/search.php 中给出 null

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

here is my code

这是我的代码

enter code here

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>

回答by John Conde

The error message is quite clear. mysqli_query()requires twoparameters. You only provide one. When you see an error message like this the first thingyou need to do is go to the manual. If you did you would see you must provide your MySQLi link as the first parameter:

错误信息非常清楚。mysqli_query()需要两个参数。你只提供一个。当您看到这样的错误消息时,您需要做的第一件事就是查看手册。如果你这样做了,你会看到你必须提供你的 MySQLi 链接作为第一个参数:

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

回答by Jesse Hockenbury

You need to add the connection variable as the first argument which in this case is $con:

您需要添加连接变量作为第一个参数,在这种情况下是$con

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

回答by kimbarcelona

You need to change

你需要改变

 $search_query=mysqli_query($search_sql);

to:

到:

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

You need the connection string in the first parameter and the second is your query. Manual says:

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

mysqli_query(connection,query,resultmode);

Read more.

阅读更多。