0

I'm looking to create a formatted product list from an SQL database. My aim is to have a store on my website with a series of small boxes containing some shorthand information about each product, that when clicked will open a pop-up containing detailed information. (I have a working Javascript/JQuery code to create the pop-ups.)

Here is the PHP code so far, simply to get the information from the database and display it on a webpage... (I've been using XAMPP to provide an environment for me to test the code in)

<?php
mysql_connect("localhost", "root", "") or die (mysql_error ());

mysql_select_db("Database1") or die(mysql_error());

$strSQL = "SELECT * FROM Products";

$rs = mysql_query($strSQL);

while($row = mysql_fetch_array($rs)) {

echo $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "<br />";

}

mysql_close();
?>

I want the echoed line to be displayed in a divider, with a divider generated for each record in the SQL database (say I have 10 products available, there would be ten dividers, and 10 different boxes on the webpage). The divider's class is "ProductBox".

echo "<div class=\"ProductBox\">"; $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";

This was the closest I have come to a solution, which was simply managing to write a code with no syntax errors - alas, nothing actually displays on the webpage.

If I'm going about this entirely the wrong way please tell me - I'm fairly sure I need to use a SQL database to dynamically update stock on a live website, but if I need to implement a different programming language or whatever then just tell me what you think would work and help me with a solution.

1
  • 2
    obligatory notice: mysql is deprecated. please use mysqli. Commented Oct 14, 2013 at 19:54

2 Answers 2

3

You have an extra semicolon in your code

echo "<div class=\"ProductBox\">"; $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";

Replace with

echo "<div class=\"ProductBox\">". $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
Sign up to request clarification or add additional context in comments.

1 Comment

I thought it to be strange that the code was not giving me an error message, in spite of it displaying nothing - this has gotten me a lot closer to what I was looking for, thanks.
0

mysql_fetch_array needs to be used like this (see PHP Doc):

while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {

}

or you could just use "mysql_fetch_assoc" instead.

HOWEVER, if you're new to PHP, I HIGHLY RECOMMEND that you get started on the right foot. mysql_query functions are soon to be deprecated. DON'T USE THEM. Most recommend using "PDO" for querying your database. Here's a great tutorial to teach you: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

Also, as mentioned, you have an extra semi-colon.

1 Comment

Very useful article, once I've got everything working I'll definitely implement this newer function - this website will be going live so it's paramount that it is up to date in terms of compatibility.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.