首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ChangingCode从MySQL到PDO

ChangingCode从MySQL到PDO
EN

Stack Overflow用户
提问于 2013-07-04 02:10:26
回答 2查看 398关注 0票数 6

我已经用MySQL语法编写了一个CMS脚本。

我想用PDO语法取代MySQL语法。有人能帮我吗,,向我解释怎么做

这是脚本中的代码。

代码语言:javascript
复制
<?php
    $querytemp = mysql_query("select * from main_setting") or die (mysql_error());
    $row = mysql_fetch_object($querytemp);

    include "inc/upcenter_block.php";

    echo "
        <div class='headmenu'>$row->news1</div>
        <div class='bodymenu'>
        <p>".nl2br($row->news)."</p>
    </div> ";

    include "inc/downcenter_block.php";
?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-04 10:11:15

首先,如果您想从mysql_*更改为PDO

您需要更改脚本中的所有代码,而不仅仅是一个无法工作的代码。

如果你要把代码从mysql_*改为PDO

您必须使用PDO更改到数据库的连接。

这是一个样本:

代码语言:javascript
复制
// here we set the variables 
$dbhost = "localhost";
$dbname = "testcreate";
$dbuser = "root";
$dbpass = "mysql";

// here we are using ( try {} ) to catch the errors that will shows up and handle it in a nicer way
    try {
    $db = new PDO('mysql:host='.$dbhost.';dbname='.$dbname.';charset=utf-8', ''.$dbuser.'', ''.$dbpass.'');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        echo 'Error : <br>' . $e->getMessage();
    }
代码语言:javascript
复制
// here we set the varible for the connection = then starting the cennction with new POD();
$db = new PDO('mysql:host='.$dbhost.';dbname='.$dbname.';charset=utf-8', ''.$dbuser.'', ''.$dbpass.'');
代码语言:javascript
复制
// here we set an Attribute to handle the errors
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// you dont need to use it in our case because we already catching the error and handling it in out way
代码语言:javascript
复制
  // here we catch the error then handling it by echo a msg and then we used
  // $e->getMessage(); to get the error msg that should be throwing in the page
    catch (PDOException $e) {
        echo 'Error : <br>' . $e->getMessage();
    }

现在我们已经完成了连接,我将向您展示如何查询和获取表。

代码语言:javascript
复制
 // this is how we will use query
 $qr = $db->query()

 // and this is how to fetch it by taking the query variable and use the arrow then fetch 
 $ro = $qr->fetch()

我将为您的代码演示一个示例。

代码语言:javascript
复制
$querytemp = mysql_query("select * from main_setting") or die (mysql_error());
$row = mysql_fetch_object($querytemp);

我们会把这个改为

代码语言:javascript
复制
$querytemp = $db->query("select * from main_setting");
$row = $querytemp->fetch(PDO::FETCH_OBJ);

所以现在您可以在PDO中使用$row->news

现在,您可以轻松地将代码更改为PDO。

票数 10
EN

Stack Overflow用户

发布于 2013-07-04 02:14:32

将该脚本转换为以下内容:

代码语言:javascript
复制
// $pdo = connection

try {
    $stmt = $pdo->prepare("select * from main_setting");
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_OBJ);
} catch (\PDOException $e) {
    // something went wrong
}

// ...

这是一个解释:

  • PDO::prepare将从MYSQL查询中创建一个准备好的语句,并将其存储到一个变量中。
  • PDOStatement::execute将使用传递给它的参数数组执行准备好的语句(在本例中为无,因为查询没有任何参数)。
  • PDOStatement::fetch将获取最后一次执行的结果。默认情况下,它将获取到数组中。如果您传递PDO::FETCH_OBJ,它将获取一个对象。

默认情况下,PDO使用异常,这意味着您可以使用try-catch块来捕获错误(异常类型的PDOException)。

还注意到:

代码语言:javascript
复制
$stmt = $pdo->prepare("select * from main_setting");
$stmt->execute();

可以使用PDO::query将其缩短为:

代码语言:javascript
复制
$stmt = $pdo->query("select * from main_setting");
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17460857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档