2

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?

2 Answers 2

9

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; 
} 
?> 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks , The global is easier.
The global is also bad coding practise... easier !== better
Absolutely agreed with @MarkBaker, You should avoid globals as much as possible. This should be accepted one.
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;
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.