0

I've got a problem with include. I'm doing some kind of blog, and at this moment it looks like this:

index.php
article.php
class/art.class.php

Let's focus on article.php, which looks like this:

<?php
$mysqli = new mysqli("","","",""); // here are my connection details
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

$mysqli->query("SET NAMES 'utf8'");
require("class/art.class.php");
$art = new Article();
print_r($art->get_art(trim($_GET['id'])));
$mysqli->close();
?>

And art.class.php is like this:

<?php
class Article {
    function get_art($id) {
        if(!is_numeric($id)) {
            header("Location: index.php");
            die("<h2>ID isn't numeric, cannot go on.</h2>'");
        }
        if($result = $mysqli->query("SELECT * FROM `articles` WHERE id='$id';")) {
            while($row = $result->fetch_array(MYSQLI_ASSOC)) {
                $art = $row;
            }
            $result->close();
        }
        return $art;
    }
}
?>

The problem is a response from MySQL. Sorry, I mean no response. And no errors. I figured out that I need to add mysql connection code to class code. But why? How I can connect once to database and call it from anywhere, even from included class? And sorry if my english is bad..

  • 1
    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) – deceze Mar 25 '14 at 13:11
  • In your class $mysqli is local variable. Do echo isset($mysqli) in your class. It should be false. – Tigran Mar 25 '14 at 13:13
  • You can pass $mysql as a constructor parameter – Hackerman Mar 25 '14 at 13:15
  • Or you can create static function with static variable and set it up. – Tigran Mar 25 '14 at 13:16

2 Answers2

1

The get_art function within the Article class does not have access to variables outside of it's scope: please see the answer here.

In order to fix your issue, you may provide access to the $mysqli object by passing it to the constructor of the Article class when you instantiate it:

Article.php:

$mysqli = new mysqli("","","",""); // your connection details
$art = new Article($mysqli);

art.class.php:

class Article {

    protected $mysqli;

    public function __construct($mysqli) {
        $this->$mysqli = $mysqli;
    }

    function get_art($id) {

        // Replace $mysqli with $this->mysqli everywhere you need to
        // make database calls

    }
}
Community
  • 1
  • 1
seeARMS
  • 1,670
  • 1
  • 12
  • 14
-1

Although some would recommend that you avoid doing so, you could use PHP's $GLOBALS variable to store your database connection:

$mysqli = new mysqli("","","",""); // here are my connection details
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
        . $mysqli->connect_error);
}
$GLOBALS['mysql'] = $mysqli;

This way you would have access to it within your class:

class Article
{
    function get_art($id)
    {
        $mysqli = $GLOBALS['mysqli'];
        ...
    }
}
Tim Burch
  • 1,088
  • 7
  • 9
  • @symcbean Can you explain what's "messy" about it? You're right that $mysqli was created in a global scope in this case; however, that's often not the case. – Tim Burch Mar 25 '14 at 14:01
  • Wikipedia is a good place to start - http://en.wikipedia.org/wiki/Global_variable, there's a lot more discussion about good and bad programming practices on the internet - try google for more links. – symcbean Mar 25 '14 at 16:59
  • @symcbean Weak response. That wikipedia article contains one dubious source and another that doesn't support your assertion at all. I can only assume you've subscribed to the popular notion that "global variables are evil," without thinking about it at all. You might want to try googling that notion yourself. Or riddle me this, if global variables are evil, then why do they continue to be a feature of PHP? Because of their utility, I would answer. – Tim Burch Mar 25 '14 at 17:12