简体   繁体   English

我正在尝试在php中使用带有pdo的存储过程,但出现致命错误

[英]i'm trying to use store procedure with pdo in php but getting fatal error

i want to make a store procedure in php mysql which is 我想在php mysql中创建一个存储过程

SELECT * FROM countries WHERE name LIKE ?"

and call it in a function using pdo like function.php 然后使用像function.php这样的pdo在函数中调用它

<?php
Class Get{


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

and call this function in main file can anyone tell me how to do it my approach is 并在主文件中调用此函数,谁能告诉我该怎么做,我的方法是

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

getting error like 出现错误

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-->

this is my search.php 这是我的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'>";
        }

this is my store procedure 这是我的存储过程

CREATE DEFINER=`root`@`localhost` PROCEDURE `GetWords`(IN pword varchar(200))
BEGIN
SELECT * FROM dictionarysearch where word like pword;
END
    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);

    }

In $result you can get array of result from your query 在$ result中,您可以从查询中获取结果数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM