1

I am unable to fetch record from a MySQL database using PHP. Here is my code.

user.php:

require_once ('../../include/dbconfig.php');
require_once ('common.php');
error_reporting(E_ALL);
ini_set('display_errors', '1');
$userClass=new CommonConnectorFuncs();
$redata=$userClass->insertUserRecordForSignup();
echo $redata;exit;

common.php:

require_once ('../../include/dbconfig.php'); 
error_reporting(E_ALL);
ini_set('display_errors', '1');
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != "off") ? "https" : "http";
$imagepath=$protocol. "://" . $_SERVER['HTTP_HOST']."/connector/upload/";
class CommonConnectorFuncs{
     function __construct() {

    }
    // destructor
    function __destruct() {
        // $this->close();
    }
    public function insertUserRecordForSignup(){
        $sql=mysqli_query($connect,"select * from cn_user_info order by user_id desc");
        while ($row=mysqli_fetch_array($sql)) {
            $data[]=$row;
        }
        return $data;
    }
}

Here I am trying to fetch record and print through class but it's throwing the below message.

Notice: Undefined variable: connect in common.php on line 16

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in common.php on line 16

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in common.php on line 17

Notice: Undefined variable: data in common.php on line 20

Those query is working fine inside the user.php file but it's not working in common.php file.

halfer
  • 19,824
  • 17
  • 99
  • 186
satya
  • 3,508
  • 11
  • 50
  • 130
  • 2
    Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Qirel Apr 24 '17 at 10:28
  • `$connect` isn't visible inside your class. – Qirel Apr 24 '17 at 10:28
  • use `global $connect;` or parse the variable `connect` as a parameter – Rotimi Apr 24 '17 at 10:30
  • 2
    no, DONT use global $connect.. pass in the variable properly – DevDonkey Apr 24 '17 at 10:31
  • How to make this correct. – satya Apr 24 '17 at 10:32
  • Pass it as an argument to the constructor, and make it a variable of the class. Then use it as `$this->connect` inside the class (instead of `$connect`), if you've done it properly. – Qirel Apr 24 '17 at 10:37
  • agree with @DevDonkey I do not see the benefit to using $connect as global. Your function depends on a mysqli connection in order to work, so just make the connection a function parameter. – Shashank Shah Apr 24 '17 at 10:44

3 Answers3

0

As mentioned in comments, this is a scoping issue. Specifically, $connect is not in scope.

Notice: Undefined variable: connect in common.php on line 16 where connect is not defined anywhere.

Moreover, It's exactly as the error states as you're passing arguments to mysqli_query incorrectly. Assumming $connect is your mysqli connection generated at some point by new mysqli() it should be:

$sql = "select * from cn_user_info order by user_id desc";
$result = mysqli_query( $connect,$sql) or die('Could not look up user information; ' . mysqli_error($connect))

Hope it helps!

Shashank Shah
  • 2,077
  • 4
  • 22
  • 46
0

The problem is in that you are trying to access a global variable within a function.

First of all, make sure you include the php file with the relevant database connection. And as you are trying to access a global variable there are two ways to achieve this.

Method 1

Create a global variable at the top of the function.

global $connect;

But as Qirel says in this comment, it is a bad practise, so I'd suggest the next.

Method 2

Pass the connection to the function's parameters.

public function insertUserRecordForSignup($connect){
    $sql=mysqli_query($connect,"select * from cn_user_info order by user_id desc");
    while ($row=mysqli_fetch_array($sql)) {
         $data[]=$row;
    }
    return $data;
}

Hope you find this useful.

Community
  • 1
  • 1
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
0

Make the connect variable a property of your class as by parsing it in your construct function

require_once ('../../include/dbconfig.php');
require_once ('common.php');
error_reporting(E_ALL);
ini_set('display_errors', '1');
$userClass=new CommonConnectorFuncs($connect);
$redata=$userClass->insertUserRecordForSignup();
echo $redata;exit;

In your class,

class CommonConnectorFuncs{
    var $Connection; 
     function __construct($connection) {
       try{
        if($connection != null){
           $this->Connection = $connection;
        }else{
            throw new Exception('Connection is null. COuld not process further');
        }
       }catch(Exception $ex){
          echo $ex->getMessage();
          exit;
       }
    }
    // destructor
    function __destruct() {
        // $this->close();
    }
    public function insertUserRecordForSignup(){
        $sql=mysqli_query($this->Connection,"select * from cn_user_info order by user_id desc");
        while ($row=mysqli_fetch_array($sql)) {
            $data[]=$row;
        }
        return $data;
    }
}
Rotimi
  • 4,783
  • 4
  • 18
  • 27