SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP/MySQL Connection Nightmare

    On my ISP's server is installed:

    MySQL 5.5.12
    PHP 5.2.17

    Via phpMyAdmin I setup a simple test database. Now I want to do a test access of the data using a PHP script.

    My ISP said the server path to my test database (john_test) is: /var/lib/mysql/

    And that my access to my test database 'john_test' would be: /var/lib/mysql/john_test

    It appears I'm not doing something right, because I cannot connect with the following script:

    PHP Code:
    <?PHP

    //connect to server 

    $connect mysql_connect("/var/lib/mysql/john_test""john""my_real_password_goes_here");
    if (!
    $connect) {
        die(
    'Not connected : ' mysql_error());
    }

    //connect to datatbase

    $db_selected mysql_select_db("john_test"$connect);
    if (!
    $db_selected) {
        die (
    'Can\'t use john_test : ' mysql_error());
    }


    //query the database

    $query mysql_query("SELECT * FROM table1 WHERE firstname = 'John' ");

    //fetch the results / convert results into an array

        
    WHILE($rows mysql_fetch_array($query)):

           
    $firstname $rows['firstname'];
           
    $lastname $rows ['lastname'];

        echo 
    "$firstname<br>";
        echo 
    "$lastname<br>";

        endwhile;

    ?>
    I'm also a bit suspicious of not using { } 's with the WHILE loop instead of just the ":" as seen in some other code.

    Any assistance would be appreciated !

    Thanks very much.

    -GoingBONKERS
    Last edited by SpacePhoenix; Jan 30, 2014 at 13:59. Reason: placed php tags around php code

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,072
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Before you get any further, please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn't tie you down so much to a particular database server software.

    Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.

    Has the user "John" been given permission to access the database?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    ^ what he says
    And also: what error do you get?

  4. #4
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    The path looks wrong for a socket connection + your missing the socket indicator. ( : or .sock )

    Try using these two examples:
    localhost
    :/var/lib/mysql/john_test

    Try them one by one, and see if the "Not connected" error is still thrown.

    Though as mentioned, you should consider moving over to PDO or mysqli_.

    In regards to the while, that is an alternative syntax for control structures. The key is that it starts with ":" and then a few lines under you have "endwhile;". Which one you use is your choice, but be consistent. The way we do it is use { } in the code layer, and the : endif; etc. in the template code.

    You can read more about it here: http://www.php.net/manual/en/control...ive-syntax.php
    Last edited by TheRedDevil; Jan 31, 2014 at 13:54. Reason: Had to remove autoconvertion into icons.

  5. #5
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies and info. Got re-tasked here with a few Murphy Strikes and finally getting back to this.

    As it turns out, my ISP should have said to use '127.0.0.1' instead of /var/lib/mysql/john_test {SIGH}.

    The initial test (with some code revisions) is now working.

    I appreciated the responses and suggestions.

    -GoingBONKERS


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
  •