Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7498391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:27:42+00:00 2026-05-29T19:27:42+00:00

I am wondering how $link can be undefined. It is a global inside class

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:27:46+00:00Added an answer on May 29, 2026 at 7:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Link to page: http://www.northtreestudios.com/sandbox/ Wondering how I can get the background images of my
I'm using rails 2.2.2 and wondering how can I set the params values to
I am wondering how I can add DATA LINK form to my WIN application.
I was wondering if I can use <h:outputStylesheet/> to link CSS from an external
I was wondering if anyone can give me a link on following a best
I'm wondering if anyone has a link to a good tutorial or can point
I was wondering if there is a helper method, which can create a link
I'm wondering if the link on the company logo, which usually goes the home
I am wondering why the link to profile looks like: http://stackoverflow.com/users/ID/NAME not simply: http://stackoverflow.com/users/ID
I was wondering if anyone has a link to any information regarding building in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.