Re: [PHP] Mysqli support - test or complain?



On Dec 10, 2007 3:16 AM, Dave M G <martin@xxxxxxxxxxxxx> wrote:

Which assumption should I be proceeding with?


Rather than assume (because we all know what happens then), why
not build for both scenarios? Here's an example of one way....

<?
// inc/include_files.php
#
# Includes all files necessary in one file.
#


include_once('inc/config.php');
include_once('inc/db.php');
include_once('func/db.php');
?>

<?
// inc/db.php
#
# Database connection information.
#


// Establish the connection with the primary database....
if(function_exists('mysqli_connect')) {
$db_conn = mysqli_connect($_DB[host],$_DB[user],$_DB[pass]) or die
('I couldn\'t connect to the database. Know why? Because MySQL is
complaining about something. Does this make sense to you...? :<BR
/>'.mysql_error()."\n");
} else {
$db_conn = mysql_connect($_DB[host],$_DB[user],$_DB[pass]) or die
('I couldn\'t connect to the database. Know why? Because MySQL is
complaining about something. Does this make sense to you...? :<BR
/>'.mysql_error()."\n");
}
?>

<?
// func/db.php
#
# Customized database functions.
#

function db_error($function) { // This is what we'll use to process
and output (or otherwise handle) errors.
$err = "MySQL error reached when calling function
".$function."(). MySQL said:<BR />\n";
$err .= "<FONT COLOR=#FF0000>";
if(function_exists('mysqli_error')) {
$err .= mysqli_error();
} else {
$err .= mysql_error();
}
$err .= "</FONT><BR />\n";
$err .= "<BR />\n";
return $err;
}

function db_query($sql) { // Simply return the connection resource ID
require('inc/config.php');
require('inc/db.php');
// Select the site database....
if(function_exists('mysqli_query')) {
$db_select = mysqli_select_db($_DB[name],$db_conn);
} else {
$db_select = mysql_select_db($_DB[name],$db_conn);
}
return $r;
}
?>

<?
// index.php
#
# The primary file.
#


$sql = "SELECT * FROM table_name";
// Note that you use the custom function `db_query()`,
// not `mysql_query()` or `mysqli_query()`.
$result = db_query($sql);
while($row = mysql_fetch_array($result)) {
// Do the rest of your stuff here.
}
?>

--
Daniel P. Brown
[Phone Numbers Go Here!]
[They're Hidden From View!]

If at first you don't succeed, stick to what you know best so that you
can make enough money to pay someone else to do it for you.
.



Relevant Pages

  • Re: hitting the limits
    ... relatively straight forward MySql database. ... 10K visitors a month and an 80Mb database are nothing. ... heavily loaded server. ... But the connection will hang around until the garbage ...
    (comp.lang.php)
  • Re: VFP8 & MySQL
    ... > VFP functions like NVL to a MySQL one, definitely it made the switch very ... > About your connection problem, I have mine setup as default for 100 ... As for the server, we left it ... The server crashed and corrupted the database. ...
    (microsoft.public.fox.programmer.exchange)
  • Re: VFP8 & MySQL
    ... Main Location and 3 branches (these are remotely accessing MySQL at the main ... About your connection problem, I have mine setup as default for 100 ... As for the server, we left it alone. ... The server crashed and corrupted the database. ...
    (microsoft.public.fox.programmer.exchange)
  • Re: Java and MySql program example ?
    ... > I am trying to learn Java and need an example program with MySql ... > MySql database. ... Database connection established ...
    (comp.lang.java.programmer)
  • Re: OdbcDataReader
    ... I did as you told and i find out that the connection to the MySql ... database closes and if i go to the MySql mamager i not able to open it ... >> While the odbccommand is reading the Access table I open the other ...
    (microsoft.public.dotnet.languages.csharp)