9

I am having a little problem passing from mysql_* to mysqli object oriented.

My index.php file is structured like including two files:

include('connect.php');
include('function.php');

The connect.php file contains:

<?php
$mysqli = new mysqli("localhost", "root", "test", "test");

if (mysqli_connect_errno($mysqli)) {
    printf("Connection failed: %s\n", mysqli_connect_error());
    exit();
}
?>

In the function.php file there is a function called showPage that takes no arguments but uses the $mysqli connection, in lines like...

$result = $mysqli -> query("SELECT * FROM $table ORDER BY ID DESC"); // Seleziono tutto il contenuto della tabella

I cannot manage it to work without passing to the function the $mysqli variable, but this was not necessary when I used mysql_* deprecated functions!

Can I understand why, and what's the best way to resolve this?

wiredmark
  • 1,098
  • 6
  • 26
  • 44
  • @Wired once you've removed your password, then probablu you still use it – iamart Dec 24 '12 at 03:02
  • 1
    As said before it was of an old database (in local too), I am testing on it. But still I don't want my data to be written all around the web =P it bothers me anyway – wiredmark Dec 24 '12 at 03:04

3 Answers3

10

User-defined functions have their own variable scope in PHP. You need to pass $mysqli to the function as a parameter, or start the function with global $mysqli.

This exact problem is given as an example on the Variable scope page:

However, within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. For example, this script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope. You may notice that this is a little bit different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition. This can cause some problems in that people may inadvertently change a global variable. In PHP global variables must be declared global inside a function if they are going to be used in that function.

<?php
$a = 1; /* global scope */ 

function test()
{ 
    echo $a; /* reference to local scope variable */ 
} 

test();
?>
quietmint
  • 13,885
  • 6
  • 48
  • 73
  • Global variables are evil xD but your was the best answer. I was used to C that's why I couldn't get that. I prefer passing the variables as parameter everytime at this point. Thanks. – wiredmark Dec 24 '12 at 02:59
  • 1
    In this case, a global variable is imo a perfectly valid and understandable design pattern. After all, the database connection object IS a global object in your application. However, I would wrap your mysqli object in a user created object of your own, and use this object globally, as this will give you the flexibility to make improvements/upgrades as necessary without having to make wholesale changes to your app. – Kevin Jhangiani Dec 24 '12 at 09:15
0

cannot manage it to work without passing to the function the $mysqli variable, but this was not necessary when I used mysql_* deprecated functions!

That is not really correct. Even in the old mysql_* function you actually had to pass the link identifier if you needed to specify to which database connection you were relating to, e.g.:

$result = mysql_query($sql, $link);

Also what this example shows, you had to pass along the $result, too. In case you did left out the $link parameter:

$result = mysql_query($sql);

The mysql extension did look internally for the last used connection. If none would have been found, it would have created a new one with the parameters set in php.ini. That just for your information to better understood why and how this worked in the past.

hakre
  • 193,403
  • 52
  • 435
  • 836
-1

You could also use connection pooling, it's worth to check this out.

$mysqli = new mysqli('p:localhost', 'username', 'password', 'db_name');
Community
  • 1
  • 1