1


I'm trying to get a few columns from a certain SQL table, which does work like following:

<?php
include("config.php");

// SELECT DATABASE TO ECHO FROM
$sql = "SELECT * from GuildInformation";

// ECHO FROM THE ^SELECT
$data = $conn->query($sql);

foreach ($data as $row) {
// HERE IS SOME CODE WHICH DOES WORK
}
?>

^this method does work perfectly, now i want to create a function around it:

<?php
include("config.php");

function SelectGuildInfo() {
// SELECT DATABASE TO ECHO FROM
$sql = "SELECT * from GuildInformation";

// ECHO FROM THE ^SELECT
$data = $conn->query($sql);

foreach ($data as $row) {
// HERE IS SOME CODE WHICH DOES WORK
}}
?>

when i call the function SelectGuildInfo(); i get the following error code:

Fatal error: Call to a member function query() on a non-object in /users/l4g/www/84732842323772/submit.php on line 12

Line 12 is: $data = $conn->query($sql);

So when i include("config.php"); into my function it does work again, but i want the database connection to be outside of my function because i'll have more than 1 for this script and just 1 include.


Besides that i'm very new to this whole stuff and actually want to try using a class for my database connection so i came up with this:

    <?php

class dbConn{
    protected static $db;
    private function __construct(){
        try{
            self::$db = new PDO('mysql:host=localhost;dbname=NAME', 'USR', 'PWD');
            self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch(PDOException $e){
            echo "Connection Error: " .$e->getMessage();
        }
    }
    public static function getConnection(){
        if(!self::$db){
            new dbConn();
        }
        return self::$db;
    }
}

?>

How do i get to use my class in my script so i dont have to include something for every function i want to use?!

Thank you so much for helping me out!
Having now this code and it runs just perfectly!:)

include("database.class.php");

$db = dbConn::getConnection();

function SelectGuildInfo($db)
5
  • Refer to stackoverflow.com/questions/12769982/… Commented Nov 12, 2016 at 21:16
  • unfortunately i'm somewhat new to that and doesn't understand most of it @Fang but thank you Commented Nov 12, 2016 at 21:27
  • mysql class is deprecated. Use mysqli or PDO. As for the error message, it tells you that $conn is not an object in the context of the function Commented Nov 12, 2016 at 21:36
  • You're using it as a singleton. You should use Dependency Injection instead. Commented Nov 12, 2016 at 21:41
  • Yes thank you, fixed it <3 @Fang Commented Nov 12, 2016 at 22:46

1 Answer 1

1

Your code doesn't work because $conn inside the SelectGuildInfo() and $conn from your config are two different variables.

In order to make it work, you must add $conn = Database::getInstance() into your SelectGuildInfo (assuming $conn from your working example is instance of Database).

P.S. Please don't use mysql_* functions, use mysqli or PDO instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Yea i have updated my class, made a new one with PDO. at least i hope it is PDO tho - thank your for helping out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.