Wordpress theme functions not working when called - php

I have a wordpress site and I want to save and do the database connection in my functions.php
So I can be calling it in my templates.
Here are the functions code in the in functions.php
<?php
function dbconnd() {
$server = "localhost";
$duser = "wpuser";
$dpass = "wppass";
$dbname = "wpdb";
$conn = new mysqli($server, $duser,$dpass, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
}
?>
So in my main.php if I call dbconnd(); , it seems it's not working as it throws a fatal error as in the case below:
Fatal error: Call to a member function query() on a non-object in
/main.php on line 49
<?php
dbconnd();
$sql = "SELECT * FROM orderform WHERE status='last';";
$result = $conn->query($sql); //this is line 49
?>
But in case below when am not calling the function, it works perfectly
<?php
$server = "localhost";
$duser = "wpuser";
$dpass = "wppass";
$dbname = "wpdb";
$conn = new mysqli($server, $duser,$dpass, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM orderform WHERE status='last';";
$result = $conn->query($sql);
?>

The way you are doing right now, $conn is a local variable only inside the function. So you can't get the value outside the function that way.
The best solution would be this:
function dbconnd() {
$server = "localhost";
$duser = "wpuser";
$dpass = "wppass";
$dbname = "wpdb";
$conn = new mysqli($server, $duser,$dpass, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else
return $conn;
}
And then when you're going to call the function, run this:
$conn = dbconnd();
Also don't forget to include or require the functions.php file inside the php file calling it.

Related

PHP connection and query error: Fatal error: Call to a member function query() on null in

I'm getting the above error in regards to my statement below, tried multiple fixes from other answers in stack but continue to get the same error :(
this is part of a php code in a user registration form, would like their info to insert in the table
if($conn->query( "INSERT INTO users (FIRST_NAME, LAST_NAME, USERNAME, PASSWORD)
VALUES ('$fname', '$lname', '$uname', '$pword')")==TRUE){
echo 'Inserted';
}else{
echo 'Not Inserted';
}
i have this other code in a separate file for the $conn
function dbConnect(){
$servername = "x";
$database = "activity2";
$username = "root";
$password = "root";
// Create connection
$conn = new
mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);
} //echo "Connection successful"; //make variable global to access in other
functions global $conn; return $conn;}
First thing to do is to create the class that will return a connection:
<?php
//filename: dbConnect.php
class dbConnect
{
public function connect() {
global $conn;
$servername = "localhost";
$username = "root";
$password = "";
$database = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} //echo "Connection successful"; //make variable global to access in other
return $conn;
}
}
?>
Now you can execute your sql command in another file:
<?php
require 'dbConnect.php';
$db = new dbConnect();
$conn = $db->connect();
$nome = "Anailton";
$email = "jose#hotmail.com";
if($conn->query( "INSERT INTO clientes (NOME, EMAIL) VALUES ('$nome', '$email')")==TRUE){
echo 'Inserted';
}else{
echo 'Not Inserted';
}
?>

How to display number of rows in a sql table using php?

I'm trying to get lines' number from the result of the sql query, so I started by connecting to the database, then checking if the connection was established and finally I tried to count the number of lines, here's the code :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$res = $conn->query('SELECT COUNT (id_offre) AS nb FROM offres');
$data = $res->fetch();
$nb = $data['nb'];
echo $nb;
?>
I get these errors :
Uncaught Error: Call to a member function fetch() on boolean
Call to a member function fetch() on boolean
I used echo to check the 'nb' value, what is the problem?
Your query fails which makes $conn->query() to return false.
So, why does the query fail?
SQL doesn't allow for any spaces between function names and the parentheses, like you have at COUNT ().
So if you change:
SELECT COUNT (id_offre) AS nb FROM offres
to
SELECT COUNT(id_offre) AS nb FROM offres
...it should work.
Note: I would highly recommend you to have a read about mysqli::error() and add some error handling to your code.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (! $conn) {
die("Connection failed: " . mysqli_connect_error());
}
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: ".$conn - > connect_error);
}
$res = $conn->query('SELECT * FROM offres');
$data_count = mysqli_num_rows($res);
echo($data_count);
?>
check this out!

PHP MySQL function dies

Ive written my function just to check if the database connection is working.
And it seems like he cant connect to my database, thats no problem, but he dies at the point where i run the function.
function testconnection() {
global $dbhost, $dbuser, $dbpassword, $dbname;
error_reporting(E_ERROR);
$conn = mysql_connect($dbhost, $dbuser, $dbpassword);
$dbconn = mysql_select_db($dbname);
if ( !$conn ) {
return "connfailed";
}
if ( !$dbcon ) {
return "dbconnfailed";
}
}
It stops any further building of the website.
All variables are defined. This function is just used to display an error message if it returns "dbconnfailed".
but even with echo testconnection(); it displays nothing.
can be seen here
but I host this at a big company and on localhost via xampp
it isnt working on one.com but it is working on xampp
You did not add your connection to $dbconn
$dbconn = mysql_select_db($dbname,$dbconn);
Pls Don't use the deprecated and insecure mysql_*-functions. They have been deprecated since PHP 5.5 (in 2013) and were completely removed in PHP 7. Use MySQLi or PDO instead
If you are using mysqli: Try this one..
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM usertable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
use mysqli library
function testconnection() {
global $dbhost, $dbuser, $dbpassword, $dbname;
error_reporting(E_ERROR);
$conn = mysqli_connect($dbhost, $dbuser, $dbpassword);
/* check connection */
if (mysqli_connect_errno()) {
return "connfailed " . mysqli_connect_error();
}
$dbconn = mysql_select_db($dbname);
if(!$dbcon) {
return "dbconnfailed " . mysqli_error($dbconn);
}
}

New mysqli () is reset to null when using a function

I am currently working with mySQL and php to create a web application.
In my main index.php file, I have
<?php
require_once('includes/config.php');
require_once('includes/functionName.php');
?>
My config.php is as below:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "DB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected Successfully!";
?>
When I call the actual function functionName("test");
it goes into the actual functionName.php and goes to
$sql = "UPDATE Table SET id = " . $test1 . " WHERE name = \"". $name . "\"";
echo $sql;
var_dump ($conn);
This returns UPDATE Table SET id = 09 WHERE name = "test" which is correct as an SQL query.
However, my $conn returns NULL, and as a result I get FATAL ERROR query on NULL.
Please tell me why my $conn is returning NULL, and failing my query?
Thank you it was solved after adding line global $conn

Why the require_once is not working in following scenario?

I've following code of a function in a PHP file titled sample.php :
function deleteValue($value) {
$servername = "localhost";
$username = "root";
$password = "jumbo";
$dbname = "demo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "DELETE FROM user WHERE value = '$value'";
if ($conn->query($sql) === TRUE) {
$conn->close();
return true;
} else {
$conn->close();
return false;
}
}
Actually, in this file(sample.php) there are too many such functions which repeats the following database connectivity code :
$servername = "localhost";
$username = "root";
$password = "jumbo";
$dbname = "demo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Then, I created one file titled db.php in the same folder and added the above code to it. Using require_once('db.php'); at the beginning of file 'sample.php'I included the file to the above code. Finally the code appeared as follows :
require_once('db.php');
function deleteValue($value) {
$sql = "DELETE FROM user WHERE value = '$value'";
if ($conn->query($sql) === TRUE) {
$conn->close();
return true;
} else {
$conn->close();
return false;
}
}
Now it's giving me 500 Internal Server error.
Even instead of including the file I tried by pasting the whole code from db.php at the beginning of file sample.php as follows but still I get 500 Internal Server Error.
$servername = "localhost";
$username = "root";
$password = "jumbo";
$dbname = "demo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
function deleteValue($value) {
$sql = "DELETE FROM user WHERE value = '$value'";
if ($conn->query($sql) === TRUE) {
$conn->close();
return true;
} else {
$conn->close();
return false;
}
}
Can someone please correct the mistake I'm making in my code?
Thanks in advance.
I will suggest you that in db_config.php file you should use only database config and functions like this:
$servername = "localhost";
$username = "root";
$password = "jumbo";
$dbname = "jumbo_jet";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_errno) {
echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
}
And in common.php use code like this:
require_once("db_config.php");
function deleteToken($receivedToken) {
global $conn;
$sql = "DELETE FROM user_login WHERE token = '$receivedToken'";
if ($conn->query($sql) === TRUE) {
return true;
}
return false;
}
I hope it will work for you. If anything else then let me know.

Categories