我在 php mysql 中遇到问题,在调用 json_encode 方法后在数据库中获得类似的条目。这是我的 php 代码:
class xyz
{
private $_mysqli;
public function __construct( $hostname, $user, $password, $database ) {
$this->_mysqli = new mysqli( $hostname, $user, $password, $database );
if ( mysqli_connect_errno() ) {
die('couldn\'t connect connection ('. mysqli_connect_errno(). ')'. mysqli_connect_error() );
}
return $this->_mysqli;
}
public function insert( $user, $app ) {
if ($stmt = $this->_mysqli->prepare("INSERT INTO `track` ( user, app ) VALUES (?, ?)")) {
$stmt->bind_param( 'ss', $user, $app );
$stmt->execute();
$stmt->close();
} else {
printf( "Prepared Statement Error: %s\n", $this->_mysqli->error );
}
}
public function __destruct() {
return $this->_mysqli->close();
}
public function app( $params = array(), $index = NULL ) {
//get ads from array based on scheduling
if (is_null( $index )) {
return $params;
} else {
//$this->insert( $user, $ap );
echo json_encode(array('delay' => '43200000', 'success' => 'true', 'result' => json_encode($params[$index]) ));
}
}
}
error_reporting( E_ALL );
ini_set( "display_errors", 1 );
$user = $_REQUEST['user'];
$app = $_REQUEST['app'];
require 'config.inc.php'; //file contain db credentials and array named $params
$res = new xyz( 'localhost', 'root' , '#####' ,'user_db' );
$res->insert( $user, $app );
$res->app($params, 1);
如果我调用$res->insert_query( $imei, $pn )
and $res->app($params, 1)
,它会在浏览器上显示一次 json,但会在数据库中插入两次条目。这两种情况都会发生,我可以将插入函数放在应用程序函数中,也可以使用类对象在外部调用。就好像我在这些功能中有一个错误。但我无法解决它,请帮助我解决这个问题。