创建一个PHP函数以返回MySQL结果 [英] Creating a php function to return mysql results

查看:57
本文介绍了创建一个PHP函数以返回MySQL结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个函数,该函数将返回一个mysql查询,然后我可以遍历并处理结果,但是它似乎没有用.我什至可能做不到正确的方法.

Im trying to create a function, that will return a mysql query, which i can then loop through and handle the results, but it doesnt seem to be working. I might not even be doing this the right way.

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

我尝试将以上所有内容加载到函数中,但是每次只能返回一个结果.由于生病需要处理每个结果,因此我想"上述方法是我想要的方法,但是请告诉我是否有更好的方法,或者我做错了什么.

I tried loading all of the above into the function, but could only get it to return a single result each time. Since ill need to handle each result, i "think" the above way is how i want to do it, but let me know if there is a better way, or what im doing wrong.

当我用用户名回显函数时,我得到以下信息;

When i echo the function with the username, i get the following;

Resource id #5

推荐答案

删除链接变量$result周围的双引号.

Remove double quotes around the link variable $result.

function GetAccounts($username){
  require("dbconn.php");
  $result = mysql_query("SELECT * FROM `accounts` WHERE `username` = '$username' ") or trigger_error(mysql_error()); 
  return $result;
 }

这篇关于创建一个PHP函数以返回MySQL结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆