Does php 5.6 support prepared statement?

谁说胖子不能爱 提交于 2019-12-26 08:56:08

问题


Does php 5.6 support prepared statement ? As I'm getting this error :

"PHP Fatal error: Call to a member function prepare() on resource "

Here Is the code :

$testSql = $ce_conn -> prepare('select transaction_id from 
tbl_sales_revenue limit 1');
$testSql -> execute();
$testSql -> bind_result($transaction_id);
$testSql -> fetch();
$testSql -> close();

function connectCE() {
    global $config;
    $ce_conn = mysql_connect($config['Datasources']['CE']['host'], $config['Datasources']['CE']['username'], $config['Datasources']['CE']['password']);
    if (!$ce_conn) {
        die('Could not connect to CE database : ' . mysql_error());
    }
    $ce_selected = mysql_select_db($config['Datasources']['CE']['database'], $ce_conn);
    if (!$ce_selected) {
        die('Could not use CE dot database : ' . mysql_error());
    }
    return $ce_conn;
}

回答1:


Yes, PHP 5.6 supports prepared statements. Both MySQLi and PDO extensions have this functionality.

What you are using here is mysql_* API, which has been deprecated and removed. Do not use it! Switch to prepared statements.

Additionally, PHP 5.6 is not supported anymore, and you should consider upgrading as soon as possible.

A sample prepared statement using PDO could look like this:

$dsn = "mysql:host=$host;dbname=$db;charset=utf8mb4";
$options = [
    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
    \PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $user, $pass, $options);

$stmt = $pdo->prepare('select transaction_id from tbl_sales_revenue WHERE id=? limit 1');
$stmt->execute([$_POST['id']]);
$record = $stmt->fetch();

Or using mysqli:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli($host, $user, $pass, $db);
$mysqli->set_charset('utf8mb4');

$stmt = $mysqli->prepare('select transaction_id from tbl_sales_revenue WHERE id=? limit 1');
$stmt->bind_param('s', $_POST['id']);
$stmt->execute();
$record = $stmt->get_result()->fetch_assoc();



回答2:


If you want to use the prepare() method, you need to use the PDO Class.

Here you can find a link on how to connect to PDO. After the switch to PDO, you can use the prepare() statement.



来源:https://stackoverflow.com/questions/58178477/does-php-5-6-support-prepared-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!