#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Unable to access database using mysqli


    I have some knowledge of php and mysql but am new to mysqli.
    I have just installed apache with PHP version 5.3.15 on my iMac and am trying to write a program that uses mysqli to access a database, but get an error -
    'Warning: mysqli::mysqli() [mysqli.mysqli]: [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Library/WebServer/Documents/addPlayer.php on line 26

    Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2002): No such file or directory in /Library/WebServer/Documents/addPlayer.php on line 26
    Unable to connect to database [No such file or directory]'

    I checked the phpinfo() file and mysqli is there.

    The code is -

    $db = new mysqli('localhost', 'user', 'pass', 'demo');

    if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
    }else{
    echo 'Connected';
    }

    Any suggestions
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,502
    Rep Power
    594
    Your MySQL daemon is not running.

    P.S. When posting code, use [ PHP ] tags. See the sticky at the top of this forum.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    186
    Rep Power
    84
    Put this in a file called phpinfo.php in the document root

    PHP Code:
    <?php
    phpinfo
    ();
    ?>
    Now, open the file
    http://localhost/phpinfo.php

    Scroll down the page to the Configuration. The sections are listed in alphabetical order. Is mysqli listed?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    How do I start the daemon. Is that thro the apache config file.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,502
    Rep Power
    594
    No. How you start it depends on the OS. Generally with Linux you use 'service mysqld start' or 'mysqlctl'. However, you probably want to have it start on boot, in which case you also need to use 'chkconfig' once.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    876
    Rep Power
    275
    are you certain the MySQL pkg/program is installed on your iMac? Just installing Apache with PHP doesn't automatically install MySQL, too. [its a separate pkg]
    I suggest you do a google search for installing MAMP [Mac-Apache-MySQL-PHP] properly on your OSX version...or find out if its already installed (which is not usually the case).
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    I've got MAMP pro 2.1.2 installed with Mysql 5.5.29 and PHP 3.5.5 with phpMyAdmin.
    If I use the older Mysql functions I don't get any error but I do with the Mysqli functions. I haven't used PHP for about 6 years and am just trying to get back into it.
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    a Google search told me that you should try "127.0.0.1" instead of "localhost".
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    Yes, mysqli is listed in the phpinfo.php file
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Still having problems


    As I have been unable to use mysqli to make a server connection, I have tried using the older mysql functions.
    I don't get an error but I cannot get a connection.
    I have set up a user in PhpMyAdmin as host - localhost, user - John, password - John.
    I have used the following code and cannot get a connection.

    PHP Code:
    $db mysql_connect('localhost','John','John');
    if (!
    $db){
        echo 
    'Error: connection failed';
    }else{
        echo 
    'Error: connection worked';

    Where am I going wrong.
  20. #11
  21. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Like I already suggested above: Try '127.0.0.1' instead of 'localhost'. This will open a TCP connection rather than using a socket (which MySQLi told you doesn't work).

    If you're still getting false, output the exact error message with mysql_error().

    Since phpmyadmin obviously can connect to the database, use the exact database user like in phpmyadmin. If you cannot remember the credentials, look them up in config.inc.php in the phpmyadmin folder.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    In the config.inc.php file the server host is 'localhost', the user is 'root' and the password is 'root'. If I use these or the user of 'John' and password of 'John' I get the error of 'No such file or directory'. If I use the host of '127.0.0.1' with either of the above user/passport combinations I get the error ' Connection refused'.

IMN logo majestic logo threadwatch logo seochat tools logo