Let me apologize in advance. I've been working on this project all day and I'm tired and probably just need sleep but I can't seem to connect to a mysql database from a function.
I have a database file which get's called through require_once()
and have a function that is being called but it doesn't seem to want to connect.
Here's the connect code:
// Create connection
$con = mysqli_connect("$database_serverURL","$database_username","$database_password","$database_dbName");
// Check connection
if (mysqli_connect_errno($con))
{
$database_error = true;
$database_errorInfo = mysqli_connect_errno($con);
}
$database_error = false;
Here's my function:
//Select from database
function selectFromDatabase($table_name){
//Declare array
$return_array = array();
//Make query
$result = mysqli_query($con,"SELECT * FROM $table_name");
while($row = mysqli_fetch_array($result)){
$return_array = $row[];
}
writeJson($return_array);
//close database connection
mysqli_close($con);
}
It says that the first parameter is null and it's supposed to be the database connect information. I know I can make a connection to the database and pull info from the table but when it's inside the function it doesn't want to work.
I tried making the connect data global but that didn't work and i have a feeling that's a bad idea.
I feel like i should learn more about class systems and functions and stuff but I just want this to work.
Sorry if it's right in front of me. I even tried putting the connection code inside the function but still squat.
Again sorry for the stupid question. Especially due to the fact that it's about connecting to a database :P
Edit:
This was my first attempt on creating an Object Oriented script. Here was my final solution.
I had a config script called class_config.php
:
<?php
class declarations{
//Define variables
private $databaseArray;
//Populate variables
public function populateVariables(){
$this->databaseArray = array('databaseServer', 'databaseUsername', 'databasePassword', 'databaseName');
}
//Return variable requested
public function returnVariable($variable){
$this->populateVariables();
return $this->$variable;
}
}
?>
Database class (class_database.php):
<?php
//Include config class
require_once('class_config.php');
function createInstance(){
//Create class instance
$obj = new declarations;
//Return class instance
return $obj;
}
function connectDB(){
//Create class instance
$obj = createInstance();
//Get database array from 'declarations' class
$databaseArray = $obj->getVariable('databaseArray');
//Create strings from array values
list ($serverURL, $username, $password, $databaseName) = $databaseArray;
//Create database connection
$con = mysqli_connect($serverURL, $username, $password, $databaseName);
//Return connection
return $con;
}
?>
Anytime I need a database connection, I just call the database connect function. I would create an instance by using $con = connectDB(); and just use $con in my query. In my projects I usually have one function that executes all queries and returns the result:
function executeQuery($payload){
//Create database connection
$con = connectDB();
//Execute database query
$result = mysqli_query($payload);
//Return result
return $result;
}