我有两个 php 变量,一个整数和另一个 json,我将它们转换为字符串变量,然后将它们插入到 postgresql 数据库中。
将整数转换为字符串变量:
$string1 = (string)$integer;
将 json 从 facebook api 转换为字符串变量:
$string2 = json_encode($json);
现在,我必须将这两个字符串变量插入到 postgres 数据库中:
$query = "INSERT INTO interests VALUES(". $string1 ." ," . $string2 .")";
pg_query($con, $query) or die("Cannot execute query: $query\n");
这是行不通的。我已经尝试了很多解决方案,但仍然无法正常工作。
我将函数更改为插入数据库
function push_interests(){
$id = $facebook->getUser();
$int = $facebook->api('/me/interests');
$host = "hostname";
$user = "user";
$pass = "password";
$db = "database";
$con = pg_connect("host=$host dbname=$db user=$user password=$pass")
or die ("Could not connect to server\n");
$id = (string)$id;
$int = json_encode($int);
$sql = "INSERT INTO interests VALUES($1,$2)";
pg_prepare($con,'my_insert', $sql) or die ("Cannot prepare statement1\n") ;
pg_execute($con,'my_insert', array($id,$int)) or die ("Cannot execute statement1\n");
pg_close($con);
}
输出是:无法执行语句 1 我创建了如下数据库:
$query = "DROP TABLE IF EXISTS interests";
pg_query($con, $query) or die("Cannot execute query: $query\n");
$query = "CREATE TABLE interests(id VARCHAR(25) PRIMARY KEY, interests VARCHAR(500))";
pg_query($con, $query) or die("Cannot execute query: $query\n");