我试着去创建一个函数,将返回一个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;
}
Run Code Online (Sandbox Code Playgroud)
我收到的错误是
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
Run Code Online (Sandbox Code Playgroud)
我尝试将上述所有内容加载到函数中,但每次都只能返回一个结果.由于生病需要处理每个结果,我"想"上面的方式是我想要做的,但让我知道是否有更好的方法,或我做错了什么.
当我使用用户名回显函数时,我得到以下内容;
Resource id #5
Run Code Online (Sandbox Code Playgroud)
ada*_*ost 11
删除链接变量周围的双引号$result.
function GetAccounts($username){
require("dbconn.php");
$result = mysql_query("SELECT * FROM `accounts` WHERE `username` = '$username' ") or trigger_error(mysql_error());
return $result;
}
Run Code Online (Sandbox Code Playgroud)
Tom*_*igh 11
把$result双引号意味着它将被转换为字符串,然后不再型"资源"的.尝试改为:
return $result;
Run Code Online (Sandbox Code Playgroud)