Reputation: 85
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!
Upvotes: 2
Views: 671
Reputation: 8873
This line of your code,
$ab = $connect->query($a);
$ba = $connect->query($b);
must have an equivalent associative arrangement of
$ab => $ba,
and this line of code,
$cd = $connect->query($c);
$dc = $connect->query($d);
must have an equivalent associative arrangement of
$cd => $dc
and in your config.class.php, do like this
require 'data.php';
$admins = array(
$ab => $ba,
$cd => $dc
);
its just that the variables you are using in your associative doesn't match with the variable as a result from your query.
Upvotes: 1
Reputation: 2541
Are you sure that you aren't making any syntax error? Unexpected variable sometimes might mean that you are missing some bracket or semicolon.
Also, $ab = $connect->query($a);
returns a ResultSet, not a regular string
/int
. You cannot put objects as keys in arrays. You need to extract data from ResultSets. You can find more here (for instance): http://www.w3schools.com/php/php_mysql_select.asp
Upvotes: 0
Reputation: 286
Well, first of all, check if your $a, $b, $c, $d are filled in your data.php (by die(print_r($a));
or something similar).
If those are all filled with the right values, check whether you are including your data.php file before calling your array or after, because if it's after it won't work because the file hasn't loaded the $a, $b, $c, $d variables yet
Maybe the variables cant get passed to your class file because of some restrictions, then maybe consider putting them (encrypted though !!) in one of the global variables, (i.e. session / cookies). => THIS IS A DIRTY FIX ;) try option 1 and two FIRST :)
Upvotes: 0
Reputation: 21
You can include "data.php" in "config.class.php", but please make sure that there will be no variables in "config.class.php" with the same name as of "data.php".
Upvotes: 0