0

I can connect to MySQL server but not the DB. So on my webpage it shows "Connected to MySQL" but then underneath that I get "Could not select examples which tells me it didn't connect to the database". Could anyone help?

<?php
$username = "user_admin";
$password = "Password";
$hostname = "localhost:3306"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>

**<?php
//select a database to work with
$selected = mysql_select_db ("my_dbname", $dbhandle)
  or die("Could not select examples");  
?>**

<?php
$sql = "SELECT * FROM `company` WHERE \'companyname\' like \'%a%\' LIMIT 0, 30 "; 

 while($row = mysqli_fetch_array($sql))
   {
   echo $row['companyname'];
   echo "<br>";
   }

 mysql_close($con);
?>
6
  • 3
    BTW what is it with the \'companyname\' Commented Dec 29, 2013 at 17:25
  • 1
    What it may reall be telling you is that the database my_dbname doesn't exist. Are you 10000% it does? Commented Dec 29, 2013 at 17:25
  • and you are sure you have that database ?? Commented Dec 29, 2013 at 17:25
  • mysql_ is deprecated. But use the command line to check permissions. i.e. dev.mysql.com/doc/refman/5.6/en/mysql.html Commented Dec 29, 2013 at 17:26
  • The companyname is a field within the company table. The database definitely exists, I have just double checked and it is holding within the company table over 849,000 rows. Commented Dec 29, 2013 at 17:32

2 Answers 2

2

As Lincb said, maybe the database doesn't exist.

Try to display the error to have more information :

$selected = mysql_select_db('my_dbname', $dbhandle);
if (!$selected ) {
   die ('Error database : ' . mysql_error());
} 
Sign up to request clarification or add additional context in comments.

12 Comments

I have used your script Tosx. Error database : Access denied for user 'my_username'@'localhost' to database 'my_dbname'
I think the information like password is not correct. Try to open directly your phpMyadmin or your database client, and access to your base. Your password is 'Password' ? Generaly the default identifiant is : login : root pwd : ""
This means you either have the wrong password, or you don't have permissions for that db. Can you try accessing it through PHPMyAdmin or some other way? If you can get into it there, then your script has the wrong password.
The password must be correct as there is only one used in the script. It connects to MYSQL but not to the DB, so if the password was incorrect surely it would fail on connecting to MYSQL too? I access MySQL through hostgator so I do not get prompted for any username or password and cannot see a logout option.
using 'hostgator' can you see your table ? Indeed is weird that the database connection don't failed. Try to list all database using this : $sql = "SHOW TABLES FROM $dbname"; $result = mysql_query($sql); if (!$result) { echo 'Error MySQL : ' . mysql_error(); }
|
1

THe most likely cause is that the database "my_dbname" doesn't exist. Also, use mysqli or PDO. mysql is deprecated and insecure.

2 Comments

The database name is not actually "my_dbname" it is something else which I don't want to post up.
OK, but make sure it's the same as what you have in your script. Anyway, I don't think this is the problem, based on your other comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.