Reputation: 337
This is my code :
<?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;
}
?>
but this code will not work because the pp() function can't access the $connector. How can i define a global variable as $connector?
Upvotes: 2
Views: 8293
Reputation: 212522
Surely better than using globals, even if not 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;
}
?>
Upvotes: 9
Reputation: 222408
<?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;
}
?>
Upvotes: 4