I have this working array in a php class file like config.class.php
var $admins = array("a" => "b", "c" => "d");
Now, for some reason I need to store the a, b, c, d in the SQL Database & call them here. To achieve that: I created a testing file data.php as below:-
<?php
$x = "localhost";
$y = "user";
$z = "pass";
$xyz = 'db-name';
// Create connection
$connect = mysqli_connect($x, $y, $z, $xyz);
// Check connection
if (!$connect) {
die("Connection Error: " . mysqli_connect_error());
}
$a = 'SELECT name FROM admin WHERE id = "aaaa";';
$b = 'SELECT pass FROM admin WHERE id = "aaaa";';
$ab = $connect->query($a);
$ba = $connect->query($b);
$c = 'SELECT name FROM admin WHERE id = "bbbb";';
$d = 'SELECT pass FROM admin WHERE id = "bbbb";';
$cd = $connect->query($c);
$dc = $connect->query($d);
?>
// id is a string not int in my case.
Now, in the config.class.php file there are some other files which required & as I am modifying the file so the existing syntax of calling other files is given as below above the start of the class :-
require_once dirname(__FILE__)."/file1.php";
require_once dirname(__FILE__)."/file2.class.php";
require_once dirname(__FILE__).'/file3.class.php';
I added the data.php (which is in the same folder where the config.class.php file is) file in it first as:-
require_once dirname(__FILE__)."/data.php";
require_once dirname(__FILE__)."/file1.php";
require_once dirname(__FILE__)."/file2.class.php";
require_once dirname(__FILE__).'/file3.class.php';
and changed Associative Array to like this:-
var $admins = array($ab => $bc, $cd => $dc);
But it is not recognizing these variable. I tried to store them in another variables within config.class.php and then, used those variables in array but still on execution:-
Unexpected variable $ab ..and so on are coming as error. Please, guide me where I am doing wrong and what to do. Thank you!
$abis simply a global variable … those don’t make their way into a class magically all by themselves. You should write a proper constructor for your config class, and pass those variables into that constructor as parameters, so that the constructor can then set them in the array.