| px | top | add code | search | signup | login | help |
<?php
function data_connect($cmd,$database)
// Title: Data Connect database connection abstraction function
// Author: William E. "haplo" Dunn Jr. haplo@epithna.com
// Version: 1.0 Tanis Release
// History: 1.0 Initial Code Release Works
// ToDo: Find a cleaner way to to do the data retrival from the database dataset
//
//Returns a result set to code it was called from which contains the results from the
//query it was passed
//Function assumes the use of the local host and a common web user which is used to
//access the databases
{
//First establish a connection to the sql server by connection ID in $datacon
$datacon = mysql_connect("database server","sql login","sql login password");
//make the requested $database passed to the function the active db uses $datacon to reference the
//connection
mysql_select_db($database, $datacon);
//use $datacon to send query to the server and retrieve requested data uses sql $cmd string passed
//to the function sets $result equal to the identifier of the result of the sql query
$result = mysql_query($cmd,$datacon);
//parse the results into an array for return to calling code uses $result to reference the dataset
//First get the number of rows in the dataset
$rrows = mysql_num_rows($result);
//set $test_val equal to the first value in the sql dataset
//it will be a boolean containing the value of false if there was no data returned
//it will be an array of the coloum values from the database if there was data returned from
//the query
$test_val = mysql_fetch_array($result);
//if the dataset has no results this boolean construct will return false
if (!$test_val)
{
//there was no data return false to the code that called data_connect so it knows there we
//no results and can take proper action based on that information
//clean up our database connections, not strictly required, but just good practice to do so
//free the memory used to hold the dataset
mysql_free_result($result);
//close the connection to the database
mysql_close($datacon);
//retun the vlaue false to the calling code
return false;
}
else
{
//there are results for the query we sent parse them out of te data set and put them into an
//array to be passed back to the calling code so it can make use of the dataset
//we will have to read through the sql dataset until we will have read all rows and put them
//into the array $rs
//set our iteration variable $i to 0 so we know it starts clean
$i = 0;
//setup a loop to parse the dataset into the array rs[], this will actually end up returning
//a two-dimentional array since we are filling the array with arrays of data represeting the
//coloums in the database
do
{
//stuff the contents of $test_val in the array $rs[] at the current free
//position
$rs[] = $test_val;
//get the next value in the data set
$test_val = mysql_fetch_array($result);
}
//loop until the iteration variable $i is equal to the number of rows in the result set
while (++$i != ($rrows));
//clean up our database connections, not strictly required, but just good practice to
//do so free the memory used to hold the dataset
mysql_free_result($result);
//close the connection to the database
mysql_close($datacon);
//return the array $rs[] representing the result set of the executed query to the
//calling code
return $rs;
}
} //end of data_connect function
?php>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.