Hello DaniWeb,

I've finally gotten around to swapping from MySQL to MySQL(i), mainly due to it's security advantages with Prepared Statements.

In the past I've been adding include ('/Some/Directory/File.php); to the top of each page, so that I can store my functions in a secure directory and so that I only need to edit it once (such as if I change the password for the database).

When I attempted to do this in MySQL(i), I cannot get it to work. For example,

function Index_Visits()
    {
        $Connect = mysqli_connect('localhost', '***', '***', '***');

        $Query = "SELECT Index_Visits FROM SiteStatistics WHERE ID = 1";
        $Result = mysqli_query($Connect, $Query);

        $Row = mysqli_fetch_array($Result, MYSQLI_ASSOC);

        $CurVisits = $Row['Index_Visits'];

        echo $CurVisits;
    }

I use the above to display the total number of page visits to the Index Page, it is only part of it but it gives you an example.
When I go to call this, such as Index_Visits(); it shall work fine if it is in the same file, but if I store the function in a seperate .php file and include it and then try to execute it, it shall not work... no errors, no display, just a blank browser.

Member Avatar for LastMitch

@AHarrisGsy

but if I store the function in a seperate .php file and include it and then try to execute it, it shall not work... no errors, no display, just a blank browser.

Change this

$Query = "SELECT Index_Visits FROM SiteStatistics WHERE ID = 1";

to this:

$Query = "SELECT Index_Visits FROM SiteStatistics WHERE id=1";

Nope, nothing.

Thanks for the suggestion though, LastMitch.

As I say, it works fine when it is in a single file but when I try and include it from a seperate file it breaks.

Member Avatar for LastMitch

@AHarrisGsy

As I say, it works fine when it is in a single file but when I try and include it from a seperate file it breaks.

I realized what you did and you missed something:

Read this closely and you will realize the issue:

http://php.net/manual/en/mysqli-result.fetch-array.php

I will give you a hint:

it's an ->

Here is how it looks:

$row = $result->fetch_array(MYSQLI_ASSOC);

I realized what you did and you missed something:

Read this closely and you will realize the issue:

But if I am doing it Procedural Style?

I have also tried it OOP style, with no luck.

In my experience, a blank screen after making a change in PHP usually indicates some fatal error is happening in the PHP. You can try turning display_errors on and setting error_reporting to E_ALL.

Also be sure to check the path in your include statement to make sure it is correct.

commented: Nice Answer! +5

I copied your situation and tested the code on my own wamp server and it worked fine. I moved the include file above the document root and it still worked okay. This wamp server is so sensitive it throws an error if I look at the screen funny.

In my experience, a blank screen after making a change in PHP usually indicates some fatal error is happening in the PHP. You can try turning display_errors on and setting error_reporting to E_ALL.

Also be sure to check the path in your include statement to make sure it is correct.

Hey, thanks for the tip about ini_set. It showed up an error with the include path, although the path was correct it didn't like it unless I had:

$_SERVER['DOCUMENT_ROOT'] in it..

Strange, $_SERVER['DOCUMENT_ROOT'].'/PHP/Services/Index_Visits.php is exactly the same as /PHP/Services/Index_Visits.php in this instance as index.php is in the Document Root and PHP is equally in the Document Root.

Member Avatar for LastMitch

@AHarrisGsy

Hey, thanks for the tip about ini_set. It showed up an error with the include path, although the path was correct it didn't like it unless I had:

$_SERVER['DOCUMENT_ROOT'] in it..

Good to know!

Thanks for the help,

LastMitch
dcdruck
adam.adamski.96155

Hopefully now I can re-write my existing sites using the more secure MySQLi and Prepared Statements!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.