mysql - Using variables included in another PHP file -


so have config.php file want have login credentials.

config.php

<?php   $server = "localhost";   $username = "root";   $password = "admin123";   $database = "web_apps";   $table = "agencies"; ?> 

and have connection.php file in config.php file included.

connection.php

<?php require_once('config.php'); $connection = new mysqli($server, $username, $password, $database); if(mysqli_connect_errno()){     echo "connection not established";     exit(); } $url_id = isset($_get["id"])?$_get["id"]:null; $query = 'select * '.$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 $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 $key) {     if(strpos($key,"name") !== false){         break;     }     $name_index++; }  foreach ($all_data $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 supposed use configuration variables in config.php , establish connection database , throw out data other files. both of these files on same directory. have file index.php requires_once connection.php , displays data in 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 ... </option>                 <?php                 foreach($search_keys $key){                       echo "<option value=".$key.">".$fieldname[$key]."</option>";                                       }                 ?>             </select>             <form class="ui-filterable">                 <input id="autocomplete-input" data-type="search" placeholder=<?php echo '"search '.$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 $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 show var_dump of data being pulled database. index.php wont't. whenever open index.php, 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 member function execute() on boolean in c:\xampp\htdocs\cdcs\core\connection.php on line 12 

if declare , initialize config variables inside connection.php, index.php shows data in application.

i want have 3 files , still show information index.php database configured in config.php via connection.php

any appreciated. thanks.

you executing index.php located in c:\xampp\htdocs\cdcs\.

there executing require_once 'core/connection.php' include scripts content index.php. therefore code within file connection.php runs same directory, index.php located.

therefore cannot find config.php , fails include, , therefore variables undefined. script looks c:\xampp\htdocs\cdcs\config.php rather c:\xampp\htdocs\cdcs\core\config.php

to fix this, use require_once 'core/config.php' inside connection.php or setup proper usage of include-dir within php.ini.

sidenode: should use constants instead of variables because variables used changing content, database credentials don't during execution time.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -