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>";
    }


?>

Recommended Answers

All 4 Replies

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.

Give like this... Its working...

include ("../includes/store.php");
Member Avatar for diafol

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.