php - DB connection not always working -


i have problem connection works things not others. here code within sqlconn.php file:

//db connection details $servername = "localhost"; $username = ""; $password = ""; $dbname = ""; //create connection $conn = new mysqli($servername, $username, $password, $dbname);  // check connection if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error); }  

the error getting is: "fatal error: call member function query() on non-object"

the problem code is:

include ('sqlconn.php'); $entryid = htmlspecialchars($_post["entryid"]); //check if post variables empty if (empty ($entryid)) {     //kill process if     die(header("location:http://www..com/money/?deletefailed=true&reason=blank"));}  //check if strings contain numbers //if (!is_numeric($bookingid)){     //die(header("location:http://www..com/money/?deletefailed=true&reason=number"));}  //function strip input data of bad inputs function clean_input($data) {     return preg_replace('/[^a-za-z0-9\-]/', '', $data); // removes special chars. }  //delete table $sql = "delete moneys         entryid ='$entryid'";  //return error message if sql fails if ($conn->query($sql) === false) {    header("location: http://.com/money/?sqlfailed=true"); } else {     //return page     $conn->close();     exit(header("location:http://www..com/money")); } 

specifically: $conn->query($sql)

i understand not recognizing $conn variable object confusing part exact code works in other parts of site, when adding or removing things db, if returning rows $conn variable seems work fine.

the db connection code used within every php file, removed , created include file grab connection details, thing have changed, of used work perfectly.

neil: "fatal error: call member function query() on non-object"

this problem means, connection not instantiated properly.

your error checking code incorrect:

//this won't work if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error); }  

use this:

if (!$conn) {    die('error connecting mysql. error code: '. mysqli_connect_error()); } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -