致命错误:调用成员函数prepare()null [英] Fatal error: Call to a member function prepare() on null

查看:444
本文介绍了致命错误:调用成员函数prepare()null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code:

 函数insertheadright($ name){
$ str1 = xss($ name);
$ sql =INSERT INTO`nav-menu`(`id`,`Head-categories`,`RightMenu`,`LeftMenu`,`Sub-categories`,`Show-sub`,`Show-head ','keywords','description`)VALUES(NULL,?,'1','0','0','0','1','','');
$ result = $ connect-> prepare($ sql);
$ result-> bindValue(1,$ str1);
$ query = $ result-> execute();
if($ query){
$ num = 1;
返回$ num;
} else {
$ num = 0;
返回$ num;
}
}

代码2:

  $ object = insertheadright($ _ POST [nav-name]); 
if(isset($ object)){
if($ object == 1)
echo< div class = ok>请参阅附件< / div>;
else
echo'< div class =error>更多资讯快速入门< / div>';

错误:

< blockquote>

注意:未定义变量:在第8行连接到/home/user/domains/site/public_html/inc/nav/function-nav.php致命错误:调用成员函数prepare ()on null / in /home/designpr/domains/shrg.ir/public_html/inc/nav/function-nav.php on line 8



解决方案

使连接变量可以像你这样访问你的函数:

  function insertheadright ($ name){
global $ connect;

或者,您可以将它作为参数传递给函数:

 函数insertheadright($ connect,$ name){

取决于你的代码,其中一个可能比另一个更好,但只要你保持一致,你就可以。


code:

function insertheadright($name) {
    $str1 = xss($name);
    $sql = "INSERT INTO `nav-menu` (`id`, `Head-categories`, `RightMenu`, `LeftMenu`, `Sub-categories`, `Show-sub`, `Show-head`, `keywords`, `description`) VALUES (NULL, ?, '1', '0', '0', '0', '1', '', '')";
    $result = $connect->prepare($sql);
    $result->bindValue(1, $str1);
    $query = $result->execute();
    if ($query) {
        $num = 1;
        return $num;
    } else {
        $num = 0;
        return $num;
    }
}

code 2:

$object = insertheadright($_POST["nav-name"]);
if (isset($object)) {
    if ($object == 1)
        echo "<div class=ok>منو با موفقیت افزوده شد</div>";
    else
        echo '<div class="error">مشکل در ثبت فهرست</div>';
}

in error:

Notice: Undefined variable: connect in /home/user/domains/site.com/public_html/inc/nav/function-nav.php on line 8 Fatal error: Call to a member function prepare() on null in /home/designpr/domains/shrg.ir/public_html/inc/nav/function-nav.php on line 8

解决方案

Make the connect variable accessible to your function like so:

function insertheadright($name) {
    global $connect;

or, you could pass it as an argument to the function:

function insertheadright($connect, $name) {

Depending on your code, one might be better then the other but as long as you remain consistent you'll be okay.

这篇关于致命错误:调用成员函数prepare()null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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