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 8889727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:19:55+00:00 2026-06-14T22:19:55+00:00

Is there anything wrong with connecting and closing to a database by calling the

  • 0

Is there anything wrong with connecting and closing to a database by calling the function below with the mysql_query and mysql_fetch_array commands between the two

<?php

function dbconnect()
{   
    $sql = "localhost"; 
    $username = "------";
    $password = "-----";
    $connection = mysql_connect($sql, $username, $password) or 
    die("unwable to cct");
    $databse = mysql_select_db("-------", $connection); 
    global $connection;
}

function close()
{
    global $connection;
    mysql_close($connection);
}

dbconnect();
$query = "Some SQL Statement";
$data = mysql_query($query, $connection); - L1
while (mysql_fetch_assoc($data))
{
  //echo something 
}
close();
?>

At present, I am getting an error saying that $connection at L1 needs to be a resource but is a BOOL. If I give a die statement there, the same is triggered. I have no idea what is wrong. Please spot any errors you can. I have to take a sabbatical from coding and I am back after a while.

Thanks & regards

  • 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-06-14T22:19:56+00:00Added an answer on June 14, 2026 at 10:19 pm

    You must use the global keyword before assigning the $connection variable. Otherwise, you declare a local $connection inside the function and then call a reference to the yet non-existent global $connection. In the other functions, that non-existent global is used.

    function dbconnect()
    {   
        // Global first to be sure the subsequent $connection is the global
        // rather than a new one local to this function
        global $connection;
    
        $sql = "localhost"; 
        $username = "------";
        $password = "-----";
        // Now this modifies the global $connection
        $connection = mysql_connect($sql, $username, $password) or die("unwable to cct");
        $databse = mysql_select_db("-------", $connection); 
    }
    

    More readable would be to use the $GLOBALS array:

    function dbconnect()
    {   
        $sql = "localhost"; 
        $username = "------";
        $password = "-----";
    
        // Using the $GLOBALS superglobal array
        $GLOBALS['connection'] = mysql_connect($sql, $username, $password) or die("unwable to cct");
        $databse = mysql_select_db("-------", $GLOBALS['connection']); 
    }
    

    Best of all would be to return $connection from dbconnect() and use that value in other functions:

    function dbconnect()
    {   
        $sql = "localhost"; 
        $username = "------";
        $password = "-----";
        $connection = mysql_connect($sql, $username, $password) or 
        die("unwable to cct");
        $databse = mysql_select_db("-------", $connection);
    
        // Return from the function
        return $connection; 
    }
    
    // call as 
    $connection = dbconnect();
    // and define your other functions to accept $connection as a parameter
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Php novice. 1.Is there anything wrong with this PHP & MySQL code? include_once db_login.php
Is there anything wrong with this (User model, line 95): scope :by_recent, where('created_at BETWEEN
Is there anything wrong or inherently unsafe about the way I've programmed this? I'm
Is there anything wrong with this code or can this be done more efficiently?
Is there anything wrong with having a single class (one .h) implemented over multiple
Is there anything wrong with checking so many things in this unit test?: ActualModel
Is there anything wrong with this code? My entity is not getting updated. public
i just wondering is there any performance issue or anything thing wrong if that
I have to pull data from at least 3 databases, is there anything wrong
is there anything more comfortable than this to detect a special page? The page

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.