• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Help me with PHP

kamejoko

New Member
Help me. I have this simple PHP code for connecting to a MySQL database. It works very well on localhost, but when connection to a remote MySQL server, it cannot returns the right result. The output is

Connected Selected Quereied
Warning: Supplied argument is not a valid MySQL result resource in d:\inetpub\wwwroot\check.php on line 9

It connected to the server, seleted the db sucessfully, but there is nothing in the result (my table is not empty of course). I don't know what can I do to make it works with remote MySQL servers. Can anyone help me to sort it out please. Thank you very very much.
This is the code:

PHP:
<?php
$inlink = mysql_connect("www.freesql.org","name","pass") or die("Cannnot connect");
echo("Connected\n");
mysql_select_db("dbname") or die("Cannot select");
echo("Selected\n");
$display_query = "SELECT * FROM tablename";
$display_result = mysql_query($display_query);
echo("Quereied\n");
$display_row = mysql_fetch_array($display_result);
?>
 
You are validating both the connect and the select calls (with ||die), which is good. You are not doing this for the mysql_query() call however.

Try this:

$display_result = mysql_query($display_query);
if(!$display_result) die("Error: " . mysql_error());

If the query itself is not successful for some reason, $display_result will be empty, and thus (as PHP is telling you) is not a valid resource result. mysql_error() will show the error message the MySQL server (or client) issued.
 
Back
Top