0

I figured out hot to get contected to my database in line one I dont get a connection error anymore. But not Im getting an error that says no database selected. What could the issues be? Is my code correct?

First time reading from a mysql database in php. Thanks for the help.

<?php     

if (!($con = mysql_connect('host', 'user', 'pw'))) { 
   die('Connect failed: ' . mysql_error()); 
} 

mysql_select_db("my_db", $con) or die('Error select: '.mysql_error()); 



$result = mysql_query ('SELECT * FROM Gallerys') or die ('Error query: '.mysql_error ());

echo "<table border='1'>
 <tr>
 <th>Thumb Url </th>
 <th>Gallery Url</th>
 </tr>";

while($row = mysql_fetch_array($result))
   {
   echo "<tr>";
   echo "<td>" . $row['THUMBURL'] . "</td>";
   echo "<td>" . $row['GALLERYURL'] . "</td>";
   echo "</tr>";
   }
 echo "</table>";

mysql_close($con);
 ?> 
2
  • remove your connection detail! You don't want to be hacked right? Commented Aug 29, 2012 at 23:12
  • 4
    mysql_connect, mysql i _connect_errno - I think that might be the cause of your problem ;-) Commented Aug 29, 2012 at 23:14

2 Answers 2

2

In addition to getting rid of mysqli_... function do the following:

mysql_select_db("my_db", $con) or die('Error: '.mysql_error());

That will tell you what happened when you were selecting database. I bet you don't have my_db or you don't have permission to open it.


Final resultion to anyone following this thread - the problem was with the name of the datbase. It was not my_db but something else.

Sign up to request clarification or add additional context in comments.

10 Comments

I updated the code with your sugestions. I get the error (Error select: Access denied for user 'gallerys'@'%' to database 'my_db') Now.
I guess that would answer your question. You need to obtain permiission to the database.
Ah so my user id and password are wrong im using. thanks Ill check that.
What else could i be missing? I check my username and password and its correct for The database.
Its failing on this line (mysql_select_db("my_db", $con) or die('Error select: '.mysql_error()); ).... What is my_db?
|
2

Your connect-code is using mysql_connect, but your error-checking is using mysqli:

if (mysqli_connect_errno()) {
    exit('Connect failed: '. mysqli_connect_error());
}

Try updating to:

if (!($con = mysql_connect('host', 'user', 'pass'))) {
    die('Connect failed: ' . mysql_error());
}

Alternatively, you could update all of the references to mysql_* functions to actually use the mysqli_* functions - as this is recommended. This is a long-way-around to solve the problem at hand though, but it should also solve the problem.

Sample with mysqli:

$con = mysqli_connect('host', 'user', 'pass', 'db_name');
if (!$con) {
    die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}

3 Comments

Just as a small side note: !$con = mysql_connect('host', 'user', 'pass') -- this would work as expected as well.
@zerkms I know, however, I use Netbeans as my regular IDE; it gives a "suggestion"-highlight when attempting to assign something in a conditional-check to wrap the assignment in an additional set of parentheses so that you, and whoever-else reads your code knows you mean to do the assignment inside the conditional check
I updated the code with your sugestions now i get the error code (Error select: Access denied for user 'gallerys'@'%' to database 'my_db')

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.