将代码从MySQL更改为PDO [英] ChangingCode From MySQL To PDO

查看:59
本文介绍了将代码从MySQL更改为PDO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个使用MySQL语法编写的CMS脚本.

I have made a CMS Script written using MySQL Syntax.

我想用PDO语法替换MySQL语法.有人可以帮助我做到这一点向我解释如何做到这一点吗?

I want to replace the MySQL Syntax with the PDO Syntax. Can someone help me do it and explain to me how to do it?

这是脚本中的代码.

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

推荐答案

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

First if you want to change from mysql_* to PDO

您将需要更改脚本中的所有代码,而不仅仅是 单行不行的

you will need to change all of your codes in the script, not only a single one that just wont work

,如果要将代码从mysql_ *更改为PDO

and if you going to change the codes from mysql_* to PDO

您将不得不使用PDO更改与数据库的连接

you will have to change the connection to the database by using 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();
    }


// 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.'');


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

--------------------------------------------

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

--------------------------------------------

now that we done with the connect i will show you how to query and fetch tables

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

所以现在您可以将$row->news与PDO一起使用

so now you may use $row->news with PDO

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

and now you can change your codes to PDO easily

这篇关于将代码从MySQL更改为PDO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆