Hi all,
And thanks ever so much for everybody's responses! I'll address each of them!
Nile: I created a new page based on your cleaned-up code, but got the following error when I ran it:
Parse error: syntax error, unexpected $end in C:\wamp\www\chris\petdisplay2.php on line 45
Jas: I'm fairly sure my query is right - when I run the query by querying the MySQL db directly it returns the relevant records. I tried inserting your code:
PHP Code:
echo $query;
$results = MySQL_Query($query) or die(MySQL_Error());
But wasn't 100% sure where I should insert it, I placed it beneath the line:
PHP Code:
$result = mysql_query($query)
I still received the same error as before with no more helpful info. Re privileges + permissions, I've checked this and have used the root@localhost user which has full permissions but it's still giving me the error.
Allahverdi: I tried adding MYSQL_ASSOC: as follows:
PHP Code:
$row = mysql_fetch_array($result, MYSQL_ASSOC)
But again-no joy!
Kuau: Re the uppercase lowercase suggestion, the book I'm working from displays the databases/tables with an initial uppercase letter, and same for tables - I've copied this in my db and as you'll see further down below I've since discovered that capital letters are not an issue.
SO!!! Thanks for your responses. What I tried tonight after your suggestions was moving onto the next tutorial that my book offers based on near enough the same code... a slightly more advanced page which uses a for loop rather than a while loop.
Strangely the slightly different page loaded perfectly!! Which puts to rest any debate about uppercase/lowercase, incorrect login, privileges etc.
So, whilst this is good, my inquisitive brain isn't satisfied! Why does this code continue to fail:
PHP Code:
<?php
/* Program: petdisplay.php
* Desc: Displays all pets in selected category.
* /
?>
<html>
<head><title>Pet Catalog</title></head>
<body>
<?php
$user="root";
$host="localhost";
$password="";
$database = "PetCatalog";
$connection = mysql_connect($host,$user,$password)
or die ("Couldn't connect to server");
$db = mysql_select_db($database,$connection)
or die ("Couldn't select database");
$pettype = "horse"; //horse was typed in a form by user
$query = "SELECT * FROM Pet WHERE petType='$pettype'";
$result = mysql_query($query)
or die ("Couldn't execute query.");
/* Display results in a table */
$pettype = ucfirst($pettype)."s";
echo "<h1>$pettype</h1>";
echo "<table cellspacing='15'>";
echo "<tr><td colspan='3'><hr></td></tr>";
while ($row = mysql_fetch_array($result))
{
extract($row);
$f_price = number_format($price,2);
echo "<tr>\n
<td>$petName</td>\n
<td>$petDescription</td>\n
<td align='right'>\$$f_price</td>\n
</tr>\n";
echo "<tr><td colspan='3'><hr></td></tr>\n";
}
echo "</table>\n";
?>
</body></html>
Whilst this code runs perfectly?!:
PHP Code:
<?php
/* Program: petDescripFor.php
* Desc: Displays a numbered list of all pets in
* selected category.
*/
?>
<html>
<head><title>Pet Catalog</title></head>
<body>
<?php
$user="root";
$host="localhost";
$password="";
$database = "PetCatalog";
$connection = mysql_connect($host,$user,$password)
or die ("couldn't connect to server");
$db = mysql_select_db($database,$connection)
or die ("couldn't select database");
$pettype = "horse"; //horse was typed in a form by user
$query = "SELECT * FROM Pet WHERE petType='$pettype'";
$result = mysql_query($query)
or die ("Couldn't execute query.");
$nrows = mysql_num_rows($result);
/* Display results in a table */
echo "<h1>Horses</h1>";
echo "<table cellspacing='15'>";
echo "<tr><td colspan='4'><hr></td></tr>";
for ($i=0;$i<$nrows;$i++)
{
$n = $i + 1; #add 1 so that numbers don't start with 0
$row = mysql_fetch_array($result);
extract($row);
$f_price = number_format($price,2);
echo "<tr>\n
<td>$n.</td>\n
<td>$petName</td>\n
<td>$petDescription</td>\n
<td align='right'>\$$f_price</td>\n
</tr>\n";
echo "<tr><td colspan='4'><hr></td></tr>\n";
}
echo "</table>\n";
?>
</body></html>
Are there any particularly clever people who want to offer their suggestions?!
Bookmarks