我正在尝试创建一个函数,它将返回一个 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
最佳答案
删除链接变量 $result
周围的双引号。
function GetAccounts($username){
require("dbconn.php");
$result = mysql_query("SELECT * FROM `accounts` WHERE `username` = '$username' ") or trigger_error(mysql_error());
return $result;
}
关于php - 创建一个 php 函数来返回 mysql 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1292210/