Click here to Skip to main content
15,921,530 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I want to display product data from MySQL data base neatly using a while loop.
Obviously this wont work:

PHP
<?php
$Area = $_POST['Area'];
$connect = mysql_connect("localhost", "root") or die (mysql_error());
mysql_select_db($vendor, $connect);

$sql = "SELECT * FROM products WHERE Area ='$Area'";
$result = mysql_query($sql, $connect);
while ($row = mysql_fetch_array($result)) {
?><a href='product.php'><?
echo   "" . $row['Title'] . '<br>' ;
echo   "" .$row['Price'] . '<br>' ;
?></a><?

}
?>


Can anyone give me a solution? Can be any coding language as long as it works!
Thanks a lot!
Posted
Comments
Peter Leow 3-Dec-13 3:24am    
What errors did you encounter?

I have the following observations:
What is the value of $vendor?
Did you miss out the password to the database in mysql_connect("localhost", "root", ??password??)
UlrichvL 3-Dec-13 3:34am    
localhost returned unexpected end of file.

$vendor='product';

Don't have a password on MySQL.

1 solution

Assuming it's applicable with your queries, you're attempting to create the link at the wrong point.

It is possible to put the link building in your SELECT statement, itself:

SELECT '<a href="' + theLinkThing + '">Some Text</a>' AS MyLink, etc1, etc2, etc3


Note that 'Some Text' could also be from your query.

It's simply treated as another return value in SQL, in the above case, with an array index of [0] or ['MyLink']


 
Share this answer
 
Comments
UlrichvL 3-Dec-13 10:11am    
what do you mean by MyLink, etc1, etc2, etc3?
W Balboos, GHB 3-Dec-13 10:47am    
MyLink is just an arbitrary name that will be given to the field (column) wherein you build your link in the SQL query.
etc's are other fields you may want to return. 'etc' is commonly used in English, short for the Latin term et cetra, which roughly means, "and so on in a similar manner".

From the looks of your code, you may need substantially more help then a way to create links. Based upon your failed code, perhaqps you're very new to using php on a web page, You need to work on how to make these two items interact in then manner you expect.

For example: you have html text in the middle of your for loop: it is not valid php, so it will mess up your loop.
UlrichvL 3-Dec-13 10:59am    
Some text' AS * FROM products where area = " .$Area . ", Title,Price";
$result = mysql_query($sql, $connect);
while ($row = mysql_fetch_array($result)) {
echo "" . $row['Title'] . '<br>' ;
echo "" .$row['Price'] . '<br>' ;
UlrichvL 3-Dec-13 11:00am    
and yes, im quite new to coding, only started this year (im thirteen years old) and yes, I don't know everything when it comes to php
W Balboos, GHB 3-Dec-13 13:21pm    
Take your time. Master on platform, then start to add. It gets easier if you work from a more solid foundation.

Since you clearly have designs on web programming, make certain you're fluent in html/css .
Then, although I prefer php, javaScript is a better next step. Learn about "DOM".

Fortunately, many languages follow the C/C++ style. As I noted, master a few basic items, one at a time if necessary, and then you can really run with it.

Good Luck;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900