0

Having some issues with a piece of PHP code that I'm using to learn about it a little more.

I have the following situation:

conf.php file with database connection details

<?php
    $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = "";
    $dbname = "mydatabase";
?>

functions.php file where I (hope to) store some functions to reuse in different pages

<?php
function dbconnect() {
    global $dbhost;
    global $dbuser;
    global $dbpass;
    global $dbname;
    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    if($mysqli->connect_error) {
        exit('Error connecting to database');
    }
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $mysqli->set_charset("latin1_swedish_ci");
}
?>

complete.php file which receives some data through POST from another PHP page.

<?php
    require_once( 'conf.php' );
    require_once( 'functions.php' );
?>
// some HTML code
<?php
    $mydate = date("Y-m-d H:i:s");
    dbconnect();
    $stmt = $mysqli->prepare("INSERT INTO my_table (field1, field2, field3, field4, field5, field6, field7, field8, date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("sssssssss", $_POST['field1'], $_POST['field2'], $_POST['field3'], $_POST['field4'], $_POST['field5'], $_POST['field6'], $_POST['field7'], $_POST['field8'], $mydate);
    $stmt->execute();
    $stmt->close();
?>

If I send data through POST from my first page to complete.php I get the following errors:

Notice: Undefined variable: mysqli

Fatal error: Uncaught Error: Call to a member function prepare() on null

Error: Call to a member function prepare() on null

For the first one I THINK I can get rid of by setting

global $mysqli;

before dbconnect(); in complete.php. I understand that this is because the function is coming from a different file, and I have to set the variables as global. This is why I set $dbhost, $dbuser, $dbpass and $dbname as GLOBAL in conf.php. Without them being set as global the application could not find the variables. I initially though that maybe I'm not using require_once correctly, and maybe that I should use require, include or include_once. But none of them made any difference. Please, correct me if I'm wrong.

So after setting $mysqli as global the first error goes away, but I still get the other two errors.

If I replace dbconnect(); in complete.php with

$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    if($mysqli->connect_error) {
        exit('Error connecting to database');
    }
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $mysqli->set_charset("latin1_swedish_ci");

basically the same thing from functions.php minus setting the variables as global, it works just fine and the data is added into the database.

I dont know what Im doing wrong, maybe something with the require_once function as the code works fine when the connection is written directly into complete.php, or maybe Im not creating the function properly. Id like to add the prepared db insert statement as a separate function in functions.php as well, and just call it with add_data(); but at this point Im not even getting to the issues Ill face there. :)

proone
  • 3
  • 2
  • You need `global $mysqli;` in `dbconnect()`. – Barmar Jan 08 '20 at 22:29
  • @Barmar I initially added global $mysqli;to the end of dbconnect(), just before the closing }, and it didn't work. Since you saw the problem there, I tried adding it with the other variables set as global, and it worked great. Thanks for pointing me in the right direction. You`re awesome! – proone Jan 08 '20 at 22:43
  • I think it needs to be at the beginning, with all the other `global` statements. – Barmar Jan 08 '20 at 22:44
  • You also need it if you try to use `$mysqli` in any other functions. – Barmar Jan 08 '20 at 22:44

0 Answers0