简体   繁体   English

如何使用php函数从数据库中提取数据

[英]How do I pull data from the my database using a php function

This is what I have in my main page: 这是我主页上的内容:

$foo = new Foo();
$strQry=$foo->Client();
$user_data=mysql_fetch_object($user_res);
echo $user_data->clientid;

This is what I have in my class file: 这是我的班级文件中的内容:

class Foo
{
function Client() 
{
$strQry = "SELECT clientid FROM users";
$user_res = mysql_query($strQry) or die("Unable to select, Error: ".mysql_error());
return $user_res;
}
}

I am not very good at using classes and functions with PHP and so I'm getting this error "Unable to select, Error: Access denied for user 'nobody'@'localhost' (using password: NO)" 我不太擅长在PHP中使用类和函数,因此出现此错误“无法选择,错误:用户'nobody'@'localhost'的访问被拒绝(使用密码:NO)“

Any help would be greatly appreciated! 任何帮助将不胜感激!

It looks like you are not even making a connection to the database. 看来您甚至都没有与数据库建立连接。

Here's a nice step-by-step walk through, that should show you how to connect to a mysql database using either mysqli or pdo, which are both more safe ways to talk to mysql. 这是一个很好的分步介绍,应该向您展示如何使用mysqli或pdo连接到mysql数据库,这两种方式都是与mysql进行通信的更安全的方法。

http://codular.com/php-mysqli http://codular.com/php-mysqli

There aa number of issues with your code: 您的代码有很多问题:

  • You should not be using mysql_* functions. 您不应该使用mysql_*函数。 They are deprecated. 他们已弃用。 If you are just learning about using PHP with databases, do it the right way and use mysqli or PDO . 如果您只是学习将PHP与数据库一起使用,请以正确的方式进行操作,并使用mysqliPDO
  • You need to make sure you are actually connecting to the database. 您需要确保您实际上正在连接到数据库。 Maybe you are and it is not shown here, but this is obviously critical. 也许您在这里,但未在此处显示,但这显然很关键。
  • You are intending to return a database result from your Client() method call. 您打算从Client()方法调用中返回数据库结果。 But you are not using the variable your returned the results into. 但是您没有使用返回结果的变量。

This code: 这段代码:

$strQry=$foo->Client();
$user_data=mysql_fetch_object($user_res);

Should be: 应该:

$strQry=$foo->Client();
$user_data=mysql_fetch_object($strQry);

because $strQry gets the result set resource. 因为$strQry获取结果集资源。

  • You are only calling mysql_fetch_object() once. 您只调用一次mysql_fetch_object() You could have any number of rows in the result set based on your query. 根据查询,结果集中可以有任意数量的行。 If you want all of them, you would need to loop through the result set and call mysql_fetch_object() on each row. 如果需要所有这些,则需要遍历结果集并在每一行上调用mysql_fetch_object()

Perhaps something like this: 也许是这样的:

$user_data= array();
while($row = mysql_fetch_object($strQry)) {
    $user_data[] = $row;
}
  • If you INSIST on using deprecated mysql_* functions, you might at least want to get in the habit of passing the DB connection as a parameter in your functions, to make it clear which DB connection you are working with. 如果您要使用不推荐使用的mysql_*函数进行mysql_* ,则可能至少要养成将DB连接作为函数中的参数传递的习惯,以明确使用的是哪个DB连接。

So do something like: 因此,请执行以下操作:

mysql_query($strQry, $dbConn);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从PHP提取返回数据以提供给我的AJAX POST调用函数? - How do I pull in the return data from PHP to give to my AJAX POST call function? 如何使用 PHP 7 将用户输入数据作为“日期”类型提取到我的 SQL 数据库中? - How can I pull user input data as type "date" into my SQL database using PHP 7? 如何从MySQL数据库中提取数据以在页面加载时预先填充表单? - How do I pull data from my MySQL database to pre populate a form on page load? 如何从SQL数据库[PHP]中提取某些值? - How do I pull the value of something from an SQL database [PHP]? 如何从我的数据库中提取变量? - How do I pull variables from my Database? 如何从已登录用户的数据库中提取数据? - How do I pull data from a database from a logged in user? 使用 jQuery 和 php 从数据库中提取数据 - Using jQuery and php to pull data from database 如何使用PHP中的setInterval函数从数据库获取数据 - How do I get data back from database using setInterval function in php 如何使用php将用户信息从facebook sdk提取到我网站的数据库中 - how to pull user information from facebook sdk into my site's database using php php / sql-如何在变量上使用LIKE运算符从数据库中提取结果? - php/sql- How do I use LIKE operator on variable to pull results from database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM