我在stackoverflow上尝试了几乎所有可能的解决方案,但仍然没有解决方案。
mysqli_real_escape_string($ conn,$ string);一直在返回布尔值(false)。
以下是DbConnect.php
的代码:
<?php
/**
* Handling database connection
*
* @author Asif Ali
*/
class DbConnect {
private $conn;
function __construct() {
}
/**
* Establishing database connection
* @return database connection handler
*/
function connect() {
// Connecting to mysql database
$this->conn = new mysqli('xxxxx', 'xxxxxx', 'xxxxx', 'xxxxx');
// Check for database connection error
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// returing connection resource
return $this->conn;
}
}
?>
这是我的DbHandler.php
:
class DbOperation
{
private $conn;
function __construct()
{
require_once dirname(__FILE__) . '/DbConnect.php';
$db = new DbConnect();
$this->conn = $db->connect();
}
public function addComment($user_id,$pid,$comment){
$comment_final = mysqli_real_escape_string($this->conn, $comment);
$sql_query = "INSERT INTO comments(user_id,pid,comment) values('$user_id','$pid','$comment_final')";
mysqli_query($this->conn,$sql_query);
.
.
.
}
}
?>
这是我的api电话:
$app->post('/comments', 'authenticate', function() use ($app) {
// check for required params
verifyRequiredParams(array('pid','comment'));
global $user_id;
$db = new DbOperation();
$response = array();
$pid = $app->request()->post('pid');
$comment = $app->request()->post('comment');
$result = $db->addComment($user_id,$pid,$comment);
输入:您好(成功)
输入:我要去
错误:mysqli_num_rows()期望参数1为mysqli_result,给定布尔值
我正在使用Advanced rest客户端和Android应用来测试端点,两者都失败了。 另外请建议是否有一种方法我可以通过消除转义字符开销同时使用android实现读取(从)和写入(到)mysql数据库,因为它很烦人。