Jump to content

Failure Php Request From Mysql!


sunicani
 Share

Recommended Posts

I got instructed by http://www.bicubica.com/apache-php-mysql/index.phpand want to retrieve information on MySQL via PHP scripting. however, I got the following information while trying http://localhost/mysql_test.php

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\mysql_test.php on line 15ID: Name:
but not the results like
ID: 1Name: John
which said in http://www.bicubica.com/apache-php-mysql/index.php!any experts who can guide the reason and, help solve the error, many thanks
Link to comment
Share on other sites

Well, without seeing the code this will be vague, but you have to supply mysql_fetch_array with a valid mysql_query. For example:$connect = (host, username, password, db)$query = mysql_query($connect, "SELECT name FROM example WHERE test='test'");$results = mysql_fetch_array($query);$name = $results["name"];

Link to comment
Share on other sites

yeah, I see that. maybe an error happened in SQL syntax, and, the original PHP code as below,

<?php# Define MySQL Settingsdefine("MYSQL_HOST", "localhost");define("MYSQL_USER", "root");define("MYSQL_PASS", "admin");define("MYSQL_DB", "test");$conn = mysql_connect("".MYSQL_HOST."", "".MYSQL_USER."", "".MYSQL_PASS."") or die(mysql_error());mysql_select_db("".MYSQL_DB."",$conn) or die(mysql_error());$sql = "SELECT * FROM test";$res = mysql_query($sql);while ($field = mysql_fetch_array($res));{$id = $field['id'];$name = $field['name'];echo 'ID: ' . $field['id'] . '<br />';echo 'Name: ' . $field['name'] . '<br /><br />'; }?>[\quote]I can't find error in the code, so who can help resolve? thanks, mates,
Link to comment
Share on other sites

while ($field = mysql_fetch_array($res));{$id = $field['id'];$name = $field['name'];echo 'ID: ' . $field['id'] . '<br />';echo 'Name: ' . $field['name'] . '<br /><br />';
I'm a newbie, so excuse me if I'm wrong, but I think the problem is on the while loop. Instead of "$field = mysql_fetch_array($res)", use "$field == mysql_fetch_array($res)".Regards
Link to comment
Share on other sites

No, the OP's syntax is correct. That loop actually will assign the row, as an array, to $field, but also update an internal pointer. When the pointer reaches the end, the loop will exit.The error the OP is getting specifically indicates an error in his query. It could be that his login details are incorrect, the MySQL server can't be reached, the database doesn't exist, the field doesn't exist, etc., and the way to figure out exactly what is wrong is to print the return value for mysql_error().

Link to comment
Share on other sites

What's going on with all the quotes here:$conn = mysql_connect("".MYSQL_HOST."", "".MYSQL_USER."", "".MYSQL_PASS."") or die(mysql_error());mysql_select_db("".MYSQL_DB."",$conn) or die(mysql_error());You don't need those. In fact, you never need to concatenate anything with an empty string, it doesn't do anything at all.Like Synook said, you can add error checking to the query call:$res = mysql_query($sql) or exit(mysql_error());

Link to comment
Share on other sites

hi, all, I've resolved the problem. instead of the original code I copied, I changed to the following,

<?php$con=mysql_connect("localhost","abc","123");if(!$con){die('could not conncet:'.mysql_error());}mysql_select_db("3shop",$con);$result=mysql_query("select P_id,ProductName,Brand,ProductDesc from cellphones");while($row=mysql_fetch_array($result)){echo $row['P_id']." ".$row['ProductName']." ".$row['Brand'];echo "<br />";}?>
by the way, I set up a new database as well as table in my SQL server.thank you all, mates Edited by sunshinecane
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...