Jump to content

[SOLVED] mysqli->query() doesn't seem to work inside functions! HEEEELP!! *sob*


theinfamousmielie

Recommended Posts

Hey all, first post ... hooray for forum-virgins!

 

Okay, happiness over ... the big problem is this. I rate myself as a fairly clued-up PHP developer dude. So i've been writing what is essentially a framework to simplify my web-development process. Yes, i hear you all saying "why? use something that's already made" ... well i wanna make it myself. It's part of the challenge.

 

Anyway so ... i have three folders, two of which are main ones ... there is a CoreModules folder which contains all my classes, grouped and named according to function (i.e. DatabaseController, MailController) and a folder of once-off generic functions that could be used anywhere, but get included at the beginning of any script just after the classes are all included. This is great, and has been working wonderfully. So you'll see a whole lot of my own class definitions in the code i paste.

 

THE PROBLEM: I am trying to connect to and query a DB table, and i'm using the mysqli extension. It connects to the database fine (returns an empty MySQLi object) and everything is okay until i try to run anything that basically uses ->query() ... at which point, i get a NULL return. Nada. Nothing.

 

I'm not sure what code you want me to paste to help out but i'll give you a snippet of the CountResults() function (on an aside, i realise the function is somewhat redundant but ... alas for backward compatibility!) - $this->DatabaseLink is obviously the resource from the connection.

 

The code itself works perfectly in a situation where i'm on the 'main' page and i'm querying, but it fails when inside another function. I'm thinking it has something to do with global something-or-other but ... variables that get set when initializing the controller are there and the connection is fine so ... surely everything else should be too??

 

CODE:

 

==========================

DatabaseController.php

==========================

 

...

...

 

function CountResults() {

 

$Query = $this->Query;

 

$DatabaseQuery = $this->DatabaseLink->query($Query);

 

if (@!$DatabaseQuery->num_rows) {

$NumRows = 0;

} else {

$NumRows = @$DatabaseQuery->num_rows;

}

 

// Close results set to free up memory.

if ($NumRows != 0) {

$DatabaseQuery->free_result();

}

 

return $NumRows;

 

}

 

...

...

 

Does anyone have any ideas??? :(

 

Link to comment
Share on other sites

Hi thorpe

 

The query in itself is perfect (  SELECT `Text` FROM `DisplayMessage` WHERE `Code` = '{$MessageArray[$i]}'    ... can't get much simpler than that ;) ) and the same procedure/query running outside of the function works perfectly and returns successfully. When it is inside the function, it messes up. The query gets passed correctly to ->query() ... but at that moment, it doesn't 'FAIL' or give me errors (i removed all error suppression and var_dumped like crazy) ... it just gives me emptyness.

 

Essentially:

 

$query = "SELECT `Text` FROM `DisplayMessage` WHERE `Code` = 'InvalidLoginBadPassword'";

var_dump($mysqli->query($query)); exit;

 

returns emptyness ... AKA null. But only when sitting inside of another function.

Link to comment
Share on other sites

Fair enough, hehe.

 

Links to the 2 culprit files:

http://aurora.digiworks.co.za/Temp/DisplayMessage.php.txt  <-- that would be the random function where it doesn't work

http://aurora.digiworks.co.za/Temp/MySQLDatabaseController.php.txt  <-- that would be the main database controller.

 

Both these files get included when doing this:

$PathPrefix = "../../";

include $PathPrefix . "CoreScripts/InitializeAurora.php";

 

 

 

the code that causes all the fuss:


<?php
session_start();

// Initialize Aurora Connection
$PathPrefix = "../../";
include $PathPrefix . "CoreScripts/InitializeAurora.php";

// Instantiate Core Controllers (add more as needed)
$Database = new DatabaseController;
$Debug = new DebugController;
$Page = new PageController;

// Create Database Connection
$Connection = $Database->ConnectToDatabase();

//$Debug->PreArray($_POST);

// Get variables
$GetName = $Page->GetVariable("LocationName", "", "post");
$GetCode = $Page->GetVariable("Code", "", "post");
// etc etc, long list.


// Start writing to the database, but perform checks first
$ErrorArray[] = "NoLocationName";
$ErrorArray[] = "NoLocationCity";
$ErrorArray[] = "NoLocationOfficeHour";

DisplayMessage($ErrorArray);

?>

Link to comment
Share on other sites

By the way, i removed all error-reporting etc etc - only a couple of 'undefined variable' notices, and then (obviously) errors complaining about attemting to do mysqli->num_rows on a 'non-object' ... which is exactly the problem.

 

the following code returns bool(false) when 'initially :(

 

$DatabaseQuery = $this->DatabaseLink->query($Query);

	var_dump($DatabaseQuery);

 

 

Link to comment
Share on other sites

Notice: Undefined variable: DatabaseSystem in D:\Websites\Clients\Playback\CoreModules\MySQLDatabaseController.php on line 50

 

Notice: Undefined variable: DatabaseHost in D:\Websites\Clients\Playback\CoreModules\MySQLDatabaseController.php on line 51

 

Notice: Undefined variable: DatabaseUser in D:\Websites\Clients\Playback\CoreModules\MySQLDatabaseController.php on line 52

 

Notice: Undefined variable: DatabasePassword in D:\Websites\Clients\Playback\CoreModules\MySQLDatabaseController.php on line 53

 

Notice: Undefined variable: DatabaseName in D:\Websites\Clients\Playback\CoreModules\MySQLDatabaseController.php on line 54

 

Notice: Undefined variable: PrefixTable in D:\Websites\Clients\Playback\CoreModules\MySQLDatabaseController.php on line 55

bool(false) <--var_dump output

Notice: Trying to get property of non-object in D:\Websites\Clients\Playback\CoreModules\MySQLDatabaseController.php on line 126

bool(false) <--var_dump output

Notice: Trying to get property of non-object in D:\Websites\Clients\Playback\CoreModules\MySQLDatabaseController.php on line 126

bool(false) <--var_dump output

Notice: Trying to get property of non-object in D:\Websites\Clients\Playback\CoreModules\MySQLDatabaseController.php on line 126

 

 

Line 126 in MySQLDatabaseController.php: $DatabaseQuery = $this->DatabaseLink->query($Query);

Link to comment
Share on other sites

GOT IT!  ;D

 

As stupid as it sounds, my initial thought was right, but i just didn't quite know how to execute the solution.

 

The problem: Globals. I needed to globalise $Database (the DatabaseController instance). Because of the way that I execute the code logically, every single controller is available inside of the generic function. So i global'd $Database and $Connection and referenced it again in the function and wham-bam-thank-you-ma'am it works. So simple!

 

Thorpe, thanks for your help mate :) just glad it's solved, this is a major issue for a MAJOR client.

 

The 'correct' code:

 

 

<?php
global $Connection;
global $Database;

function DisplayMessage($MessageArray, $Template = "Default") {

//$Database = new DatabaseController;
$File = new FileController;
$Database = $GLOBALS['Database'];

$File->FileName = "../../Generic/DisplayMessage_{$Template}Template.html";
$File->Open("read");

$DisplayTemplate = $File->Read();

$MessageString = "";

for ($i = 0; $i < count($MessageArray); $i++) {

          .....


        }

.....

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.