I am having issues making a new system for my website. I am working on a new connect.php and approved.php (for something else) and I cannot seem to get it to work. Here is the approved.php
approved.php
<?php
include_once "connect.php";
include_once "strings.php";
connect($conn[0][0], $conn[1][2], $conn[2][2], $conn[3][0]);
//echo $newConn;
mysqli_query($newConn, "SELECT * FROM `loginsystem`") or die("Could not connect");
?>
Here is the connect.php
<?php
function connect($servername, $username, $password, $dbname)
{
include "strings.php";
if ($servername == $conn[0][0] && $username == $conn[1][0] && $password == $conn[2][0] && $dbname == $conn[3][0] ||
$servername == $conn[0][0] && $username == $conn[1][1] && $password == $conn[2][1] && $dbname == $conn[3][0] ||
$servername == $conn[0][0] && $username == $conn[1][2] && $password == $conn[2][2] && $dbname == $conn[3][0])
{
$newConn;
if ($newConn = mysqli_connect($conn[0][0], $conn[1][0], $conn[2][0], $conn[3][0])
|| $newConn = mysqli_connect($conn[0][0], $conn[1][1], $conn[2][1], $conn[3][0])
|| $newConn = mysqli_connect($conn[0][0], $conn[1][2], $conn[2][2], $conn[3][0]))
{
echo $processMsg;
}
else
{
echo $errorMsg;
die();
}
return $newConn;
}
else if ($servername == $conn[0][0] && $username == $conn[1][0] && $password == $conn[2][0] && $dbname == $conn[3][1] ||
$servername == $conn[0][0] && $username == $conn[1][1] && $password == $conn[2][1] && $dbname == $conn[3][1] ||
$servername == $conn[0][0] && $username == $conn[1][2] && $password == $conn[2][2] && $dbname == $conn[3][1])
{
if ($newConn = mysqli_connect($conn[0][0], $conn[1][0], $conn[2][0], $conn[3][1])
|| $newConn = mysqli_connect($conn[0][0], $conn[1][1], $conn[2][1], $conn[3][1])
|| $newConn = mysqli_connect($conn[0][0], $conn[1][2], $conn[2][2], $conn[3][1]))
{
echo $processMsg;
}
else
{
echo $errorMsg;
die();
}
return $newConn;
}
else
{
die("Something went wrong with our systems");
}
}
?>
And finally strings.php With sensitive information taken out
<?php
//Information taken out for stack overflow question - not everything is here
include_once("connect.php");
$tablesChromeAds = ["food", "software", "hardware", "home", "outdoor", "indooract", "services", "other"];
$conn = array(array($server) /*both our servers go under localhost*/, array($admin, $admin1, $localAdmin), array($adminPass, $admin1Pass, $localAdminPass), array($dbGeneralDB, $dbBrowserExtensions));
$newConn;
$errorMsg = "<script type='text/javascript'>Notification.requestPermission().then(function(result) {console.log(result);});var notification = new Notification('Smart Lead Advertisement Error', {icon: '$logo',body: 'Cannot connect to database'});</script>";
$processMsg = "<script type='text/javascript'>Notification.requestPermission().then(function(e) {console.log(e);});var notification = new Notification('Smart Lead Advertisement', {icon: '$logo',body: 'Processing request...'});</script>";
//Get the value for the presale license's from the mysql database to be stored within a variable which can be later accessed from any file
$num = 50;
?>
Now everything as far I know is included correctly. No errors happen until I try to connect. I havent had this problem before. The errors are as shown below
Notice: Undefined variable: newConn in C:\xampp\htdocs\smartlead\php\approved.php on line 6
Notice: Undefined variable: newConn in C:\xampp\htdocs\smartlead\php\approved.php on line 8
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\smartlead\php\approved.php on line 8
So I know one thing that the newConn variable is not being returned properly. But why? And how can I fix this?
As commented by u_mulder
$newConn = connect($conn[0][0], $conn[1][2], $conn[2][2], $conn[3][0]);
//echo $newConn;
mysqli_query($newConn, "SELECT * FROM `loginsystem`") or die("Could not connect");
It seams that your connection
is not established try to use echo mysqli_connect_error();
function
connect.php
<?php
function connect($servername, $username, $password, $dbname) {
include "strings.php";
if ($servername == $conn[0][0] && $username == $conn[1][0] && $password == $conn[2][0] && $dbname == $conn[3][0] ||
$servername == $conn[0][0] && $username == $conn[1][1] && $password == $conn[2][1] && $dbname == $conn[3][0] ||
$servername == $conn[0][0] && $username == $conn[1][2] && $password == $conn[2][2] && $dbname == $conn[3][0]) {
$newConn;
if ($newConn = mysqli_connect($conn[0][0], $conn[1][0], $conn[2][0], $conn[3][0]) || $newConn = mysqli_connect($conn[0][0], $conn[1][1], $conn[2][1], $conn[3][0]) || $newConn = mysqli_connect($conn[0][0], $conn[1][2], $conn[2][2], $conn[3][0])) {
echo $processMsg;
} else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
echo $errorMsg;
die();
}
return $newConn;
} else if ($servername == $conn[0][0] && $username == $conn[1][0] && $password == $conn[2][0] && $dbname == $conn[3][1] ||
$servername == $conn[0][0] && $username == $conn[1][1] && $password == $conn[2][1] && $dbname == $conn[3][1] ||
$servername == $conn[0][0] && $username == $conn[1][2] && $password == $conn[2][2] && $dbname == $conn[3][1]) {
if ($newConn = mysqli_connect($conn[0][0], $conn[1][0], $conn[2][0], $conn[3][1]) || $newConn = mysqli_connect($conn[0][0], $conn[1][1], $conn[2][1], $conn[3][1]) || $newConn = mysqli_connect($conn[0][0], $conn[1][2], $conn[2][2], $conn[3][1])) {
echo $processMsg;
} else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
echo $errorMsg;
die();
}
return $newConn;
} else {
die("Something went wrong with our systems");
}
}
?>
Here's an example of how the assignment from a function works in PHP.
function test() {
$moose = 'Wow';
return $moose;
}
test();
echo 'empty->' . $moose . "<-empty"; //undefined;
$moose = test();
echo $moose;
Note on the first function call test()
we do nothing with the returned value so it is just lost. All variables are relative to their function as well. You could call the variable $cattle
in the function then $moose
in the script and all $moose
references would be fine.
Demo: https://eval.in/611062