database access problem - mysql_connect()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nate
Forum Newbie
Posts: 14
Joined: Tue Feb 12, 2008 6:45 pm

database access problem - mysql_connect()

Post by nate »

I have this PHP I've been working on to access my database and I can't seem to get past this error:

Warning: mysql_connect(): Access denied for user 'quotes@82.'@'82.197.130.21' (using password: YES) in /home/www/quotes.mypressonline.com/aaaTest.php on line 28
Access denied for user 'quotes@82.'@'82.197.130.21' (using password: YES)

Here is my code, and notice the difference from above: user 'quotes@82.'@'82.197.130.21' as opposed to below:
'quotes@82.197.130.21'

<?php

// mysql config
$db = mysql_connect("fdb1.runhosting.com:3306", "quotes@82.197.130.21", "DBpw");
if (!$db) { die( mysql_error() ); }

// open "quotes", as current db
$db_selected = mysql_select_db("quotes", $db);
if (!$db_selected) { die ( mysql_error() ); }

$result = mysql_query("SELECT * FROM quotes WHERE AuthorFirst=First and AuthorLast=Last", $db_selected) or die( mysql_error() );

$num_rows = mysql_num_rows($result) or die( mysql_error() );

$i=0;
while ($i < $num_rows) {

$quote = mysql_result($result,$i,"Quotes");
$first = mysql_result($result,$i,"AuthorFirst");
$last = mysql_result($result,$i,"AuthorLast");

echo "$quote<br>-$first $last<br><br>";

$i++;
}

// close database
mysql_close($db) or die( mysql_error() );

?>

Your help is much appreciated, thank you,
Nate
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: database access problem - mysql_connect()

Post by EverLearning »

Did you try connecting with:

Code: Select all

 
$db = mysql_connect("fdb1.runhosting.com:3306", "quotes", "DBpw");
 
?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: database access problem - mysql_connect()

Post by anjanesh »

Goto mysql console and see what grants you have for quotes.

Code: Select all

SHOW GRANTS FOR 'quotes'@'fdb1.runhosting.com';
SHOW GRANTS FOR 'quotes'@'82.197.130.21';
nate
Forum Newbie
Posts: 14
Joined: Tue Feb 12, 2008 6:45 pm

Re: database access problem - mysql_connect()

Post by nate »

Well, I've tried $db = mysql_connect("fdb1.runhosting.com:3306", "quotes", "DBpw"); and now every combination I can think of for the host and username, no luck there...

I also tried
SHOW GRANTS FOR 'quotes'@'fdb1.runhosting.com';
Result: #1044 - Access denied for user '87604_quotes'@'%' to database 'mysql'

SHOW GRANTS FOR 'quotes'@'82.197.130.21';
Result: #1044 - Access denied for user '87604_quotes'@'%' to database 'mysql'

as well as a few others

SHOW GRANTS FOR '87604_quotes'@'82.197.130.21';
Result: #1141 - There is no such grant defined for user '87604_quotes' on host '82.197.130.21'

SHOW GRANTS FOR '87604_quotes'@'fdb1.runhosting.com';
Result: #1141 - There is no such grant defined for user '87604_quotes' on host 'fdb1.runhosting.com'

GRANT SELECT ON `quotes` . * TO 'root'@'%';
Result: #1044 - Access denied for user '87604_quotes'@'%' to database 'quotes'

Good ideas anyway, will resetting the GRANT permission fix this with that result message? I'm off to try that now, if it works I'll post what I did here, but in the meantime if you have any more suggestions I'd like to hear them.

Thanks, Nate
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: database access problem - mysql_connect()

Post by anjanesh »

You'll need to login as mysql admin to access the mysql database and use the GRANT statement to grant privileges to a user.
Or if you're using a control panel like CPanel, Plesk etc, you can do it there.

Code: Select all

GRANT ALL ON databaseName.* TO 'username'@'localhost';
GRANT ALL ON databaseName.* TO 'username'@'%';
nate
Forum Newbie
Posts: 14
Joined: Tue Feb 12, 2008 6:45 pm

Re: database access problem - mysql_connect()

Post by nate »

I tried messing with the GRANT settings on the mysql server and I could not get any access or change any permission settings as far as I can tell I have SELECT and PROCESS and that's it. hmmmm. Do you see anything wrong with the code? Thanks a bunch, Nate
soumitrar
Forum Newbie
Posts: 4
Joined: Thu Feb 22, 2007 11:09 am

Re: database access problem - mysql_connect()

Post by soumitrar »

<html>
<body>
<?php
$con = mysql_connect("localhost:3306","root","root");
if (!$con)
{
echo "BAG";
echo ("Could not connect: ") . mysql_error();
}

mysql_select_db("<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>", $con);

$result = mysql_query("SELECT * FROM pet");

while($row = mysql_fetch_array($result))
{
echo $row['name'] . " " . $row['owner']. " " . $row['species']. " " . $row['sex'];
echo "<br />";
}

mysql_close($con);
?>
</body>
</html>
----------------------------------

when i am browsing this page as http://localhost/db_connect.php, blank page is showing with no errors.

Can anyone solve the issue?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: database access problem - mysql_connect()

Post by anjanesh »

Code: Select all

<html>
<body>
<?php
$con = mysql_connect("localhost:3306", "root", "root");
if (!$con)
{
       echo "BAG";
       die("Could not connect: ") . mysql_error();
}
 
mysql_select_db("smurf", $con) or die("Couldnt select database");
 
$result = mysql_query("SELECT * FROM pet");
 
if (mysql_num_rows($result) == 0)
 echo "No rows to display";
else
 while($row = mysql_fetch_array($result))
  {
         echo $row['name'] . " " . $row['owner']. " " . $row['species']. " " . $row['sex'];
         echo "<br />";
  }
 
mysql_close($con);
?>
</body>
</html>
Now what does it show ?
Post Reply