php 警告:mysqli_query() 需要至少 2 个参数,1 个给定。什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19148407/
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
Warning: mysqli_query() expects at least 2 parameters, 1 given. What?
提问by user2840637
I made a PHP page that is supposed to select two names from a database and displays them.
我制作了一个 PHP 页面,该页面应该从数据库中选择两个名称并显示它们。
It just says:
它只是说:
Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/tdoylex1/public_html/dorkhub/index.php on line 4
Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/tdoylex1/public_html/dorkhub/index.php on line 8
警告:mysqli_query() 需要至少 2 个参数,1 个在第 4 行的 /home/tdoylex1/public_html/dorkhub/index.php 中给出
警告:mysqli_query() 需要至少 2 个参数,1 个在第 8 行的 /home/tdoylex1/public_html/dorkhub/index.php 中给出
My code is:
我的代码是:
<?php mysqli_connect(localhost,tdoylex1_dork,dorkk,tdoylex1_dork);
$name1 = mysqli_query("SELECT name1 FROM users
ORDER BY RAND()
LIMIT 1");
$name2 = mysqli_query("SELECT name FROM users
ORDER BY RAND()
LIMIT 1");
?>
<title>DorkHub. The online name-rating website.</title>
<link rel="stylesheet" type="text/css" href="style.css">
<body bgcolor='EAEAEA'>
<center>
<div id='TITLE'>
<h2>DorkHub. The online name-rating website.</h2>
</div>
<p>
<br>
<h3><?php echo $name1; ?></h3><h4> against </h4><h3><?php echo $name1; ?></h3>
<br><br>
<h2 style='font-family:Arial, Helvetica, sans-serif;'>Who's sounds the dorkiest?</h2>
<br><br>
<div id='vote'>
<h3 id='done' style='margin-right: 10px'>VOTE FOR FIRST</h3><h3 id='done'>VOTE FOR LAST</h3>
回答by Chibette
The issue is that you're not saving the mysqli connection. Change your connect to:
问题是您没有保存 mysqli 连接。将您的连接更改为:
$aVar = mysqli_connect('localhost','tdoylex1_dork','dorkk','tdoylex1_dork');
And then include it in your query:
然后将其包含在您的查询中:
$query1 = mysqli_query($aVar, "SELECT name1 FROM users
ORDER BY RAND()
LIMIT 1");
$aName1 = mysqli_fetch_assoc($query1);
$name1 = $aName1['name1'];
Also don't forget to enclose your connections variables as strings as I have above. This is what's causing the error but you're using the function wrong, mysqli_query returns a query object but to get the data out of this you need to use something like mysqli_fetch_assoc http://php.net/manual/en/mysqli-result.fetch-assoc.phpto actually get the data out into a variable as I have above.
也不要忘记将您的连接变量作为字符串括起来,就像我上面所说的那样。这就是导致错误的原因,但您使用的函数错误,mysqli_query 返回一个查询对象,但要从中获取数据,您需要使用类似 mysqli_fetch_assoc http://php.net/manual/en/mysqli-result 的东西.fetch-assoc.php将数据实际放入一个变量中,就像我上面所说的那样。
回答by Shanaka WickramaArachchi
the mysqli_query
excepts 2 parameters , first variable is mysqli_connect
equivalent variable , second one is the query you have provided
的mysqli_query
节选2个参数,第一变量是mysqli_connect
等效可变的,第二个是你所提供的查询
$name1 = mysqli_connect(localhost,tdoylex1_dork,dorkk,tdoylex1_dork);
$name2 = mysqli_query($name1,"SELECT name FROM users ORDER BY RAND() LIMIT 1");