www.webdeveloper.com
Results 1 to 12 of 12

Thread: Cant connect to database on localhost

  1. #1
    Join Date
    Mar 2012
    Location
    Battle Ground, Washington
    Posts
    30

    Cant connect to database on localhost

    I am trying to so a simple CRUD tutorial from this page.
    http://www.codeofaninja.com/2012/01/...-database.html
    I created in PHPMyAdmin the database and table structure but i get a database not found error.

    Since the default "localhost" parameter did not connect, I changed that parameter to "localhost:8080". Now when I hit load the page just hangs and eventually times out. I am assuming that is the wrong $host?
    My URL in Firefox for WAMP is http://localhost:8080/NINJA/

    Code:
    <?php
    //set connection variables
    $host = "localhost:8080";
    $username = "root";
    $password = "";
    $db_name = "codeofaninja"; //database name
    
    //connect to mysql server
    $mysqli = new mysqli($host, $username, $password, $db_name);
    
    //check if any connection error was encountered
    if(mysqli_connect_errno()) {
        echo "Error: Could not connect to database.";
        exit;
    }
    ?>

  2. #2
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    Don't you need to use mysqli_connect($host, $username, $password, $db_name);

  3. #3
    Join Date
    Mar 2012
    Location
    Battle Ground, Washington
    Posts
    30
    I have never done this before. I am trying to explicitly follow that tutorial. Seems like I create the database and tables then it says I cannot connect to it.

    How would you re-write my db_connect.php file?

  4. #4
    Join Date
    Mar 2012
    Location
    Battle Ground, Washington
    Posts
    30
    I altered this line:
    Code:
    //connect to mysql server
    $mysqli = new mysqli($host, $username, $password, $db_name);
    And changed it to:

    //connect to mysql server
    $mysqli = new mysqli_connect($host, $username, $password, $db_name);

    I get the error:
    Fatal error: Class 'mysqli_connect' not found in C:\wamp\www\NINJA\db_connect.php on line 9

  5. #5
    Join Date
    Jan 2009
    Posts
    3,346
    The OP is using the OOP version of mysqli rather than the antiquated mysql_* procedural functions.

    $host = "localhost:8080";
    Should probably be
    PHP Code:
    $host "127.0.0.1"
    or
    PHP Code:
    $host "localhost"
    The database server doesn't run on the same port as the web server. Also check to make sure you have started the database server.

  6. #6
    Join Date
    Mar 2012
    Location
    Battle Ground, Washington
    Posts
    30
    1) I have properly added this database and table structure to PHPMyAdmin
    2) I have restarted all WAMP services

    Here is my DB_CONNECT CODE
    Code:
    <?php
    //set connection variables
    $host = "localhost";
    $username = "brian";
    $password = "300game";
    $db_name = "codeofaninja"; //database name
    
    //connect to mysql server
    $mysqli = new mysqli_connect($host, $username, $password, $db_name);
    
    //check if any connection error was encountered
    if(mysqli_connect_errno()) {
        echo "Error: Could not connect to database.";
        exit;
    }
    ?>
    when I try to run it I get this error:
    http://i1101.photobucket.com/albums/...it/error-1.jpg

  7. #7
    Join Date
    Jan 2009
    Posts
    3,346
    I cannot visit that domain from work. Can you copy and paste the error message?

  8. #8
    Join Date
    Mar 2012
    Location
    Battle Ground, Washington
    Posts
    30
    ( ! ) Fatal error: Class 'mysqli_connect' not found in C:\wamp\www\NINJA\db_connect.php on line 9
    Call Stack
    # Time Memory Function Location
    1 0.0011 374016 {main}( ) ..\index.php:0
    2 0.0019 377856 include( 'C:\wamp\www\NINJA\db_connect.php' ) ..\index.php:10

  9. #9
    Join Date
    Jan 2009
    Posts
    3,346
    You need to change that line back to the way you had it before:
    $mysqli = new mysqli($host, $username, $password, $db_name);

  10. #10
    Join Date
    Mar 2012
    Location
    Battle Ground, Washington
    Posts
    30
    Still get the error. Now my PHPMyAdmin is broken..SHOOT ME NOW!!

  11. #11
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    I ran this on my WAMP system, and it connects fine;

    PHP Code:

    //set connection variables
    $host "localhost";
    $username "root";
    $password "";
    $db_name "test"//database name

    echo "Hello<br>";

    //connect to mysql server
    $mysqli =  mysqli_connect($host$username$password$db_name);

    //check if any connection error was encountered
    if(mysqli_connect_errno()) {
        echo 
    "Error: Could not connect to database.";
        
    }else {echo 
    "Connected to DB";} 
    If I add "new" in front of mysqli_connect I get this error;
    Fatal error: Class 'mysqli_connect' not found in C:\wamp\www\mysqli.php on line 19
    Last edited by Dasher; 03-28-2012 at 11:01 PM.

  12. #12
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by Schmit43 View Post
    Still get the error. Now my PHPMyAdmin is broken..SHOOT ME NOW!!
    What do you mean it is broken? Do you get an error message?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles