SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Help with connecting to sql?

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with connecting to sql?

    Hi I'm a bit of a noob with PHP and SQL. I've bought Kevin Yank's "Build your own database driven website using PHP & MYSQL" and I seem to have hit a brick wall.

    I'm running apache and mysql on my computer. With running simple php files on the local host I've had no problems. However when I try to connect to my database in chapter 4 (the bit that displays jokes in the database) none of the PHP code seems to work.

    At first I presumed I must have some typos, so I downloaded the archive for the book and used that file. I've changed the appropriate settings in this file (the password) yet I still get nothing.

    The thing is, it's supposed to display an error message if it doesn't connect, I just get a blank page and nothing (php OR HTML) runs AFTER the first bit of PHP. I don't even get the error messages.

    This is my code:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Our List of Jokes</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <?php
    
    // Connect to the database server
    $dbcnx = @mysql_connect('localhost', 'root', 'password');
    if (!$dbcnx) {
      exit('<p>Unable to connect to the ' .
          'database server at this time.</p>');
    }
    
    // Select the jokes database
    if (!@mysql_select_db('ijdb')) {
      exit('<p>Unable to locate the joke ' .
          'database at this time.</p>');
    }
    
    ?>
    <p>Here are all the jokes in our database:</p>
    <blockquote>
    <?php
      
    // Request the text of all the jokes
    $result = @mysql_query('SELECT joketext FROM joke');
    if (!$result) {
      exit('<p>Error performing query: ' . mysql_error() . '</p>');
    }
    
    // Display the text of each joke in a paragraph
    while ($row = mysql_fetch_array($result)) {
      echo '<p>' . $row['joketext'] . '</p>';
    }
    
    ?>
    </blockquote>
    </body>
    </html>
    NOTE: 'password' is the password

  2. #2
    SitePoint Zealot ninjayong's Avatar
    Join Date
    May 2006
    Location
    Holland
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What happens when you put this bit of code in:

    // Connect to the database server
    $dbcnx = @mysql_connect('localhost', 'root', 'password');
    if ($dbcnx) {
    echo 'successful connection to database';
    }

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    India
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would suggest that you first try out a very simple php code and see. For instance...

    Code:
    <?php
    echo "Hello world";
    ?>

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Celia,

    The simple php code works completely fine. This just confuses me more.

    Ninja,

    With that suggestion I still get the same result. A completely blank screen with the title as listed but nothing else. Not even the text tags outside of the php.

  5. #5
    SitePoint Member
    Join Date
    Mar 2006
    Location
    North East England
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you have the mysql php module installed? I had a similar problem with postgresql in Windows and it was because the module for php was missing and php.ini needed some tweaking (to actually enable the module at start up of IIS and loading of php).

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Change your MySQL password

    First of all be a little more creative with the password. Something some fruitcake hacker or chimp cannot figure out. The password is pretty generic. Second change the password from the MySQL command prompt. If you can access from there you almost certainly will be able to access it from script within a PHP file. Check for typos in your PHP file thoroughly.

    DAD

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    htmldad: that's not actually my real password, and this is only an offline server for learning purposes. But thanks for the concern.

    ryan: I think that's most likely to be my problem here, I'll have a tweak around with the settings and see if I missed something.

    Thanks guys

  8. #8
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,485
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Turn on PHP error reporting, this should give you a clue as to where your script is failing.

    Did you cut and paste your code in your first post ??
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •