|
There are a whole slew of mysql_ functions available to the PHP Programmer.
The functions that I myself use the most are;
mysql_connect(); or mysql_pconnect();
mysql_select_db();
mysql_query();
mysql_fetch_array();
mysql_error();
Using some of the above functions
Creating a connection to the database, select a database and run a query;
<?
$conn = mysql_connect("localhost","userid","password") or DIE("Unable to
Connect to Server");
mysql_select_db("dbname") or DIE("Failed to Connect to Database");
$query = "SELECT * from tablename";
$res = mysql_query($query) or DIE("Failed to run Query");
?>
99% of the time I use the following code to do error reporting. Found this code playing with PHPgen
<?
$verbose_queries=0;
function mysqlquery($db_name,$query) {
global $verbose_queries;
if($verbose_queries !=0)
echo $query . "<BR*>";
$result = mysql($db_name,$query);
return $result;
}
// I call this function like this
$db_name = "databasename";
$conn = mysql_connect("localhost","username","password");
$_query = "SELECT * from tablename";
$res = mysqlquery("$db_name","$query");
if(!empty($res) {
// Do something or nothing
} else {
echo mysql_error() . "<BR>";
}
?>
>> Comments/FeedBack
|