2013-10-02 124 views
0

我做了一個PHP頁面,應該從數據庫中選擇兩個名稱並顯示它們。警告:mysqli_query()需要至少2個參數,給定1。什麼?

它只是說:

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> 
+3

您缺少一個參數。 http://php.net/manual/en/mysqli.query.php – Schleis

+1

做編程的第一課:如果錯誤信息顯示你錯過了某些東西,那麼很可能你錯過了那個東西。 –

+0

[mysqli \ _query可能至少需要2個參數](http://stackoverflow.com/questions/8073278/mysqli-query-expects-at-least-2-parameters) – JasonMArcher

回答

17

的問題是,你沒有保存的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這樣的數據來實際地將數據輸出到一個變量中,如上所述。

相關問題