php - 创建一个 php 函数来返回 mysql 结果

标签 php mysql

我正在尝试创建一个函数,它将返回一个 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/

相关文章:

php - Hadoop起点

php - iPage 将 PHP 连接到我的数据库

MySQL - 没有子查询的多行求和

PHP MySQL selectpicker 中的无限类别树

php - mysql_fetch_array 仅打印第一个结果

php - 向 UNION ALL 查询添加额外字段

mysql - 为什么mysql不支持函数式索引?

php - 如何读取 .txt 文件并将其导入数据库?

javascript - 使用 php 设置图像 uploader 字段

php - redirect_status 的奇数 (44)