Best way to establish Mysql connection through PHP script

gginni

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

Code:
<?php
$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 />";
    }
 
mysql_close($db);
 
?>

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
 
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.*
 
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.
 
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.

So which should I use??? I want to use simple option ... instead of mysql_connect... what should I use???
 
mysql_ is sooooooo 2007. (So is md5 for password hashing, by the way)

Use the mysqli library, or PDO.

mysqli library is not heavy?? what xenforo uses for establishing connection?? I want simplest solution... what should I use instead of mysql_connect??
 
So which should I use??? I want to use simple option ... instead of mysql_connect... what should I use???

You should consider PDO as it is much more flexible (it can be used for all databases that PDO supports)
http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

mysqli library is not heavy?? what xenforo uses for establishing connection?? I want simplest solution... what should I use instead of mysql_connect??
Both MySQLi and PDO should be built-in since PHP 5.3.
 
So which should I use??? I want to use simple option ... instead of mysql_connect... what should I use???
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.


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

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.
 
Top Bottom