Page 1 of 1
Newbie - First proper script
Posted: Thu Aug 18, 2005 4:18 pm
by ethoemmes
Hi
Please excuse me I am a complete newbie. I am trying to use the below script to display details of books which are stored on a mySQL database.
Basically I can connect to the database but when I run this script nothing happens, no data is returned.
Can anyone spot my probably obvious mistake? Also does any know of any guides on pulling data from a mySQL database and inputting into tables?
Code: Select all
<?php
include "config.php";
$table = "tblBooks";
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
// read data from database
$result = mysql_query("SELECT * FROM $table WHERE 1 LIMIT 0 , 30")
or die ("Could not read data because ".mysql_error());
// print the data in a table
if (mysql_num_rows($result)) {
print "<table cellpadding=2 cellspacing=0 border=0 width=\"100%\">\n";
while ($qry = mysql_fetch_array($result)) {
print "<tr>$qry[Author]</tr>";
print "<tr>$qry[Title]</tr>";
}
print "</table>\n";
}
mysql_close();
?>
Thanks for your time.
JCART | Please use Code: Select all
tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Thu Aug 18, 2005 4:25 pm
by korto
What exactly do you mean by WHERE 1 in the query? Is it a typo error?
Posted: Thu Aug 18, 2005 4:27 pm
by ethoemmes
Yes this was a typo. I have deleted the WHERE clause but still no joy.
Thanks for your time.
Posted: Thu Aug 18, 2005 4:34 pm
by korto
since you are getting any error messages I suppose the connection and queries are OK.
Try putting an else clause in the if (mysql_num_rows($result)) -in fact I think if($result) would work as well- so that you can see if the query is actually returning any records or not.
Another thing is that you are starting to print out '<table>' etc without including them in '<html><body>' elements
Posted: Thu Aug 18, 2005 4:43 pm
by ethoemmes
Please bear with me as I explained this is my first proper script.
Another thing is that you are starting to print out '<table>' etc without including them in '<html><body>' elements
This will sound silly but how would I do this. Do you mean like so?
Code: Select all
<html>
<body>
<?php
include "config.php";
$table = "tblBooks";
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
// read data from database
$result = mysql_query("SELECT * FROM $table")
or die ("Could not read data because ".mysql_error());
// print the data in a table
if (mysql_num_rows($result)) {
print "<table cellpadding=2 cellspacing=0 border=0 width=\"100%\">\n";
while ($qry = mysql_fetch_array($result)) {
print "<tr>$qry[Author]</tr>";
print "<tr>$qry[Title]</tr>";
}
print "</table>\n";
}
mysql_close();
?>
</body>
</html>
Also could you give me an example of the mentioned ELSE clause?
Thanks for your patience.
JCART | Please use Code: Select all
tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Thu Aug 18, 2005 4:50 pm
by korto
Either that or before printing "<table cellpadding=2 cellspacing=0 border=0 width=\"100%\">" print "<html><body>"
and also where you do (print "<tr>$qry[Author]</tr>";) do (print "<tr><td>$qry[Author]<td></tr>";)
as for the else clause I suggested that for debugging, so you can do:
Code: Select all
if (mysql_num_rows($result)) {
print "<table cellpadding=2 cellspacing=0 border=0 width=\"100%\">\n";
while ($qry = mysql_fetch_array($result)) {
print "<tr>$qry[Author]</tr>";
print "<tr>$qry[Title]</tr>";
}
else {
echo 'If statementis not valid, no records returned';
}
JCART | Please use Code: Select all
tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Thu Aug 18, 2005 5:05 pm
by ethoemmes
aahhh
Tried your suggestions and still not working! I just can't see whats wrong. No error is generated from the error handling so I guess the it is returning records.
I am now getting an error message
Parse error: parse error, unexpected T_PRINT in /usr/local/psa/home/vhosts/rrbltd.com/httpdocs/Development/Read_Cataloguev2.php on line 22
Which relates to the line
Code: Select all
print "<table cellpadding=2 cellspacing=0 border=0 width=\"100%\">\n";
My code now reads:
Code: Select all
<?php
include "config.php";
$table = "tblBooks";
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
// read data from database
$result = mysql_query("SELECT * FROM $table")
or die ("Could not read data because ".mysql_error());
// print the data in a table
if (mysql_num_rows($result)) {
print "<html><body>"
print "<table cellpadding=2 cellspacing=0 border=0 width=\"100%\">\n";
while ($qry = mysql_fetch_array($result)) {
print "<tr>$qry[Author]</tr>";
print "<tr><td>$qry[Author]<td></tr>";
print "</table>\n";
print "</html></body>"
}
else {
echo 'If statementis not valid, no records returned';
}
mysql_close();
?>
Once again thanks for your time and patience.[/quote]
Posted: Thu Aug 18, 2005 5:09 pm
by korto
You forgot to end the print "<html><body>" with a semicolon
On line 21 it should be print "<html><body>";
The same applies where yoou close those tags {print "</body></html>";}
Posted: Thu Aug 18, 2005 5:15 pm
by ethoemmes
No error but still no data.
Do you know of any good reference sites which have examples of what I am trying to achieve?
I'm sure this can't be too hard but would be easier if I could see a working example.
Any other suggestions?
Thanks
Solved
Posted: Thu Aug 18, 2005 5:17 pm
by ethoemmes
Spotted my mistake
I was using wrong field names......sorry for wasting your time

Posted: Thu Aug 18, 2005 5:30 pm
by korto
no worries
Posted: Thu Aug 18, 2005 6:23 pm
by raghavan20
print "<tr>$qry[Author]</tr>";
print "<tr><td>$qry[Author]<td></tr>";
should not you put them in quotes??? , I mean the keys of the array, $qry

Posted: Thu Aug 18, 2005 6:48 pm
by feyd
that's perfectly valid, non-notice producing.
Posted: Thu Aug 18, 2005 7:01 pm
by raghavan20
most of the times, i forget to do this
Code: Select all
$somearray = array("1", "2,"3,"4");
for($i =0; $i < count($somearray); $i++){
echo $somearray[i];//I put this instead of $somearray[b][$i][/b] and it never showed me a error but wont display anything
}