0

I am building a website that connects to a mysql database. When I include the file that holds the connection to the database I get an error message. If I write the code in each page the connection works fine and displays records held in database. Here is my code;

this code returns an error

<?php
    /** read from the database and load to tables on the web parts page */
    include ("includes/store.php");


    $cars = mysql_query("SELECT * FROM cars");
    while($row = mysql_fetch_array($cars))
    {
        $make = $row['make'];
        echo $make. "<br>";
    }


?>

<?php
    include("includes/footer.php");
?>

the error message is:

Warning: include(includes/store.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\nicks\forms\cars.php on line 3

Warning: include() [function.include]: Failed opening 'includes/store.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\nicks\forms\cars.php on line 3

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\nicks\forms\cars.php on line 8

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\nicks\forms\cars.php on line 8

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\nicks\forms\cars.php on line 9:

But when i code like this it works fine and has no errors?

<?php
    /** read from the database and load to tables on the web parts page */
    $access = mysql_connect("localhost", "root", "woot");
                if (!$access)
                {
                    die ("failed to connect to database" . mysql_error($access));
                }

                $select = mysql_select_db("ngash", $access);
                if(!$select)
                {
                    die ("failed to connect to database" . mysql_error($select));
                }

    ?>

    <?php
    $cars = mysql_query("SELECT * FROM cars");
    while($row = mysql_fetch_array($cars))
    {
        $make = $row['make'];
        echo $make. "<br>";
    }


?>

Edited by mike_2000_17: Fixed formatting

4
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by komanche
0

Have set the correct path to the include i.e.

<?php
include '../includes/store.php';
?>

OR

<?php
include '../../includes/store.php';
?>

As the directory path to the includes could be a folder or two up.

0

You are not referencing the include file properly as mentioned. This can be baffling if your are including a file which itself includes files.

Just for testing purposes, you can try this:

include($_SERVER['DOCUMENT_ROOT'] . "/path-to-include/store.php");

THis *should* work.
After this, you should go at it to resolve the reference prob - it won't be too difficult.

0

Thanks alot guys it work it was the path after hours of toil now I can get to the combo box page.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.