mysqli:插入和更新日期的准备语句错误?

发布于 2024-10-20 09:44:10 字数 2354 浏览 1 评论 0原文

我想使用准备好的语句在 mysqli 中插入和更新数据,

下面是我的 database 类,

class database extends mysqli
{
    ...

    # insert and update data
    public function query_stmt($sql,$types = null,$params = null)
    {
        # create a prepared statement
        $stmt = parent::prepare($sql);

        if($stmt)
        {
            # bind parameters dynamically for markers
            if($types&&$params)
            {
                $bind_names[] = $types;
                for ($i=0; $i<count($params);$i++) 
                {
                    $bind_name = 'bind' . $i;
                    $$bind_name = $params[$i];
                    $bind_names[] = &$$bind_name;
                }
                $return = call_user_func_array(array($stmt,'bind_param'),$bind_names);
            }

            # execute query 
            $result = $stmt->execute();

            if($result) return 'success!';
                else return 'failed!';

            # close statement
            $stmt->close();

        }

        else
        {
            return self::get_error();
        }
    }

    # display error
    public function get_error() 
    {
        if($this->errno || $this->error)
        {
            return sprintf("Error (%d): %s",$this->errno,$this->error);
        }
    }

    public function __destruct()
    {
        parent::close();
        //echo "Destructor Called";
    }
}

当我有这样的正确查询时它工作正常,

$sql = "
    INSERT root_countries_cities_towns (
        tcc_names,
        cny_numberic,
        tcc_created
    )VALUES(
        ?,
        ?,
        NOW()
    )";

$mysqli->query_stmt($sql, 'ss',array('UK','004'));

但我希望它在我有一个查询错误,例如

$sql = "
        INSERT root_countries_cities_townx (
            tcc_names,
            cny_numberic,
            tcc_created
        )VALUES(
            ?,
            ?,
            NOW()
        )";

root_countries_cities_townx表不存在。所以我希望query_stmt的方法将这种类型的错误消息返回到我的浏览器。但我的方法不行!为什么?

我尝试测试它以返回一些默认单词,无论是否可以,但我的浏览器上根本没有显示任何内容,

# execute query 
$result = $stmt->execute();

if($result) return 'success!';
else return 'failed!';

我应该在代码中添加或更改什么,以便它可以在正常时返回积极的消息,并且不正常时返回负面消息?

谢谢。

I want to use prepared statement to insert and update data in mysqli,

Below is my database class,

class database extends mysqli
{
    ...

    # insert and update data
    public function query_stmt($sql,$types = null,$params = null)
    {
        # create a prepared statement
        $stmt = parent::prepare($sql);

        if($stmt)
        {
            # bind parameters dynamically for markers
            if($types&&$params)
            {
                $bind_names[] = $types;
                for ($i=0; $i<count($params);$i++) 
                {
                    $bind_name = 'bind' . $i;
                    $bind_name = $params[$i];
                    $bind_names[] = &$bind_name;
                }
                $return = call_user_func_array(array($stmt,'bind_param'),$bind_names);
            }

            # execute query 
            $result = $stmt->execute();

            if($result) return 'success!';
                else return 'failed!';

            # close statement
            $stmt->close();

        }

        else
        {
            return self::get_error();
        }
    }

    # display error
    public function get_error() 
    {
        if($this->errno || $this->error)
        {
            return sprintf("Error (%d): %s",$this->errno,$this->error);
        }
    }

    public function __destruct()
    {
        parent::close();
        //echo "Destructor Called";
    }
}

It works fine when I have a correct query like this,

$sql = "
    INSERT root_countries_cities_towns (
        tcc_names,
        cny_numberic,
        tcc_created
    )VALUES(
        ?,
        ?,
        NOW()
    )";

$mysqli->query_stmt($sql, 'ss',array('UK','004'));

But I want it to return something when I have an incorrect query, such as,

$sql = "
        INSERT root_countries_cities_townx (
            tcc_names,
            cny_numberic,
            tcc_created
        )VALUES(
            ?,
            ?,
            NOW()
        )";

the table of root_countries_cities_townx does not exist. So I want the the method of query_stmt to return this type of error message to my browser. But my method doesn't! Why?

I have tried to test it to return some default words whether it is ok or not, but nothing at all comes out on my browser,

# execute query 
$result = $stmt->execute();

if($result) return 'success!';
else return 'failed!';

What should I add or change in the code so that it can return a positive message when it is ok and returns negative message when it is not ok?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渔村楼浪 2024-10-27 09:44:10

您正在返回错误消息,而不是回显它们。您要么必须修改数据库类以使用 echo 而不是 return,要么执行

$ret_text = $mysqli->query_stmt(...);
echo $ret_text;

You're returning the error messages, not echoing them. You'd either have to modify the DB class to use echo instead of return, or do

$ret_text = $mysqli->query_stmt(...);
echo $ret_text;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文