“使用mysqli或PDO”错误

时间:2014-10-30 12:03:45

标签: php pdo mysqli

当我运行下面的代码时,我收到错误告诉我使用mysqli或PDO。请帮我将其更改为mysqli代码。我试过但它不起作用,因为我是初学者,我复制了这段代码。

<?php

//Make the database connection.
db_connect() or die('Unable to connect to database server!');

function db_connect($server = 'localhost', $username = 'root', $password = '', $database =    'hct_db', $link = 'db_link') {
global $$link;
$$link = mysql_connect($server, $username, $password);
if ($$link) mysql_select_db($database);
return $$link;
}

//Function to handle database errors.
 function db_error($query, $errno, $error) { 
 die('Cannot connect to database');
 }

//Function to query the database.
function db_query($query, $link = 'db_link') {
global $$link;
$result = mysql_query($query, $$link) or db_error($query, mysql_errno(), mysql_error());
return $result;
}

//Get a row from the database query
function db_fetch_array($db_query) {
return mysql_fetch_array($db_query, MYSQL_ASSOC);
}
?>

1 个答案:

答案 0 :(得分:1)

自PHP 5.5.0起,不推荐使用MySQL扩展,不建议用于编写新代码,因为将来会删除它。相反,应该使用mysqli或PDO_MySQL扩展名。

作为MySQLi的快速解决方案

<?php

//Make the database connection.
db_connect() or die('Unable to connect to database server!');

function db_connect($server = 'localhost', $username = 'root', $password = '', $database =    'hct_db', $link = 'db_link') {
    global $$link;
    $$link = mysqli_connect($server, $username, $password);
    if ($$link) mysqli_select_db($$link, $database);
    return $$link;
}

//Function to handle database errors.
function db_error($query, $errno, $error) { 
    die('Cannot connect to database');
}

//Function to query the database.
function db_query($query, $link = 'db_link') {
    global $$link;
    $result = mysqli_query($$link, $query) or db_error($query, mysqli_errno($$link), mysqli_error($$link));
    return $result;
}

//Get a row from the database query
function db_fetch_array($db_query) {
    return mysqli_fetch_array($db_query);
}
?>