Simple Question, from database into a table

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

phelpsa
Forum Commoner
Posts: 48
Joined: Thu Feb 17, 2005 1:05 pm

Simple Question, from database into a table

Post by phelpsa »

Please could someone help me with this script:

Code: Select all

<?php

$conn = mysql_connect("localhost", "root", "");

if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}

if (!mysql_select_db("bugeye")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}

$sql = "SELECT Date, Event, Website
FROM ubb_events";

$result = mysql_query($sql);

if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}


while ($row = mysql_fetch_assoc($result)) {
$date = $row["Date"];
$event = $row["Event"];
$website = $row["Website"];
}

foreach (***) {
echo '$date<br>$event<br>$website<br>'; };

mysql_free_result($result);

?>
Especially where I have gone wrong from line 31 onwards, and what needs to go in the foreach brackets.

Thanks in advance.

Adam
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Post by Kadanis »

hi

what are you trying to achieve, if you can state your goal it might be easier for someone to help you.
phelpsa
Forum Commoner
Posts: 48
Joined: Thu Feb 17, 2005 1:05 pm

Post by phelpsa »

I'm trying to take the date, event and website link from the table ubb_events and post them into a table.

Adam
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Not sure how you want the table to look like.
Try replacing this:

Code: Select all

while ($row = mysql_fetch_assoc($result)) {
$date = $row["Date"];
$event = $row["Event"];
$website = $row["Website"];
}

foreach (***) {
echo '$date<br>$event<br>$website<br>'; };

mysql_free_result($result);
With this:

Code: Select all

echo '<table><tr><th>Date</th><th>Event</th><th>Website</th></tr>';

while ($row = mysql_fetch_assoc($result))
{
	echo '<tr><td>' . $row['Date'] . '</td><td>' . $row['Event'] . '</td><td>' . $row['Website'] . '</td></tr>';
}

echo '</table>';

mysql_free_result($result);
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Post by Kadanis »

Try this. You need to replace both you while loop and foreach with this code. It will loop over all the records in the table in your database and then print them out in a html table on the page.

Code: Select all

echo "<table width=\"760\" align=\"center\">";
echo "<tr><td>Date</td><td>Event</td><td>Website</td></tr>";
while ($row = mysql_fetch_assoc($result)) {
	$date = $row["Date"];
	$event = $row["Event"];
	$website = $row["Website"];
	echo "<tr><td>$date</td><td>$event</td><td>$website</td></tr>";
}
echo "</table>";
Hope it helps
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Post by Kadanis »

lol, Oren you beat me to it :lol:
phelpsa
Forum Commoner
Posts: 48
Joined: Thu Feb 17, 2005 1:05 pm

Post by phelpsa »

Thats great! Thanks a lot.

Adam
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Kadanis, I recommend you to change this:

Code: Select all

echo "<tr><td>Date</td><td>Event</td><td>Website</td></tr>";
Into this:

Code: Select all

echo '<tr><td>Date</td><td>Event</td><td>Website</td></tr>';
(I replaced the double quotes with single quotes)

From the php.net site: When a string is specified in double quotes or with heredoc, variables are parsed within it.
Since you don't have any variables on this line, there is no need to call to the parser :wink:
phelpsa
Forum Commoner
Posts: 48
Joined: Thu Feb 17, 2005 1:05 pm

Post by phelpsa »

The script is used in a template, here is the script I've finally come up with. The template is loaded fine but it doesnt show the results.

Code: Select all

mysql_connect("localhost", "root", "");

mysql_select_db("bugeye");

$sql = "SELECT Date, Event, Website
FROM ubb_events
ORDER BY Date AS
LIMIT 5";

$result = mysql_query($sql);



while ($row = mysql_fetch_assoc($result))
{
        $events = '<tr><td>' . $row['Date'] . '</td><td><a href="' . $row['Website'] . '">' . $row['Event'] . '</a></td></tr>';
}

mysql_free_result($result);

eval('echo "'.template('Events').'";');
Template:

Code: Select all

<table><tr><th>Date</th><th>Event</th></tr>
$events
</table>
Could someone help me even further?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Try this:

Code: Select all

$events .= '<tr><td>' . $row['Date'] . '</td><td><a href="' . $row['Website'] . '">' . $row['Event'] . '</a></td></tr>';
Note the '.' (period) before the '=' sign.
phelpsa
Forum Commoner
Posts: 48
Joined: Thu Feb 17, 2005 1:05 pm

Post by phelpsa »

Unfortunately that made no difference :(

It shows the table with the 'Dates' and 'Events', but not the results.

Adam
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$events isn't output or given to anything to output.
phelpsa
Forum Commoner
Posts: 48
Joined: Thu Feb 17, 2005 1:05 pm

Post by phelpsa »

Sorry, I'm new to this, please could you explain a bit further? :wink:

Adam
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Template:

Code: Select all

echo '<table><tr><th>Date</th><th>Event</th></tr>' . $events . '</table>'
phelpsa
Forum Commoner
Posts: 48
Joined: Thu Feb 17, 2005 1:05 pm

Post by phelpsa »

There can't be any PHP in the template, only variables.

Adam
Post Reply