Jump to content

connect/select a database


gBase

Recommended Posts

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.
Link to comment
Share on other sites

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:

[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?");
?>
[/code]

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

I actually already tried adding to this code:

[code]$sql = 'SHOW DATABASES';[/code]

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?

Link to comment
Share on other sites

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:
[code=php:0]// 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());[/code]
Link to comment
Share on other sites

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

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

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!
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.