1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Best way to establish Mysql connection through PHP script

Discussion in 'General PHP and MySQL Discussions' started by gginni, Apr 28, 2013.

  1. gginni

    gginni Active Member

    I was using below code in my custom page for establishing sql connection:-

    $db = mysql_connect("localhost","username","password");
        if (!$db) {
            die("Database connection failed miserably: " . mysql_error());
    $db_select = mysql_select_db("databasename",$db);
        if (!$db_select) {
            die("Database selection also failed miserably: " . mysql_error());
    $result = mysql_query("SELECT * FROM mytable", $db);
        if (!$result) {
            die("Database query failed: " . mysql_error());
        while ($row = mysql_fetch_array($result)) {
            echo $row[1]." ".$row[2]."<br />";
    I wrote this code yesterday night and within 12 hours I started getting below error:-

    Cannot allocate memory: couldn't create child process:

    Can anyone tell me what was wrong in creating connection using above way. Do I need to use persistent sql connection??? I'm new in PHP , so any help will be greatly appreciated
  2. 0xym0r0n

    0xym0r0n Well-Known Member

    I got those errors randomly on my old host. No idea wtf it really means but I can probably suggest that SELECT * should be narrowed down to exactly what you need. If you're calling on the ENTIRE db for your xf install, then a poopy hosting company might give your a lot of those errors. lol

    I moved hosts and haven't really had an issue...

    But at least narrow down the exact tables you're using ie xf_user.*, xf_thread.*
  3. tyteen4a03

    tyteen4a03 Well-Known Member

    mysql_ is sooooooo 2007. (So is md5 for password hashing, by the way)

    Use the mysqli library, or PDO.
  4. rellek

    rellek Well-Known Member

    mysql_* is not only 2007, it is deprecated in PHP 5.5 too. So it will throw a message (unless you switch that off with error_reporting), so better don't use it for future compatibility's sake.
  5. gginni

    gginni Active Member

    So which should I use??? I want to use simple option ... instead of mysql_connect... what should I use???
  6. gginni

    gginni Active Member

    mysqli library is not heavy?? what xenforo uses for establishing connection?? I want simplest solution... what should I use instead of mysql_connect??
  7. tyteen4a03

    tyteen4a03 Well-Known Member

    You should consider PDO as it is much more flexible (it can be used for all databases that PDO supports)

    Both MySQLi and PDO should be built-in since PHP 5.3.
  8. Ingenious

    Ingenious Well-Known Member

    gginni you might find the PHP manual useful, it talks about the old mysql_ plus the newer mysqli, with examples:


    It's just a case of looking up some tutorials and adjusting your code from the old mysql_ to the newer mysqli equivalents. PDO as mentioned above is another alternative.
    tyteen4a03 likes this.
  9. rellek

    rellek Well-Known Member

    You can use MySQLi. The functions have almost the same syntax so it's pretty easy to convert mysql_* to mysqli_*.
    I however would consider writing a class to have MySQL access. Because then you always have calls like
    $result = $db->query('SELECT * FROM table');

    no matter what system runs in the background. So if you at one point want or need to replace your MySQL driver, you just change the class and you're fine.

    IIRC, MySQLi is included since PHP 5.0. It was disabled by default in 5.1 though. But as a PHP extension, there was no big deal in re-enabling it.

Share This Page