I have always used the following for MySQL database connection for procedural coding not OO running on PHP 4 and now I am moving some old websites to PHP 5.4:
<?php
global $db_user;
global $db_pass;
global $db_host;
global $db_name;
$db_user = 'USERNAME';
$db_pass = 'PASSWORD';
$db_host = 'HOST';
$db_name = 'DBNAME';
function do_query ( $sql )
{
global $db_user;
global $db_pass;
global $db_host;
global $db_name;
mysql_pconnect( $db_host, $db_user, $db_pass )or die("Cant connect to $db_host: ");
mysql_select_db( $db_name )or die("Cant select $db_name: ");
$res = mysql_query( $sql )or die("You messed up in your sql Using <b>$sql</b>\n\r" . mysql_error());
return $res;
}
?>
How can I change this to work with PHP 5.4 as all that is required for my website is to change this file as everything else now is up to date and the current way I connect with MySQLi never output errors the same way?
Thanks in advance.
UPDATE:
I tried the following:
<?php
$db_user = 'USERNAME';
$db_pass = 'PASSWORD';
$db_host = 'HOST';
$db_name = 'DBNAME';
function do_query ( $sql )
{
mysqli_connect( $db_host, $db_user, $db_pass )or die("Cant connect to $db_host: ");
mysqli_select_db( $db_name )or die("Cant select $db_name: ");
$res = mysqli_query( $sql )or die("You messed up in your sql Using <b>$sql</b>\n\r" . mysqli_error());
return $res;
}
?>
And use a mysqli_close(); at the end but it still doesn't work.
I also tried using it procedurally with:
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
But I can't seem to make that work as a function.
I have done loads of tests with different versions but I didn't want to over fill the page with information most people already know so I just put up the original code I have used in the past as I have so many other variations I have tried. I was not just trying to pass the buck, just trying to explain what exactly I was trying to convert to MySQLi. Sorry if people think this is pointless but I am stuck. Thanks.
mysqli_query
requires the connection passed as the first argument – Machavity Jul 26 at 0:37mysqli_close();
as shown, you need DB connection passed to it like somysqli_close($con);
while assigning DB connection like so$con = mysqli_connect($db_host, $db_user, $db_pass,$db_name)
-mysqli_close()
requires DB connection to be passed in as a parameter. – Fred -ii- Jul 26 at 0:54