警告:mysqli_query()需要至少2个参数,给定1个.什么? [英] Warning: mysqli_query() expects at least 2 parameters, 1 given. What?
问题描述
我制作了一个PHP页面,该页面应该从数据库中选择两个名称并显示它们.
I made a PHP page that is supposed to select two names from a database and displays them.
它只是说:
警告:mysqli_query()至少需要2个参数,第4行的/home/tdoylex1/public_html/dorkhub/index.php中提供1个参数
Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/tdoylex1/public_html/dorkhub/index.php on line 4
警告:mysqli_query()至少需要2个参数,第8行的/home/tdoylex1/public_html/dorkhub/index.php中提供1个参数
Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/tdoylex1/public_html/dorkhub/index.php on line 8
我的代码是:
<?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>
推荐答案
问题是您没有保存mysqli连接.将您的连接更改为:
The issue is that you're not saving the mysqli connection. Change your connect to:
$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'];
也不要忘记像上面一样将连接变量封装为字符串.这是导致错误的原因,但是您使用的函数错误,mysqli_query返回查询对象,但是要从中获取数据,您需要使用类似mysqli_fetch_assoc
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.php to actually get the data out into a variable as I have above.
这篇关于警告:mysqli_query()需要至少2个参数,给定1个.什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!