Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic Fetchrow 2

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
GB
The following code connects to the database but I am unable to fetch any records from a table. I have seen the example code in several places so I assume it is correct.

I can access the table via a Perl script but the log in for that also includes the Database name but the PHP version connects without the database name.
What am I missing?
Code:
<?php
$username = "xxxxxxxx";
$password = "yyyyyyyy";
$hostname = "00,00,000,000"; 

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


$query = "SELECT NUM,XAXIS,YAXIS FROM MORETOUR";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "Num :{$row['NUM']} <br>" .
"XAXIS : {$row['XAXIS']} <br>" . 
"YAXIS : {$row['YAXIS']} <br><br>";
} 
?>
and returns the following
Code:
Connected to MySQL

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /ServerPathAndScriptName on line 14


Keith
 
I appreciate you've blanked out the username, password and hostname but the hostname has commas in it and sort of looks like an IP (which should have '.' not ','). Is that intentional?

What happens if you run the query directly, not via PHP?

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
Thanks
Sorry - the commas in the hostname were down to me being in bad typing mode.
I have solved the problem - added '@mysql_select_db'.
I thought that the database name would have to be declared somewhere but none of the examples I saw included this line.
Googling the error shows that many more people have encountered the same problem.
Another one for the notebook.
Code:
<?php
$username = "xxxxxxxx";
$password = "yyyyyyyy";
$hostname = "00.00.000.000"; 

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

[red]|@mysql_select_db("dbname");[/red]

$query = "SELECT NUM,XAXIS,YAXIS FROM MORETOUR";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "Num :{$row['NUM']} <br>" .
"XAXIS : {$row['XAXIS']} <br>" . 
"YAXIS : {$row['YAXIS']} <br><br>";
} 
?>


Keith
 
Ah yes, that's an important bit :)
I didn't notice myself lol

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
you probably would have gotten ameaningull error had you added osme tpye or error checking to ytour query:

Code:
$result = mysql_query($query) [red]or die(mysql_error());[/red];

This would probably have told you something along the lines of:

"No database selected"

Ohh and you should never suppress errors when you are developing so your db selection would be better if it was:

Code:
$db=mysql_select_db('dbname') or die(mysql_error());

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I am more used to querying MySQL with Perl, there the host and database are combined in a single statement during connection. I assumed that getting the message 'connected to MySQL' meant that I was ready to access it.
Another lesson learned.

I normally only add error checking when I encounter a problem, should I have a die statement on every query throughout the script or should these be removed once fully working?


Keith
 
I assumed that getting the message 'connected to MySQL' meant that I was ready to access it.
Another lesson learned.

It is ready to be accessed, but of course the DB has to be selected, if you want to perform queries. Since you hadn't actually specified anywhere else, its only logical that something was missing.

Also for errors,
Its better to kill the script and handle the error, then to just let it fail.

You wouldn't want the script to continue if at all possible without whatever data you were extracting from the DB as that may cause additional errors down the line.

Also with the die, you can at least redirect the user to a "Sorry" page instead of having him see any errors that might be generated after the fact, or have the entire page just be jumbled because something went wrong at some point.

Also when developing its not a good idea to use the @ character before the functions to suppress the errors.








----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Code:
Also when developing its not a good idea to use the @ character before the functions to suppress the errors.
Once again, the examples I have seen show the @ symbol before the function so I assumed that was how the syntax should be written. Why are examples shown this way if the syntax is wrong?

Keith
 
The syntax is not wrong, its just not recommended when developing. @mysql_select_db() is perfectly valid, Its suppressing any errors that the function might generate, but its valid.

When developing, I think you'd want any and all errors to be shown, so you know what's wrong when something doesn't work.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top