0

This is a simple problem but for some reason I am not seeing it. I am changing a piece of code from MySQL to MySQLi. The problem is that when I use the new MySQLi code, the select does not work.

I am showing three pieces of code. 1- The Index.php file including the codes 2. The Connect.php file showing how I connect to the DB 3. The Redirect.php class code using a function that is failing.

Inside the Redirect.php at the bottom, I have included the OLD code that works and the NEW code that does not work. All I see in the difference is that the new code is using the $con to communicate to the DB and I've tried global $con and it also fails. Please, I need your help. Can you let me know what I am doing wrong. Thanks.

<?php 
/*******************************/
/*  HERE IS THE INDEX.PHP FILE */
/*******************************/
session_start();
    include("connection.php");
    include("redirect.php");
    $log=new redirect();
    $log->check_logged();               
    $theme=$log->get_theme();
    echo 'This is the Website Theme: ' . $theme . '<br>';
    exit;
?>

<?php 
/**************************************************/
/* HERE IS THE CONNECTION.PHP CODE BEING INCLUDED */
/**************************************************/
$con = mysqli_init( );
if (!$con)
    {
        die("ERROR: MySQLi Initialed failed");
    }

if (!mysqli_real_connect($con, SERVERNAME,USERNAME,PASSWORD,DATABASENAME, NULL, NULL, MYSQL_CLIENT_INTERACTIVE))
    {
        die("MySQLi Real Connect Error: " . mysqli_connect_error());
    }
?>

<?php 
/**************************************************/
/* HERE IS THE REDIRECT.PHP CODE BEING INCLUDED */
/**************************************************/
class redirect
{
    function __construct()
    {   }

    function check_logged()
    {
        if(isset($_SESSION['member_id']) && $_SESSION['member_id']!="")
        {
            if(isset($_GET['loginad']) && $_GET['loginad']==1)
                header('location:'.SITEADDRESS_MEMBER.'memberoverview.php?loginad=1');
            if(isset($_GET['surfad']) && $_GET['surfad']==1)
                header('location:'.SITEADDRESS_MEMBER.'memberoverview.php?surfad=1');   
            else
                header('location:'.SITEADDRESS_MEMBER.'memberoverview.php');
        }
    }

    function get_theme()
    {
        /************************************************/
        /* HERE IS THE OLD REDIRECT.PHP CODE THAT WORKS */
        /************************************************/
        $result = mysql_query("select * from theme where status='yes'");
        if(mysql_num_rows($result)>0)
        {
            $data=mysql_fetch_array($result);
            return $data['theme_name'];
        }


        /********************************************************************/
        /* HERE IS THE NEW REDIRECT.PHP CODE BEING TESTED THAT DOESN'T WORK */
        /********************************************************************/
        $result = mysqli_query($con, "select * from theme where status='yes'")
        if(mysqli_num_rows($result)>0)
        {
            $data=mysqli_fetch_array($result, MYSQLI_ASSOC);
            return $data['theme_name'];
        }
    }
}
?>

2 Answers2

0

There are other approaches, but the main issue is that $con is not avilable in the class. You can pass into the constructor, set it and use $this->con within the class:

include("connection.php"); // $con is defined here
$log = new redirect($con); // pass it to __construct()

class redirect
{
    public $con;

    function __construct($con) // accept it here
    {
        $this->con = $con;     // set it here
    }

    function get_theme()
    {
        // use it here
        $result = mysqli_query($this->con, "select * from theme where status='yes'");
    }
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
-1

You can use a singleton to have only one connection and easy to access when you need it :

class connection{

    private $con = null;

    public static function getCon()
    {
        if(null === $this->con)
        {

            $con = mysqli_init( );
            if (!$con)
            {
                die("ERROR: MySQLi Initialed failed");
            }

            if (!mysqli_real_connect($con, SERVERNAME,USERNAME,PASSWORD,DATABASENAME, NULL, NULL, MYSQL_CLIENT_INTERACTIVE))
            {
                die("MySQLi Real Connect Error: " . mysqli_connect_error());
            }

            $this->con = $con;
        }

        return $this->con;
    }
}

Then call it when you need it

function get_theme()
{
    /********************************************************************/
    /* HERE IS THE NEW REDIRECT.PHP CODE BEING TESTED THAT DOESN'T WORK */
    /********************************************************************/
    $result = mysqli_query(connection::getCon(), "select * from theme where status='yes'")
    if(mysqli_num_rows($result)>0)
    {
        $data=mysqli_fetch_array($result, MYSQLI_ASSOC);
        return $data['theme_name'];
    }
}

For information you can use mysqli in POO mode. I would recommend it coz you could easily return directly the mysqli object rather than just a $con

ThinkTank
  • 1,187
  • 9
  • 15