簡體   English   中英

如何將我當前的mysqli程序方式更改為pdo,我可以在其中調用存儲過程

[英]How can i change my current mysqli procedural way into pdo where i can call store procedure

我目前正在使用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;
}

在這里,我想知道如何在使用mysqli_escape_string進行pdo的同時更改函數和主要php腳本調用函數,我希望能獲得一些幫助

對於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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM