MySQL Pulling info into table & PHP tutorial sites

Associate
Joined
22 Dec 2004
Posts
1,828
Location
Southampton, UK
I'm asking this question for a friend, so i apologise if i'm not as descriptive or informative as i need to be.

So basically a friend is creating a website for the local kids. There will be a page on the site that has a calendar of events. This calendar is created using a table.

The events are listed in a MySQL table and friend would like to know what PHP is required to pull the info from the MySQL table into the web page.

An example of whats needed would be great.

As i have no clue on php or MySQL i said i'd try here and see if anyone can advise.

While i'm here can anyone recommend any good tutorial sites for learning PHP
 
you just need examples of mysql select queries with the php to implement them right? I have a great book called- PHP and MySQL web development by Welling & Thomson. It's brilliant for any php/MySQL apps from a begginer to an intermediate level. You can google search 'php mysql' queries and you'll get a fair amount of stuff returned.

an example query:

Code:
<table>
         <tr><td>HEADINGS HERE</TD>
         </tr>
$date_q = "select this_event from DATE_TABLE where date = '$date'";
$date_r = mysql_query($date_q) or die(mysql_error());

$date_rows = mysql_num_rows($date_r);

for($i=0;$i < $date_rows;$i++)
{
     $row = mysql_fetch_assoc($date_r);
     echo '<tr>';
     echo '<td>'.$row['date'].'</td>;
     echo '</tr>';

}

Very crude example but shows how you can pull stuff from a database and loop for every element matching your query.
 
Well I learnt all my PHP and MySQL stuff from here. Good as a reference too :)

The basis of what you need will be something like this:

Code:
<?php

// Conect to database
mysql_connect("host","username","password") or die (mysql_error());
mysql_select_db("dbname") or die (mysql_error());

// Get info from database
$getevents = mysql_query("SELECT * FROM events") or die (mysql_error());
$numevents = mysql_num_rows($getevents);

echo "There are $numevents in the database!";

echo "<table>";
echo "<tr><td>Event Name</td><td>Date</td></tr>";

while ($listevents = mysql_fetch_array($getevents)) {

echo "<tr><td>$listevents['eventname']</td><td>$listevents['eventdate']</td></tr>";

}

echo "</table>";

?>

I've not tested it but it should work fine :)
 
safeastinkywinky said:
Very crude example but shows how you can pull stuff from a database and loop for every element matching your query.

Indeed. It won't work anyway though- you actually have to connect to a database before you can pull information from it and theres also the fact that none of it is enclosed in <?php tags :p Wouldn't bother with a for loop either- a while loop is much better for this :)
 
safeastinkywinky said:
Fair enough, I just assumed they'd have all the connect info in a separate file!

Yeah, I normally do put it in a separate file which I then require() but if they don't have any previous PHP experience then they may wonder why it doesn't work :D
 
I use EzSQL. It's a wrapper to allow you to use the same code on multiple DBMS software without changing any PHP. The EzSQL functions make the code writing easier too I find :)
 
Back
Top Bottom