Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Wont Connect To DB in MYSQL

Options
  • 04-02-2010 10:12pm
    #1
    Registered Users Posts: 229 ✭✭


    Hey all,

    Just starting out with PHP and MYSQL.

    Ive created a simple database in MYSQL named nate_db and have:

    Created a connection
    Selected a Database (nate_db)
    Performed a Query
    Returned Data

    and

    Closed Connection

    Yet when I load up the page with my local host server turned on - I get a blank page!

    Whats wrong?

    Heres the code...

    Cheers
    <?php
    // 1. CREATE DATABASE CONNECTION
    $connection = mysql_connect("localhost", "root", "MY_PASSOWORD_HERE");
    if (!$connection){
    die("CONNECTION FAILED!!!!!" . mysql_error());
    }


    // 2. SELECT A DATABASE TO USE

    $db_select = mysql_select_db("nate_db", $connection);
    if (!$db_select){
    die("COULD NOT CONNECT TO DATABASE" . mysql_error());
    }
    ?>
    <html>
    <head>
    <title>OUR BUSINESS WEBSITE</title>
    </head>
    <body>
    <?php

    // 3. PERFORM DATABASE QUERY

    $result = mysql_query("SELECT * FROM subjects", $connection);
    if (!$result){
    die("QUERY ERROR USER!!!!" . mysql_error());
    }


    // 4. USE RETURNED DATA

    while ($row = mysql_fetch_array($result)){
    echo $row[1]. " " .$row[2];
    }
    ?>
    </body>

    </html>
    <?php

    // 5. CLOSE CONNECTION

    mysql_close($connection);

    ?>


Comments

  • Closed Accounts Posts: 112 ✭✭lostprophetsie


    Try add this to see if you can get an error message.
    [PHP]<?php ini_set('display_errors', 1); ?>[/PHP]


  • Registered Users Posts: 498 ✭✭bobbytables


    Try add this to see if you can get an error message.
    [PHP]<?php ini_set('display_errors', 1); ?>[/PHP]
    +1, you are not seeing the error messages, which are most likely due to typos or permissions.


  • Registered Users Posts: 229 ✭✭silverwex


    Thanks all.

    Was weird, pasted that in refreshed it and its working!

    Then, to try things out, I took out that code you gave me and its still working. Weird! :D


  • Closed Accounts Posts: 112 ✭✭lostprophetsie


    The code I gave only turns on error messages so you would have seen where the problem was for example. As bobbytales said, it can be down to typos or permissons and by including
    [PHP]<?php ini_set('display_errors', 1); ?> [/PHP]
    you can get a better idea where the problem lies.
    Removing it from the page is fine but if you run into any trouble again you can just paste it back in to get an error message on the page instead of the blank page you were experiencing.


Advertisement