这是我的代码:
<?php
require_once 'classes/dbconnect.php';
$connector = new dbconnect();
pp();
function pp(){
$uid="0000007";
$table = $connector->query("SELECT * FROM tc_personal WHERE uid = '$uid'");
$tb = mysql_fetch_object($table);
print $tb->name;
}
?>
但是这段代码不起作用,因为pp()函数无法访问$ connector。 如何将全局变量定义为$ connector?
答案 0 :(得分:9)
肯定比使用全局变量更好,即使不是OOP
<?php
require_once 'classes/dbconnect.php';
$connector = new dbconnect();
pp($connector);
function pp($connector){
$uid="0000007";
$table = $connector->query("SELECT * FROM tc_personal WHERE uid = '$uid'");
$tb = mysql_fetch_object($table);
print $tb->name;
}
?>
答案 1 :(得分:4)
<?php
require_once 'classes/dbconnect.php';
global $connector;
$connector = new dbconnect();
pp();
function pp(){
global $connector;
$uid="0000007";
$table = $connector->query("SELECT * FROM tc_personal WHERE uid = '$uid'");
$tb = mysql_fetch_object($table);
print $tb->name;
}
?>