gBase Posted September 29, 2006 Share Posted September 29, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22446-connectselect-a-database/ Share on other sites More sharing options...
fenway Posted September 29, 2006 Share Posted September 29, 2006 I don't know what you mean by "sync up"... and SHOW DATABASES. Quote Link to comment https://forums.phpfreaks.com/topic/22446-connectselect-a-database/#findComment-100646 Share on other sites More sharing options...
gBase Posted September 29, 2006 Author Share Posted September 29, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/22446-connectselect-a-database/#findComment-100828 Share on other sites More sharing options...
wildteen88 Posted September 29, 2006 Share Posted September 29, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/22446-connectselect-a-database/#findComment-100878 Share on other sites More sharing options...
gBase Posted September 29, 2006 Author Share Posted September 29, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/22446-connectselect-a-database/#findComment-100905 Share on other sites More sharing options...
wildteen88 Posted September 29, 2006 Share Posted September 29, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22446-connectselect-a-database/#findComment-100909 Share on other sites More sharing options...
gBase Posted September 29, 2006 Author Share Posted September 29, 2006 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! Quote Link to comment https://forums.phpfreaks.com/topic/22446-connectselect-a-database/#findComment-101135 Share on other sites More sharing options...
NevadaSam Posted September 29, 2006 Share Posted September 29, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22446-connectselect-a-database/#findComment-101195 Share on other sites More sharing options...
gBase Posted September 30, 2006 Author Share Posted September 30, 2006 Great! I'm all set, thanks everybody! ;D Quote Link to comment https://forums.phpfreaks.com/topic/22446-connectselect-a-database/#findComment-101203 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.