我应该把我的$ mysqli变量传递给每个函数? [英] Should I pass my $mysqli variable to each function?

查看:323
本文介绍了我应该把我的$ mysqli变量传递给每个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题从mysql_ *传递到mysqli面向对象。



我的 index.php 文件的结构,文件:

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

connect.php文件包含:

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

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

function.php 文件中有一个函数 $ mysqli 连接,以

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

我无法管理它<函数$ mysqli变量,但是这不是必要的,当我使用mysql_ *不推荐使用的函数!



我能理解为什么,这个?

解决方案

用户定义的函数在PHP中有自己的变量范围。您需要将 $ mysqli 传递给函数作为参数,或者使用 global $ mysqli 启动函数。 p>

这个确切的问题是作为一个例子给出的 Variable scope page:


但是,在用户定义函数中,局部函数范围是
介绍。在函数内使用的任何变量默认为
,局限于局部函数作用域。 例如,此脚本不会
产生任何输出,因为echo语句引用$ a变量的本地
版本,并且它在此之前未分配值
您可能会注意到,这与C语言中的
有点不同,因为C中的全局变量自动
可用于函数,除非被本地
定义特别覆盖。这可能导致一些问题,人们可能会
无意中更改一个全局变量。在PHP全局变量中,
必须在函数中声明为全局变量,如果它们将在
函数中使用。




 <?php 
$ a = 1; / *全局范围* /

function test()
{
echo $ a; / *引用局部范围变量* /
}

test();
?>


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?

解决方案

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();
?>

这篇关于我应该把我的$ mysqli变量传递给每个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆