2013-07-08 39 views
-2

這是舊版本的MySQL代碼,工程確定:轉換一個MySQL功能的mysqli

 function getResult($sql) { 
     $result = mysql_query($sql, $this->conn); 
     if ($result) { 
      return $result; 
     } else { 
      die("SQL Retrieve Error: " . mysql_error()); 
     } 
    } 

不過,我試圖改變它的mysqli。這是我到目前爲止的地方。

function getResult($connection){; 
     $result = mysqli_query($connection, $this->conn); //L92 
     if ($result) { 
      return $result; 
     } else { 
     die("SQL Retrieve Error: " . mysqli_error($connection)); //L96 
     } 
    } 

可怕的消息正在發生:

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\sotdhit\dogsfunc.php on line 92 

Warning: mysqli_error() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\sotdhit\dogsfunc.php on line 96 
SQL Retrieve Error: 

爲什麼facepalming我。它也可能是一個在網頁上的電話搞砸了嗎?這裏是。

$db1 = new dbmember(); //name of the class 
$db1->openDB(); 
$sql="SELECT * from member"; 
$result=$db1->getResult($sql); 
echo "<table border='1'>"; 
echo "<tr><th>Member ID</th><th>Name</th><th>Address</th><th>Postcode</th><th>Photo</th></tr>"; 
while($row = mysqli_fetch_assoc($result)) 
{ 
echo "<tr>"; 
echo "<td>{$row['mid']}</td><td>{$row['name']}</td>"; 
echo "<td>{$row['address']}</td><td>{$row['postcode']}</td>"; 
echo"<td><img height='80' width='120' src='{$row['photo'] }' /></td>"; 
echo "</tr>"; 
} 
echo "</table>"; 
+1

請仔細閱讀警告,然後查看代碼的哪一部分,以確定實際連接。 – Sirko

+0

我第二次Sirko。 –

+1

你正在將一個SQL字符串傳遞給你的函數,作爲一個名爲'$ connection'的參數;但實際的連接似乎在$ this-> conn中。在mysqli_query()周圍交換兩個參數(並考慮將'$ connection'重命名爲更少混淆的東西) – andrewsi

回答

1

其他各地的道:

$result = mysqli_query($this->conn, $connection); 
         ^^^^^^^^^^^^ 

預計mysqli的連接句柄是第一個參數,與MySQL不同,它用它作爲一個可選的最後一個參數。

+0

好的,明白了。但是,由於某些原因,參數2將作爲NULL中斷操作。你認爲沒有任何東西從網站傳遞給函數,因爲我改變了一些變量名。我不確定。 – user2557798

+0

如果$ connection在函數中爲null,那麼你要麼傳遞一個null,要麼傳遞一個未定義的變量,或者什麼都不傳遞。 –

+0

我明白了,但我真的不知道還有什麼問題。我意識到數據沒有通過,在某個地方。但我看過的例子似乎並不涉及我在這裏想要達到的目標。有什麼明顯的看起來可能會導致呃逆?我也可以加上謝意。 – user2557798