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)"
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.
There aa number of issues with your code:
mysql_*
functions. They are deprecated. If you are just learning about using PHP with databases, do it the right way and use mysqli
or PDO
. Client()
method call. 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.
mysql_fetch_object()
once. 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. Perhaps something like this:
$user_data= array();
while($row = mysql_fetch_object($strQry)) {
$user_data[] = $row;
}
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. So do something like:
mysql_query($strQry, $dbConn);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.