0

I am wondering how $link can be undefined. It is a global inside class cdb.php and is set when the mysql connect is called. If it were undefined, when mysql connect was called, it would die because I have coded it this way.

<html>
<head>
</head>
<body>

<?php
function justGetCSNumbers($input)
{
$input = preg_replace('/[\D]/',"",$input);
$sp = preg_split("/,/",$input);
$numbs = preg_grep('/^(\d+)(,\d+)*$/',$sp);
$csv = implode(",",$numbs);
#echo $csv;
return $csv;
}
function queryDB($cleaned)
{
$split = preg_split('/,/',$cleaned);
$resAy = array();
for($i=0;$i<count($split);$i++)
{
        if((strlen($split[$i])>5)&&(strlen($split[$i])<10))
        {
        $resAy[$i] = "uid='$split[$i]'";
        }
}
if(count($resAy)>0)
{
$q = 'SELECT * FROM userbase WHERE '.$resAy[0];#.$whereclause;
echo '<br/> query: '.$q.'<br/>';
connectDB();
return mysql_query($q,$link) or die("Couldn't complete query ".mysql_error($link));
}
}
function find(){
$p = $_POST['userToQuery'];
if(isset($p))
{
$csv = justGetCSNumbers($p);
$found= queryDB($csv);
}
}
include('cdb.php');
find();
?>

Sorry for the poorly formatted code, using vi.

My apache2 error log shows that the variable $link is undefined when i use it here, even after calling connectDB(); which is the code that does a mysql_connect and therefore sets up the link. 'mysql_error() expects parameter 1 to be resource, null given' I refactored my code so the link would be defined (this version), but somehow I am having trouble.

[EDIT] Here is the cdb.php class:

<?php
function connectDB()
{
global $link;
$uname = 'site123';
$pass = 'abc123';
$loc = "localhost";
$link = mysql_connect($loc, $uname, $pass) or die("Couldn't connect to the DB");
$dbname = 'jagrail';
$db = mysql_select_db($dbname,$link);
        if(!$db)
        {die("Failed to select db");}
        if (mysqli_connect_errno()) {
                printf("Connect failed: %s\n", mysqli_connect_error());

        }
#return $link;
}
?>

1 Answer 1

1

EDIT:

Add:

global $link;

to the top of the queryDB() function, so that it knows $link is a global variable, rather than just a local.

Sign up to request clarification or add additional context in comments.

5 Comments

I was using that exact code previously. It always triggered the exact same error. As I said above, "It is a global variable inside class cdb.php and is set when the mysql connect is called. If it were undefined when mysql connect was called inside cdb.php, it would die because I have coded it this way." This method of getting $link works perfectly in another class I have, I don't know why its not working here.
Post your connectDB() method (omitting password, etc).
Ah, global variable, then add: global $link; to the top of the queryDB() function.
shouldn't calling connectDB() set the variable $link for the same scope that connectDB() was called in since $link is a global? btw, your above comment seems to have fixed it. But do I have to add global $link to the top of queryDB(), couldn't I just add $link to the top?
Nope, it sets it in the global scope (outside of any function), but any variables you immediately use inside a function are assumed to be local, not global - thats what the global keyword is for.

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.