警告:mysqli_query()需要至少2个参数,给定1个。什么?

时间:2013-10-02 23:24:59

标签: php mysql

我创建了一个PHP页面,它应该从数据库中选择两个名称并显示它们。

它只是说:

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

我的代码是:

<?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>

2 个答案:

答案 0 :(得分:24)

问题是你没有保存mysqli连接。将您的连接更改为:

$aVar = mysqli_connect('localhost','tdoylex1_dork','dorkk','tdoylex1_dork');

然后将其包含在您的查询中:

$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 http://php.net/manual/en/mysqli-result.fetch-assoc.php的东西来实际将数据输出到变量中我在上面。

答案 1 :(得分:2)

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");