Log in

View Full Version : Correct Syntax



john0611
12-02-2009, 07:31 PM
Hi all,

This one has been really doin my head in, I am wonderin if anyone could help, am I using the correct syntax to return data from mysql, mybe my query is at fault, its not getting any data echoed correctly? displaying all data, rather that just the data relating to the category, products and images.

The DB:

category:

id (primery key)
category_id (index)

products:

id (foriegn key to category_id)
name
description
measurements
img_id (index)

images

image_id (foriegn key to img_id)
name
filename
thumbnail





<?php

// query db
if (isset($_GET['id'])) {
$sql = "SELECT * FROM `category` WHERE category_id = '" . mysql_real_escape_string($_GET['id']) . "'"; // from category get id
if ($result = mysql_query($sql)) {
if (mysql_num_rows($result)) {
$row = mysql_fetch_assoc($result);
echo "<h2>" . $row['category_id'] . "</h2>";
} else {
echo "Something is wrong!";
}
}
}
?>
<div class="datadivleftcontent">



<?php // queries DB and echo results relating to products & images
// query db
if (isset($_GET['id'])) {
$query = "SELECT products.*, images.image_id, images.filename, images.thumbnail FROM products, images WHERE image_id = '" . mysql_real_escape_string($_GET['id']) . "'";

if ($result = mysql_query($query)) {
if (mysql_num_rows($result)) {
while ($row = mysql_fetch_assoc($result)) {

echo "<h4>" . $row['name'] . "</h4>"; // echo date results as format "h4"
echo "<p>" . $row['description'] . "</p>"; // echo content as p format "body text"
echo "<h5>Measurements: " . $row['measurements'] . "</h5>"; // echo content as p format "body text"



echo "<a href='img/products/{$row['filename']}' rel='lightbox' title='{$row['name']}'>";

echo "<img src='img/products/thumbnails/{$row['filename']} 'alt='{$row['name']}' title='{$row['name']}' width='156' height='109'></a>";

}
} else {
echo "Something is wrong!";
}
}
}
?>

Hope some of this makes sense, Thanks you for any help and suggestions.