SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    BYO Database Driven Website - my_sqli connect not working

    Please help if you can - stumped!

    Got to the stage (p.117) of connecting to MySQL with PHP.

    Accessing the php file on localhost which contains:

    Code:
    <?php
    $link = mysqli_connect('localhost', 'root', 'password');
    if (!$link)
    {
    	$output = 'Unable to connect to the database server.';
    	include 'output.html.php';
    	exit();
    }
    
    if (!mysqli_set_charset($link, 'utf8'))
    {
    	$output = 'Unable to set database connection encoding.';
    	include 'output.html.php';
    	exit();
    }
    
    if (!mysqli_select_db($link, 'ijdb'))
    {
    	$output = 'Unable to locate the joke database.';
    	include 'output.html.php';
    	exit();
    }
    
    $output = 'Database connection established.';
    include 'output.html.php';
    ?>
    i.e. http://localhost/connect/index.php or http://localhost/connect

    produces a 500 server error.

    This seems odd, as I have successfuly accessed and run other php filesin previous exercises in the book. The only difference with this one is that it references mysqli.

    I can successfully connect to mysql via the command prompt, so that appears to working.

    Thanks!

  2. #2
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm having this same issue, although I am not getting the 500 server error.

    I am given the $output value of !$link ('Unable to connect to the database server.') if I try to access localhost/connect/. Strangely enough, when I remove the 'root' parameter I am given the $output value of !mysqli_select_db($link, 'ijdb')...'Unable to locate the joke database.'

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    Wenatchee, WA
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems like chapter 4 of this book gives lots of folks problems. I'm guessing robertmarks probably found whatever his problem was - probably *not* the database if he was getting 500 errors from Apache.

    nick, it sounds like your login credentials for your database (in index.php) are not correct, which is why you were getting the 'cannot connect' error message. The 'root' parameter you mention is the user account for accessing the database. If you 'removed' that, then I'd guess values are getting substituted to where mysqli_connect is getting some weird (and incorrect) string for the database name, and as a result 'cannot locate' it.


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
  •