mysqli:插入和更新日期的准备语句错误?
我想使用准备好的语句在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在返回错误消息,而不是回显它们。您要么必须修改数据库类以使用
echo
而不是 return,要么执行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