Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将代码从MySQL更改为PDO_Php_Mysql_Pdo - Fatal编程技术网

Php 将代码从MySQL更改为PDO

Php 将代码从MySQL更改为PDO,php,mysql,pdo,Php,Mysql,Pdo,我用MySQL语法编写了一个CMS脚本 我想用PDO语法替换MySQL语法。有人能帮我做这件事吗?和给我解释一下怎么做 这是脚本中的代码 <?php $querytemp = mysql_query("select * from main_setting") or die (mysql_error()); $row = mysql_fetch_object($querytemp); include "inc/upcenter_block.php"; ec

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

我想用PDO语法替换MySQL语法。有人能帮我做这件事吗?和给我解释一下怎么做

这是脚本中的代码

<?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";
?>

转换该脚本的过程大致如下:

// $pdo = connection

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

// ...
解释如下:

  • 将从MYSQL查询中创建一个准备好的语句,并将其存储到变量中
  • 将使用传递给它的参数数组执行准备好的语句(在本例中为无,因为查询没有任何参数)
  • 将获取上次执行的结果。默认情况下,它将获取到一个数组中。如果您传递
    PDO::FETCH_OBJ
    ,它将获取一个对象
默认情况下,这意味着您可以使用try-catch块捕获错误(异常类型为
PDOException

还请注意:

$stmt = $pdo->prepare("select * from main_setting");
$stmt->execute();
可通过使用缩短为:


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

您需要更改脚本中的所有代码,而不仅仅是 一个就是不行的

如果你想把代码从mysql改成PDO

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

这里有一个例子:

// 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();
    }


我将为您展示一个代码示例

$querytemp = mysql_query("select * from main_setting") or die (mysql_error());
$row = mysql_fetch_object($querytemp);
我们将此更改为

$querytemp = $db->query("select * from main_setting");
$row = $querytemp->fetch(PDO::FETCH_OBJ);
因此,现在您可以在PDO中使用
$row->news


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

请向我解释您所做的!!现在我可以简单地使用$row->news1从tabke?@JhonSmith中回显信息,如果您的
main\u设置
表中有一行名为
news1
,是的。可能,这里需要自我学习:这是一个相当奇怪的赏金。问题是:为什么?为了记录在案,无论谁读到这篇文章:这里的OP是谁试图玩这个系统的袜子傀儡。
// 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
  // 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();
    }
 // 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()
$querytemp = mysql_query("select * from main_setting") or die (mysql_error());
$row = mysql_fetch_object($querytemp);
$querytemp = $db->query("select * from main_setting");
$row = $querytemp->fetch(PDO::FETCH_OBJ);