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

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"

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

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");

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)?

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

I have been searching, but I couldn't find it. where is it located?

Member Avatar for diafol

What phpmyadmin on localhost?

http://localhost/phpmyadmin/


Go to the Privileges TAB

Either ADD new user or change the privileges:

never mind I found it and got it working. thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.