我正在尝试在php中使用存储过程与pdo但是会遇到致命的错误

时间:2018-01-31 06:43:26

标签: php pdo

我想在php mysql中创建一个存储过程

SELECT * FROM countries WHERE name LIKE ?"

并使用像function.php

这样的pdo在函数中调用它
<?php
Class Get{


    public function GetWord($word){
        include("conn.php");
        $result=$conn->query("CALL GetWord($word)");
        return $this->result;
    }
}
?>

并在主文件中调用此函数任何人都可以告诉我该怎么做 我的方法是

<script type="text/javascript">
    $(document).ready(function(){
        $('#search').keyup(function(){
            var search=$('#search').val();
            if($.trim(search.length)==0){
                $('#result').html('Try to find more words')
            }
            else{
                $.ajax({
                    type:'POST',
                    url:'search.php',
                    data:{'search':search},
                    success:function(data){
                        $('#result').html(data);
                    }
                })
            }
        })
    })

得到错误

Fatal error: Uncaught Error: Call to undefined method Get::prepare() in C:\PHPP\htdocs\learnbootstrap\search.php:7 Stack trace: #0 {main} thrown in C:\PHPP\htdocs\learnbootstrap\search.php on line 7-->

这是我的search.php

if(isset($_POST['search'])){
    $search=$_POST['search'];
    $query=$conn->prepare(GetWords($word));
    $query->execute(["%$search%"]);
    $count=$query->rowCount();
    if($count == 0){
            echo "sorry no results were found";
    }
    else{
        echo "<table class='table table-hover'>";
        }

这是我的商店程序

CREATE DEFINER=`root`@`localhost` PROCEDURE `GetWords`(IN pword varchar(200))
BEGIN
SELECT * FROM dictionarysearch where word like pword;
END

1 个答案:

答案 0 :(得分:0)

    class Get {

        public function getWord($word){
            $stmt = $conn->prepare("CALL GetWord(?);");
            $stmt->execute([ $word ]);
            $stmt->setFetchMode(\PDO::FETCH_ASSOC);
            return $stmt->fetchAll();
        }
    }

    if(isset($_POST['search'])){

        $search=$_POST['search'];
        $G = new Get();
        $result = $G->getWord($search);

        var_dump($result);

    }

在$ result中,您可以从查询中获得结果数组