Jump to content


Photo

connect/select a database


  • Please log in to reply
8 replies to this topic

#1 gBase

gBase
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 29 September 2006 - 01:22 AM

My PHP code to connect and select a database is working great...problem is it connects successfully but doesn't sync up with the databases I type into the code and am sure are on my server.  Is there a query I can type in PHP that says (after connecting successfully) 'print a list of all databases found in host' (localhost?)  Thanks for any help...new to some of this stuff.

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 29 September 2006 - 02:55 AM

I don't know what you mean by "sync up"... and SHOW DATABASES.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 gBase

gBase
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 29 September 2006 - 12:54 PM

In other words, I type in one of the databases on my server, and I get 'Could not select the databases, are you sure it exists?'

Here's my code:

<?php

    // connect and select a database 
    $link = mysql_connect("localhost", "root", "mypassword")
          or die 
    print "Connected successfully.<br>";
    $db = "my_database";
    mysql_select_db($db) or die("Could not select the database '" . $db . "'.  Are you sure it exists?");
?>

So, I know I connected to my server successfully because it says "Connected Successfully."

I actually already tried adding to this code:

$sql = 'SHOW DATABASES';

But when I try the page again I still get the same message and it doesn't show any databases.  I created 2 databases on my server and I can see them in phpmyadmin.  Is there a problem with my server possibly?



#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 29 September 2006 - 02:38 PM

Add the mysql_error function to the end of the your die clause that way you get an error from mysql which will tell you whats wrong. So use this:
// connect and select a database 
$link = mysql_connect("localhost", "root", "mypassword") or die ("unable to connect MySQL at this time:<br />" . mysql_error());

print "Connected successfully.<br>";

mysql_select_db("my_database") or die("Could not select the database '" . $db . "':<br />\n" . mysql_error());


#5 gBase

gBase
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 29 September 2006 - 03:11 PM

Thanks for your help with my code however I am still getting this:

Connected successfully.
Could not select the database '':
Access denied for user 'root'@'localhost' to database 'my_database'

If you guys think there may be an issue with my server (which I am starting to suspect) I can probably get this straightened out with tech support.  I just wanted to know if there was more PHP code I could write that could help me troubleshoot this further.  Thanks and please let me know what you reccomend trying next!

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 29 September 2006 - 03:16 PM

You havnt setup the my_database database for the root user to use. You'll need to change this via the mysql command line or use a third party database management software to change the permissions, such as phpmyadmin, or use MySQL Adminstrator.

If you use phpmyadmin. Login to phpmyadmin goto your database. Then click the Privileges tab/link chnage the permissions accordingly. There is nothing you can do with php.

#7 gBase

gBase
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 29 September 2006 - 09:23 PM

Thanks...that must be it!  Only problem: I'm using phpMyAdmin 2.8.02 and I don't see anything for privileges or permissions...I wish there was a phpmyadmin forum I could ask on.  The application is pretty user friendly for the most part, but I can't find anywhere to modify my permissions.  The plus side is I'm able to access the db now from my other user (non root.)  So I'm OK with that for now, but if you happen to know of where to find the permissions stuff that'd be a big helpf or my personal knowledge.  Thanks for your help!

#8 NevadaSam

NevadaSam
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationUSA

Posted 29 September 2006 - 11:46 PM

Your hosting service should have a control panel. Click on mySQL there. That may be where you set up the database before. There should be forms there for assigning users to the databases. Use a user_name and that user_password to connect to the server with.

#9 gBase

gBase
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 30 September 2006 - 12:19 AM

Great!  I'm all set, thanks everybody!  ;D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users