如何将我当前的mysqli程序方式更改为pdo,我可以调用存储过程

时间:2018-02-02 05:43:04

标签: php mysqli pdo

我目前正在使用mysqli程序编写代码,我想在pdo中更改它,因为在mysqli中我是mysqli_escape_string而我不知道如何在pdo中更改它 这是我的mysqli尝试

<?php
    if(isset($_GET['id1'])){
        $id=$_GET['id1'];
        $result=GetWordsById(mysqli_escape_string($conn,$id));//Here GetWordsById is a function calling store procedure
        if(mysqli_num_rows($result)>0)
            $row=mysqli_fetch_array($result);
        $word=$row['word'];
        $meaning=$row['meaning'];
        $synonym=$row['synonym'];
        $antonym=$row['antonym'];


        }
    ?> 

下面是我的function.php

function GetWordsByID($id){
    include("conn.php");
    $result=mysqli_query($conn,"CALL GetWordsById($id)");//Here GetWordsById is my store procedure
    return $result;
}

在这里我想知道如何更改功能和主要PHP脚本调用功能,我正在使用mysqli_escape_string来pdo我会感激一些帮助

1 个答案:

答案 0 :(得分:0)

致PDO,    您的数据库连接文件将如下所示

    <?
        $servername = "localhost";
    $username = "username";  // Enter your db username
    $password = "password";  // Enter your db password
    $dbname = "myDBPDO";   // Enter your db name

    try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }

    ?>

现在来了你的代码,

<?

   if(isset($_GET['id1'])){
        $id=$_GET['id1'];
        $result=GetWordsById($id);//Here GetWordsById is a function calling store procedure
        if($result->fetchColumn()>0){

            $row=$result->fetch(PDO::FETCH_ASSOC);
        $word=$row['word'];
        $meaning=$row['meaning'];
        $synonym=$row['synonym'];
        $antonym=$row['antonym'];


        }

}

和功能,

function GetWordsByID($id){
    include("conn.php");
    $result=$conn->prepare("  ");//Here sql statement 
    $result->execute();
    return $result;
}