SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 40

Thread: Can Not Connect To DB

  1. #1
    SitePoint Zealot
    Join Date
    Apr 2011
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can Not Connect To DB

    Hey guys, here is the piece of code that I am using that is located w/in a folder.. I type http://localhost/additem and the 3rd "IF" statement is thrown, I am tried moving a copy of the DB Folder itself to the same master folder that the additem folder is located w/in..

    <?php
    $link = mysqli_connect('localhost', 'root', '');
    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();
    }

    $sql = 'CREATE TABLE joke (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    joketext TEXT,
    jokedate DATE NOT NULL
    ) DEFAULT CHARACTER SET utf8';
    if (!mysqli_query($link, $sql))
    {
    $output = 'Error creating joke table: ' . mysqli_error($link);
    include 'output.html.php';
    exit();
    }

    $output = 'Joke table successfully created.';
    include 'output.html.php';
    ?>

  2. #2
    SitePoint Zealot Cute Tink's Avatar
    Join Date
    Apr 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (!mysqli_select_db($link'ijdb')) 
    should be

    PHP Code:
    if (!mysqli_select_db('ijdb'$link)) 
    You had the arguments reversed.

    You have it reversed on several other functions as well. The $link would be second on set_charset and query as well.

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2011
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From I am told, you have to pass the $link, before you can pass the file (db) you are looking for.

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2011
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to recap this..
    I am typing in:

    localhost/chapter4/connect

    and the file/folder itself is @:
    HD/Library/WebServer/Documents/Chapter4/connect

    I checked w/in my "myphpadmin" and there is a table named "ijdb", yet it will not connect to it.

  5. #5
    SitePoint Zealot Cute Tink's Avatar
    Join Date
    Apr 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As I told you, you are passing the values in the wrong order:

    mysql_select_db ( string $database_name [, resource $link_identifier ] )

    PHP: mysql_select_db - Manual

    Same with mysql_query:

    mysql_query ( string $query [, resource $link_identifier ] )

    PHP: mysql_query - Manual

    Trust me.

    You can write it like this if you want to know why one of your functions is failing:

    PHP Code:
    mysqli_select_db($link'ijdb') or die(mysql_error()); 

  6. #6
    SitePoint Zealot
    Join Date
    Apr 2011
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cute Tink View Post
    As I told you, you are passing the values in the wrong order:

    mysql_select_db ( string $database_name [, resource $link_identifier ] )

    PHP: mysql_select_db - Manual

    Same with mysql_query:

    mysql_query ( string $query [, resource $link_identifier ] )

    PHP: mysql_query - Manual

    Trust me.

    You can write it like this if you want to know why one of your functions is failing:

    PHP Code:
    mysqli_select_db($link'ijdb') or die(mysql_error()); 
    Well I tried that already and it didnt work.. and according my "Site Point" book. it says mysqli_query(link, query) which means you pass the link before you pass the item you want. BUT, either way, I am still getting the same error message. Also, I tried switching the link, utf8 and when i do.. it throws the 2nd error

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,530
    Mentioned
    31 Post(s)
    Tagged
    0 Thread(s)
    mysqli takes link first. base mysql takes dbname first. (You should actually be passing the DBName as parameter 4 on your connect command, btw)

    This database... does exist, right? You've already created it?

  8. #8
    From Italy with love bronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    8,604
    Mentioned
    76 Post(s)
    Tagged
    4 Thread(s)
    Instead of displaying a generic error message, display mysqli_error().

  9. #9
    SitePoint Zealot Cute Tink's Avatar
    Join Date
    Apr 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My apologies. I was looking only at mysql_select_db. I didn't notice you were using mysqli_select_db.

    At any rate, you should run this snippet:

    PHP Code:
    mysqli_select_db($link'ijdb') or die(mysqli_error()); 

  10. #10
    SitePoint Zealot
    Join Date
    Apr 2011
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this the way it should look? B/C if so, I am not seeing a error msg. btw, I tried mysqli_select_db($link, 'ijdb') or die(mysqli_error()); and I still do not get any sort of error msg.

    if (!mysqli_select_db($link, 'ijdb'));
    {
    $output = die(mysqli_error());
    include 'output.html.php';
    exit();
    }

  11. #11
    SitePoint Zealot Cute Tink's Avatar
    Join Date
    Apr 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    die() is like exit(). It will stop processing of the file. If you want the script to continue, then do this:

    PHP Code:
    if (!mysqli_select_db($link'ijdb'));
    {
    $output mysqli_error();
    include 
    'output.html.php';
    exit();


  12. #12
    SitePoint Zealot
    Join Date
    Apr 2011
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cute Tink View Post
    die() is like exit(). It will stop processing of the file. If you want the script to continue, then do this:

    PHP Code:
    if (!mysqli_select_db($link'ijdb'));
    {
    $output mysqli_error();
    include 
    'output.html.php';
    exit();

    Yea, I think I tried that also.. still no error msg.. I even went into my MAMP folder and made sure that error msg'ing was turned on..

  13. #13
    SitePoint Zealot Cute Tink's Avatar
    Join Date
    Apr 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Out of curiosity, since you are using mysqli, what happens if you do this:

    PHP Code:
    $link mysqli_connect('localhost''root''''ijdb'); 
    Mysqli allows you to include the database name in the connect function.

  14. #14
    SitePoint Zealot
    Join Date
    Apr 2011
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if I do that, the first
    if (!$link)
    {
    $output = 'Unable to connect to the database server.';
    include 'output.html.php';
    exit();
    }
    is thrown.

  15. #15
    SitePoint Zealot Cute Tink's Avatar
    Join Date
    Apr 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you connect to any other table?

    I must admit, this one is throwing me for a loop. I've never not gotten an error message from a failed database connection, so I'm a bit at a loss. The only other thing I can think of is that the user is not permitted to access the table, but if you are using the root account, that shouldn't be an issue and it should still throw an error message.

  16. #16
    SitePoint Zealot
    Join Date
    Apr 2011
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually I have never done this before.. I only have 2 tables in my DB.. one is icdb and the other is ijdb. I have tried to change the text for the other db and it still wont connect. BTW, this is all stored locally on my computer...

  17. #17
    SitePoint Zealot Cute Tink's Avatar
    Join Date
    Apr 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you have the same user/password combo in your scripts as you use for myphpadmin? You said you can get to the table via myphpadmin, so if the user/pass is different for that interface, try switching it.

  18. #18
    SitePoint Zealot
    Join Date
    Apr 2011
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From what I can tell, my user/passwords are root/root, although I am not sure all the places to check for that. I do know that
    mysqladmin is set for root/root,
    /Applications/MAMP/bin/phpMyAdmin is also set for root/root,
    and
    /Applications/MAMP/bin/mamp/index.php is set to root/root

  19. #19
    SitePoint Zealot Cute Tink's Avatar
    Join Date
    Apr 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wait. Go back to this:

    PHP Code:
    if (!mysqli_select_db($link'ijdb'));

    {

    $output mysqli_error();

    include 
    'output.html.php';

    exit();


    And use this:

    PHP Code:
    if (!mysqli_select_db($link'ijdb'));

    {

    $output mysqli_error($link);

    include 
    'output.html.php';

    exit();


    I never used procedural style mysqli, so I didn't put the $link into the error function, and from the php manual, it says it should be there.

  20. #20
    SitePoint Zealot
    Join Date
    Apr 2011
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cute Tink View Post
    Wait. Go back to this:

    And use this:

    PHP Code:
    if (!mysqli_select_db($link'ijdb'));

    {

    $output mysqli_error($link);

    include 
    'output.html.php';

    exit();


    I never used procedural style mysqli, so I didn't put the $link into the error function, and from the php manual, it says it should be there.
    Ok that got me a different response..
    Unknown database 'ijdb'

  21. #21
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,530
    Mentioned
    31 Post(s)
    Tagged
    0 Thread(s)
    Remember when I said "You have created this database, right?"... Guess what. you havent.

  22. #22
    SitePoint Zealot
    Join Date
    Apr 2011
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No I know that, that exists.. if I try to type localhost/ijdb it gives me this:
    Forbidden

    You don't have permission to access /ijdb/ on this server.
    if I try the localhost with any other name, it returns false, also I can see it in my myphpadmin control panel.. also I have a icdb table with 30 things in it.. and when I switch out the ijdb for the icdb, i get the same response

  23. #23
    SitePoint Zealot Cute Tink's Avatar
    Join Date
    Apr 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try going into myphpadmin and in the box where it says Create New Database, type in ijdb and see what happens. If your database is there, then it should fail.

    Your error says that you don't have the database.

  24. #24
    SitePoint Zealot
    Join Date
    Apr 2011
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cute Tink View Post
    Try going into myphpadmin and in the box where it says Create New Database, type in ijdb and see what happens. If your database is there, then it should fail.

    Your error says that you don't have the database.
    Trying your suggestion I get this:
    PHP Code:
    #1007 - Can't create database 'ijdb'; database exists
    CREATE DATABASE  `ijdb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci

  25. #25
    SitePoint Zealot
    Join Date
    Apr 2011
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am going to have to guess that the localhost does not know where to locate the file..

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
  •