Newbie - First proper script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ethoemmes
Forum Commoner
Posts: 26
Joined: Thu Aug 18, 2005 4:11 pm

Newbie - First proper script

Post 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]
korto
Forum Commoner
Posts: 36
Joined: Thu Aug 18, 2005 6:30 am
Location: Greece
Contact:

Post by korto »

What exactly do you mean by WHERE 1 in the query? Is it a typo error?
ethoemmes
Forum Commoner
Posts: 26
Joined: Thu Aug 18, 2005 4:11 pm

Post by ethoemmes »

Yes this was a typo. I have deleted the WHERE clause but still no joy.

Thanks for your time.
korto
Forum Commoner
Posts: 36
Joined: Thu Aug 18, 2005 6:30 am
Location: Greece
Contact:

Post 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
ethoemmes
Forum Commoner
Posts: 26
Joined: Thu Aug 18, 2005 4:11 pm

Post 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]
korto
Forum Commoner
Posts: 36
Joined: Thu Aug 18, 2005 6:30 am
Location: Greece
Contact:

Post 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]
ethoemmes
Forum Commoner
Posts: 26
Joined: Thu Aug 18, 2005 4:11 pm

Post 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]
korto
Forum Commoner
Posts: 36
Joined: Thu Aug 18, 2005 6:30 am
Location: Greece
Contact:

Post 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>";}
ethoemmes
Forum Commoner
Posts: 26
Joined: Thu Aug 18, 2005 4:11 pm

Post 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
ethoemmes
Forum Commoner
Posts: 26
Joined: Thu Aug 18, 2005 4:11 pm

Solved

Post by ethoemmes »

Spotted my mistake :x

I was using wrong field names......sorry for wasting your time :oops:
korto
Forum Commoner
Posts: 36
Joined: Thu Aug 18, 2005 6:30 am
Location: Greece
Contact:

Post by korto »

no worries
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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:wink:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that's perfectly valid, non-notice producing.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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
}
Post Reply