Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Since it's just three function calls, it's rather easy to edit. Where did you stumble specifically? (Also note that it's however not a smart idea to repeatedly call the database connection function for each query.) –  mario Jul 26 at 0:26
    
At least inject your connection into your function if you're going to do it that way. Don't make one connection per query. And mysqli_query requires the connection passed as the first argument –  Machavity Jul 26 at 0:37
    
Each time this is used, it is for one page when loading and then it shuts the connection with mysqli_close(); when done it reaches the end of the page/script. It is not constantly open. –  R A Jul 26 at 0:41
    
Sidenote: If you're actually using mysqli_close(); as shown, you need DB connection passed to it like so mysqli_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
    
Sorry. I was using that for the $link example. Thanks. –  R A Jul 26 at 0:56

1 Answer 1

up vote 2 down vote accepted

Three flaws in one post.

First, using a function to create a connection and then query is a bad idea.

Second, you're using a persistent connection. So even when the page is done the connection remains. You'll exhaust your pool of database connections like that.

Third, you're using mysql_ functions, which are deprecated.

So here's the preferred way to do things now, using mysqli and an object approach. I don't recommend $GLOBALS normally but it sounds like you're working with legacy code

$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
function do_query($sql) {
    $mysqli = $GLOBALS['mysqli'];
    $res = $mysqli->query($sql)or die("You messed up in your sql Using <b>$sql</b>\n\r" . mysqli->error);
    return $res;
}    
share|improve this answer
    
Thanks you. The problem is that I have that function used throughout the websites as the are from over 4 years ago and I don't want to spend much time on those website as they are very basic so that is why I want to stick with the function. What are the downsides to using it as a function please? –  R A Jul 26 at 0:39
    
If I use the object approach, will it still work with the scripts I have that are set out as procedural using the function? –  R A Jul 26 at 0:47
    
I've changed it to use injection of the object. Regardless of which you use, mysqli_query (object or procedural) returns a mysqli_result object. You can still use mysqli_fetch_assoc() and similar functions to procedural, but they exist purely for legacy. –  Machavity Jul 26 at 0:51
    
Thanks. So I can still use mysql_fetch_assoc(), mysql_num_rows(), etc, in a procedural format? –  R A Jul 26 at 0:55
    
More or less. Just be sure it's mysqli_ –  Machavity Jul 26 at 0:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.