我試圖創建一個函數,它將返回一個mysql查詢,然後我可以循環並處理結果,但它似乎沒有工作。我甚至可能不會以正確的方式做這件事。創建一個php函數返回mysql結果
function GetAccounts($username){
require("dbconn.php");
$result = mysql_query("SELECT * FROM `accounts` WHERE `username` = '$username' ") or trigger_error(mysql_error());
return "$result";
}
$result = GetAccounts($username);
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
$theusername = $row['theusername'];
$thepassword = $row['thepassword'];
echo $theusername;
}
我收到的錯誤是
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
我嘗試了所有的上述裝入功能,但只能得到它每次返回一個結果。由於生病需要處理每個結果,我「認爲」上述方式是我想要做到這一點,但讓我知道是否有更好的方法,或者我做錯了什麼。
當我用用戶名回顯功能,我得到以下;
Resource id #5
它工作得很好,謝謝。到底發生了什麼?爲什麼引用結果變量搞砸了? – mrpatg 2009-08-18 06:58:40
請參閱下面的Tom的回答。因爲使用雙引號表示$ result被轉換爲字符串。 – Pete 2009-08-18 07:01:50