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);
 ?> 
4

2 回答 2

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());
}
于 2012-08-29T23:14:51.560 回答
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.

于 2012-08-29T23:35:58.600 回答