So I have a config.php file where I want to have login credentials.
config.php
<?php
$server = "localhost";
$username = "root";
$password = "admin123";
$database = "web_apps";
$table = "agencies";
?>
And I have a connection.php file in which the config.php file is included.
connection.php
<?php
require_once('config.php');
$connection = new mysqli($server, $username, $password, $database);
if(mysqli_connect_errno()){
echo "Connection could not be established";
exit();
}
$url_id = isset($_GET["id"])?$_GET["id"]:NULL;
$query = 'SELECT * FROM '.$table;
$stmt = $connection->prepare($query);
$stmt->execute();
$result = $stmt->get_result();
$all_data =array();
$specific = array();
$name_list = array();
$fieldname = array();
$datatype = array();
$searchable = array();
$search_keys = array();
$name_keys = array();
$i = 0;
$m = 0;
while ($row = $result->fetch_array(MYSQLI_NUM))
{
if($i == 0){
$fieldname = $row;
}
else if($i == 1){
$datatype = $row;
}
else{
$all_data[$row[0]]= $row;
$name_keys[$m++] = $row[0];
}
$i++;
}
foreach ($all_data as $each_service){
if(!strcmp($url_id, $each_service[0])){
$specific = $each_service;
break;
}
}
for($i = 0; $i < count($datatype); $i++){
if(strpos($datatype[$i], "search_")!== FALSE){
$searchable[$i] = $datatype[$i];
$search_keys[$i] = $i;
}
}
$name_index = 0;
foreach ($datatype as $key) {
if(strpos($key,"name") !== false){
break;
}
$name_index++;
}
foreach ($all_data as $key=>$value) {
$name_list[$key] = $value[$name_index];
}
//echo $name_index;
//print_r($names);
//var_dump($all_data);
//print_r($name_list);
//print_r($specific);
//print_r($fieldname);
//print_r($datatype);
//print_r($searchable);
//print_r($search_keys);
//print_r($name_keys);
$connection->close();
?>
The connection.php file is supposed to use the configuration variables in config.php and establish a connection to the database and throw out the data to other files. Both of these files are on the same directory. I have another file index.php which requires_once connection.php and displays the data in the application.
index.php
<?php require_once("core/connection.php"); ?>
<?php require_once("core/header.php"); ?>
<div data-role="page" data-theme="a">
<div data-role="main" class="ui-content main-content">
<?php require_once('core/topbar.php'); ?>
<select id="searchby">
<option value="" selected disabled>Search by ... </option>
<?php
foreach($search_keys as $key){
echo "<option value=".$key.">".$fieldname[$key]."</option>";
}
?>
</select>
<form class="ui-filterable">
<input id="autocomplete-input" data-type="search" placeholder=<?php echo '"Search by '.$fieldname[$search_category].'"'; ?>>
</form>
<ul data-role="listview" data-filter="true" data-filter-reveal="true" data-input="#autocomplete-input" data-inset="true">
<?php
if($search_category!=NULL){
$i = 0;
foreach ($all_data as $names){
echo "<li id='".$name_keys[$i]."'><a href='info.php?id=".$name_keys[$i++]."'>".$names[$search_category]." Name = ".$names[$name_index]."</a></li>";
}
}
?>
</ul>
</div>
<div style="text-align:center">
<img width="90%" src="img/cdcs-home.png" />
</div>
<?php require_once("core/footer.php"); ?>
problem
The connection.php will show var_dump of data that is being pulled from the database. But the index.php wont't.
Whenever I open the index.php, it says :
Notice: Undefined variable: server in C:\xampp\htdocs\cdcs\core\connection.php on line 4
Notice: Undefined variable: username in C:\xampp\htdocs\cdcs\core\connection.php on line 4
Notice: Undefined variable: password in C:\xampp\htdocs\cdcs\core\connection.php on line 4
Notice: Undefined variable: database in C:\xampp\htdocs\cdcs\core\connection.php on line 4
Notice: Undefined variable: table in C:\xampp\htdocs\cdcs\core\connection.php on line 10
Fatal error: Call to a member function execute() on boolean in C:\xampp\htdocs\cdcs\core\connection.php on line 12
If I declare and initialize all the config variables inside connection.php, index.php shows the data in the application.
I want to have 3 files as it is and still show the information index.php from the database configured in config.php via connection.php
Any help would be appreciated. Thanks.