0

I have an issue reading from a database. here is the php code after I have connected to the database

echo ("Connected to MySQL");
echo ("<hr /><br />");
mysql_select_db("test");
echo ("Connected to database");
echo ("<hr /><br />");

$sql = mysql_query ("SELECT * FROM 'Product'");


while ($row = mysql_fetch_array($sql)){
	echo ($rows['Product']);
	echo ("<br />");	
}

I am trying to display the items under the Products column on a web page.

this is the error I get

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /[SNIPPED]/test.php on line 12
4
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by sirlink99
0

That error typically indicates that your query did NOT succeed. To find out what was any error, change $sql = mysql_query ("SELECT * FROM 'Product'"); to $sql = mysql_query ("SELECT * FROM 'Product'") or die(mysql_error());

On the code you posted, the query is wrong. It should be Product (using backticks NOT apostrophes). On a US keyboard, the backtick is on the same key as the tilde(~) character. Thus, the correct statement is:

$sql = mysql_query ("SELECT * FROM `Product`") or die(mysql_error());

Also, echo ($rows['Product']); should be echo ($row['Product']);, with no "s"

Edited by mike_2000_17: Fixed formatting

0

I changed those, and now I get a

No database selected

output. I though I selected the database at line 3 in this code. Here is my newest code

echo ("Connected to MySQL");
echo ("<hr /><br />");
mysql_select_db("Test");
echo ("Connected to database");
echo ("<hr /><br />");

$sql = mysql_query ("SELECT * FROM `Product`") or die(mysql_error());


while ($row = mysql_fetch_array($sql)){
	echo ($row['Product']);
	echo ("<br />");	
}

thanks for your help

0

if that is you whole code you didn't login and select your database


add this before your other line

mysql_connect("localhost", "username", "password")or die("cannot connect");
mysql_select_db("database")or die("cannot select DB");

Edited by HITMANOF44th: n/a

0

In my first post I stated

here is the php code after I have connected to the database

and I do have line 2 of your code in my newest post.

what I ment to say in the first post was after I connect to MySQL. now after I added the or die I realize that I couldn't connect to the database. With the error "Access Denied for [username]". Is there a way to allow different user to connect to the database(add a user that can connect)?

Edited by sirlink99: n/a

0

yes if you have access to phpmyadmin or mysql directly you can add a user and set privlages for that database

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.