0

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.

8
  • What does the code for index.php look like ? Can you also post the full code for your connection.php file? Possibly there are other parts of the code that might be causing the problem. Commented Mar 31, 2015 at 17:13
  • 1
    Please edit your question to post the code for index.php and connection.php and/or anything else that might be relevant. Commented Mar 31, 2015 at 17:14
  • Are you sure that a valid database connection is getting created in connection.php? Which is the file that is giving you error. Corresponding error line nos. maybe ? Commented Mar 31, 2015 at 17:21
  • yes @Maximus2012 The index.php says that I have problem in connection.php. But the connection.php wont give error by itself. I will load and show the data if i var dump in it. Commented Mar 31, 2015 at 17:25
  • what does var_dump($connection) in connection.php give you? Right after you create a connection. Commented Mar 31, 2015 at 17:26

1 Answer 1

4

You are executing the index.php which is located in C:\xampp\htdocs\cdcs\.

There you are executing require_once 'core/connection.php' which will include that scripts content into your index.php. Therefore any code within the file connection.php now runs from the same directory, index.php is located.

Therefore it cannot find config.php and fails to include, and therefore your variables are undefined. The script looks for C:\xampp\htdocs\cdcs\config.php rather than C:\xampp\htdocs\cdcs\core\config.php

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

Sidenode: You should use Constants instead of variables because variables are used for changing content, which your database credentials usually don't do during execution time.

Sign up to request clarification or add additional context in comments.

1 Comment

I think that seems to be the case here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.