including a custom php file in index.template but db connection not working

Started by rcane, September 26, 2024, 02:01:28 PM

Previous topic - Next topic

rcane

I have a custom php that includes a db.php file of mine.

it's a simple query to put some text in a <div> on the page above the menu. 

if i test the function output in a test.php (which includes the db.php file there), it works fine.

But, i include the file at the top of index.template.php and then call the function later in the page my error checking always says the db connection is not established. 

it includes the file fine as echo statements attest to that.  but, it doesn't transfer the db connection from db.php to test.php to index.template.php

I know you shouldn't put db connections in index.template.php so I'm stumped as to why the include isn't bringing that along.   


Does index.template.php somehow block included connections?

fyi: my function in test.php that has the connection is a global variable.


Doug Heffernan

What is the code that you are using? The more details you give us the better. You should know that by now :D

rcane

1. my test.php has

include /full-path-to/db.php
2. db.php has a valid, tested, db connection

3. test.php has a function that just grabs some text from a table


if ($conn->connect_error) {
    die("conn Connection ****** itself: " . $conn->connect_error);

}


function custom() {
    global $conn; // Declare the gl  echo 'the function is starting';
    if (!$conn) {
        return 'Database connection not established.'; // Check connection
    }

    // Execute the query
    $getnews = $connsibi->query("SELECT * FROM customnews");
   
    // Check for query errors
    if (!$getnews) {
        return 'Query failed: ' . $conn->error; // Return query error
    }

    $news = [];
    while ($rows = $getnews->fetch_object()) {
        $news[] = $rows;
    }

    // Prepare the output
    $output = '<div style="width: 70%; margin: auto;">'; // Inline style for div
    foreach ($news as $n) {
        $output .= 'The news is: ' . parse_bbc($n->news) . '<br>'; // Assuming parse_bbc() is defined
    }
    $output .= '</div>';

    return $output; // Return the constructed output
}



4. In index.template.php I included the full path to test.php.  I've tried it at the top of the page as well as moving it down to immediately before calling the function in question. 

5. Then down a ways in the section where you can insert custom banners and shoutboxes I put

echo custom()

echo custom will output any text phrases and such, but the first error message in the function "database connection not established" is where it crashes.   So, it's including the file, but it doesn't like the db connection. 

db.php doesn't have any includes to SSI, but my test.php does as I had to limit it's viewing to just my account.


Kindred

because, within SMF, db connections are closed (I believe)

you SHOULD be using the SMF database functions for all database related calls, since SMF has hardened the db functions -- and making your own calls potentially opens security holes.  Also, SMF plays nicely with its own calls.:)
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

shawnb61

First thought is to ensure $conn is declared global everywhere it is set or used.  In the example above, of course it will fail as shown because $conn wasn't declared global.

It's very hard diagnosing this kind of issue without seeing all the code.   It's like trying to understand what's going on in the next room looking through a keyhole...

Given the frequency of such questions, I suggest running a local copy (e.g., on wampserver) and using a step debugger (like Eclipse).

That takes all the guesswork out.

As pointed out above, using $smcFunc is preferred, due to security & safety built in.  (Of course, that assumes you are calling it correctly, with vars parameterized & typed properly...) 

Note you can print_r() your $conn object for more info on the validity of your connection.

Note also there are calls available to show the last error, mysqli_error() & mysqli_connect_error().

A question worth asking is born in experience & driven by necessity. - Fripp

rcane


shawnb61

Not that I'm aware of.

Pretty straightforward if you read a few examples.

When I started I'd find similar queries & copy & modify.

It's mainly a query with placeholders.  The placeholders specify a datatype & name.   After the query, you pass an array which associates each placeholder name with a value or php variable.

Read a few!
A question worth asking is born in experience & driven by necessity. - Fripp


Advertisement: