Page 1 of 1

Successful connect, but failure on select

Posted: Sun Jun 15, 2008 7:59 pm
by Taxon
Successful connect, but failure on select

I used phpMyAdmin - 2.10.1 on the server as follows:

1) created a database named Library
2) created a table named Books
3) created fields named INV#, TITLE, AUTHOR
4) imported records
5) formulated and ran a successful query (using phpMyAdmin)
6) used "Create PHP Code" to generate $sql
7) inserted $sql at line 14. (below):

Code: Select all

 
<html>
<head><title>Test6.php</title></head>
<body>
<?php
error_reporting(E_ALL);
define("HOST","localhost");
define("USER","userxxx");
define("PASS","passyyy");
define("DB","Library");
$cxn = mysql_connect(HOST, USER, PASS, DB)
      or  die("Cannot connect to MySQL.<br>" . mysql_error());
echo "Successful connect: " . $cxn . "<br>";
$sql = 'SELECT * FROM `Books` LIMIT 0, 30 '; 
echo "Submitting: " . $sql . "<br>";
$result = mysql_query($cxn, $sql)
      or  die("Cannot select.<br>" . mysql_error());
echo "Successful select." . $result . "<br>";
?>
</body>
</html>
 
When accessing Test6.php with my browser, I get the following result:
Successful connect: Resource id #2
Submitting: SELECT * FROM `Books` LIMIT 0, 30
Cannot select.
It appears I am able to successfully connect, but that the select is failing. If someone could explain what am I doing incorrectly, it would be surely improve my Father's Day. Please enunciate clearly, as I'm a 67 year old PHP/MySQL novice who is somewhat hearing impaired. :D

Thanks,
Roger

Re: Successful connect, but failure on select

Posted: Sun Jun 15, 2008 8:19 pm
by John Cartwright
You passed the parameters to mysql_query() in the wrong order.

Code: Select all

$result = mysql_query($cxn, $sql) or die(mysql_error());
should be

Code: Select all

$result = mysql_query($sql, $cxn) or die(mysql_error());

Re: Successful connect, but failure on select

Posted: Sun Jun 15, 2008 10:31 pm
by Taxon
Jcart-

Thanks for pointing out the parameters transposition. I corrected them, and also cleaned up the code a bit. It now looks like this:

Code: Select all

 
<html>
<head><title>Test7.php</title></head>
<body>
<?php
error_reporting(E_ALL);
$host = "localhost";
$user = "userxxx";
$password = "passwordyyy";
$database = "Library";
$connection = mysql_connect($host, $user, $password, $database)
      or  die(mysql_error() . "<br>");
echo $connection . "<br>";
$sql = "SELECT * FROM Books"; 
echo $sql . "<br>";
$result = mysql_query($sql, $connection)
      or  die(mysql_error() . "<br>");
echo $result . "<br>";
?>
</body>
</html>
 
Test7.php now results in the following:
Resource id #2
SELECT * FROM Books
No database selected
Do you have any idea what I'm doing wrong to get the "No database selected" error message?

Thanks.
Roger

Re: Successful connect, but failure on select

Posted: Sun Jun 15, 2008 11:10 pm
by John Cartwright
I noticed you are passing $database as a fourth parameter to mysql_connect(), which is not what it is used for. You need to select which database you want to use using mysql_select_db()

I.e.,

Code: Select all

$connection = mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

Re: Successful connect, but failure on select

Posted: Mon Jun 16, 2008 1:57 am
by Taxon
Jcart-

Thank you so much. It's working great now!

Roger