Reputation: 10046
So I know that mysql_pconnect is deprecated, is there a way actually to make this work, I have an old script which in the connection file has the following:
$success = mysql_pconnect ($mysql_host, $mysql_user, $mysql_password);
if (!$success)
die ("<b>Cannot connect to database, check if username, password and host are correct.</b>");
$success = mysql_select_db ($database);
if (!$success) {
print "<b>Cannot choose database, check if database name is correct.";
die();
}
Do I have an alternative to this?
//LE
try {
$success = new PDO("mysql:host=$mysql_host;dbname=$database", $mysql_user, $mysql_password, array(PDO::ATTR_PERSISTENT=>true));
} catch (PDOException $e) {
die ("<b>Cannot connect to database, check if username, password and host are correct.</b>");
}
$success = mysql_select_db ($database);
if (!$success) {
print "<b>Cannot choose database, check if database name is correct.";
die();
}
this gives me "Cannot choose database, check if database name is correct."
Upvotes: 0
Views: 2073
Reputation: 11
The mysql_pconnect function creates a persistent connection to the database. To do that with PDO (the recommended way to access a database in PHP), you can do this:
try {
$conn = new PDO("mysql:host=$mysql_host;dbname=$database", $mysql_user, $mysql_password, array(PDO::ATTR_PERSISTENT=>true));
} catch (PDOException $e) {
die ("<b>Cannot connect to database, check if username, password and host are correct.</b>");
}
Note that when you use PDO you don't need call mysql_select_db (which is also deprecated). With PDO, the database is part of the DSN (the first parameter of the PDO constructor). In fact, you'll need to replace every call to a mysql_* function with the equivalent PDO method.
Upvotes: -1
Reputation: 3813
Straight from the mysql_pconnect docs:
Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
**Alternatives to this function include:
mysqli_connect() with p: host prefix
PDO::__construct() with PDO::ATTR_PERSISTENT
as a driver option**
Upvotes: 0
Reputation: 2159
You can use mysqli
however PDO
is recommended for doing queries and plain php nowadays.
$connect = new mysqli($host, $user, $password, $db;
$r = $connect->query('SELECT ...');
$rows = $r->fetch_array(MYSQLI_NUM);
Upvotes: 0