php and mysql
I need a little bit of help on creating a webpage using php and mysql. I have a form on a webpage to organise the UK SuperMeet4 and i want the input on the form (which currently is emailed to 2 addresses using a php file) automatically displayed onto a table on another webpage. If anyone can help pls PM me or get me on any of the following:
msn messenger: ianeagle@hotmail.com
AOLim: EagsUK
Thanks
msn messenger: ianeagle@hotmail.com
AOLim: EagsUK
Thanks
If your question is specifically how to access the MySQL data from PHP and display it, here's the basics:
Use the following functions:
connectionobject = mysql_connect(host, username, password)
mysql_select_db(database)
resultobject = mysql_query(SQL, connectionobject)
mysql_close(connectionobject)
At the bottom of this post are a few wrapper functions to make things more readable in your code:
call ConnectToDatabase, then call GetRecordset with the appropriate parameters. When you're done with using the database, call CloseConnection. I haven't provided a wrapper function for inserting/updating/deleting records, so you'll have to do that yourself. Basically, to save the values from a form to the database, just do this (assuming you have a table called tblParticipant):
mysql_query("INSERT INTO tblParticipant (field1, field2, field3, ...) VALUES (value1, value2, value3, ...)");
To retrieve this data and display it, you could do something like this:
$rstParticipants = GetRecordset("tblParticpant");
echo "<table border=1>n";
echo "<tr><td>Name</td><td>Email Address</td><td>Owner?</td></tr>n";
while ($myrow = mysql_fetch_array($rstParticipants))
{
printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>n", $myrow[prtFirstName] . $myrow[prtLastName], $myrow[prtEmailAddress], $myrow[prtOwnerOrNot]);
}
echo "</table>n";
Hope this helps!
-Steve
function ConnectToDatabase($host, $database, $username, $pwd)
{
global $objConnection;
if( $host == "" || $username == "" )
{
die('No hostname or username specified.');
}
else
{
if( $objConnection )
{
CloseConnection();
}
$objConnection = mysql_connect($host, $username, $pwd) or die('Unable to connect to database: ' . mysql_error());
mysql_select_db ($database);
echo "Connected to database.
";
}
}
function CloseConnection()
{
global $objConnection;
mysql_close( $objConnection );
}
function GetRecordset($table, $fieldlist = "*", $where = "", $orderby = "")
{
global $objConnection;
$strSQL = "SELECT $fieldlist FROM $table";
if( $where != "" )
{
$strSQL = $strSQL . " WHERE $where";
}
if( $orderby != "" )
{
$strSQL = $strSQL . " ORDER BY $orderby";
}
echo "GetRecordset SQL = $strSQL";
$result = mysql_query($strSQL, $objConnection);
if (!$result)
{
echo "GetRecordset failed for SQL = $strSQL - " . mysql_error();
exit;
}
return $result;
}
Use the following functions:
connectionobject = mysql_connect(host, username, password)
mysql_select_db(database)
resultobject = mysql_query(SQL, connectionobject)
mysql_close(connectionobject)
At the bottom of this post are a few wrapper functions to make things more readable in your code:
call ConnectToDatabase, then call GetRecordset with the appropriate parameters. When you're done with using the database, call CloseConnection. I haven't provided a wrapper function for inserting/updating/deleting records, so you'll have to do that yourself. Basically, to save the values from a form to the database, just do this (assuming you have a table called tblParticipant):
mysql_query("INSERT INTO tblParticipant (field1, field2, field3, ...) VALUES (value1, value2, value3, ...)");
To retrieve this data and display it, you could do something like this:
$rstParticipants = GetRecordset("tblParticpant");
echo "<table border=1>n";
echo "<tr><td>Name</td><td>Email Address</td><td>Owner?</td></tr>n";
while ($myrow = mysql_fetch_array($rstParticipants))
{
printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>n", $myrow[prtFirstName] . $myrow[prtLastName], $myrow[prtEmailAddress], $myrow[prtOwnerOrNot]);
}
echo "</table>n";
Hope this helps!
-Steve
function ConnectToDatabase($host, $database, $username, $pwd)
{
global $objConnection;
if( $host == "" || $username == "" )
{
die('No hostname or username specified.');
}
else
{
if( $objConnection )
{
CloseConnection();
}
$objConnection = mysql_connect($host, $username, $pwd) or die('Unable to connect to database: ' . mysql_error());
mysql_select_db ($database);
echo "Connected to database.
";
}
}
function CloseConnection()
{
global $objConnection;
mysql_close( $objConnection );
}
function GetRecordset($table, $fieldlist = "*", $where = "", $orderby = "")
{
global $objConnection;
$strSQL = "SELECT $fieldlist FROM $table";
if( $where != "" )
{
$strSQL = $strSQL . " WHERE $where";
}
if( $orderby != "" )
{
$strSQL = $strSQL . " ORDER BY $orderby";
}
echo "GetRecordset SQL = $strSQL";
$result = mysql_query($strSQL, $objConnection);
if (!$result)
{
echo "GetRecordset failed for SQL = $strSQL - " . mysql_error();
exit;
}
return $result;
}
Thread
Thread Starter
Forum
Replies
Last Post








