Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8799905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:26:49+00:00 2026-06-14T00:26:49+00:00

I have two php variables, one integer and other json, which I convert into

  • 0

I have two php variables, one integer and other json, which I convert into string variable and then inserting them inside a postgresql database.

Converting integer into string variable:

$string1 = (string)$integer;

Coneverting json from facebook api into string variable:

$string2 = json_encode($json);

Now, I have to insert these two string variables into postgres database:

$query = "INSERT INTO interests VALUES(". $string1 ." ," . $string2 .")"; 
pg_query($con, $query) or die("Cannot execute query: $query\n");

This is not working. I have tried a lot of solutions but still not working.

I changed my function to insert into database

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); 

}

Output is: cannot execute statement1
I have created database as below:

$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"); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:26:50+00:00Added an answer on June 14, 2026 at 12:26 am

    Because strings need to be surrounded with simple quotes. I would strongly advise you use prepared statements to ignore these kind of problems and ensure correct variable escaping to prevent your application from beeing hacked trough SQL injection.

    $sql = "INSERT INTO interests VALUES ($1, $2)";
    $result = pg_prepare($con, 'my_insert', $sql);
    $result = pg_execute($con, 'my_insert', array($string1, $string2));
    

    See http://php.net/manual/en/function.pg-prepare.php

    Edit: Here is the actual code I’ve tested:

    <?php
    
    $con = pg_connect('')
        or die ("Could not connect to server\n"); 
    
    $id = (string) 5;
    $int = json_encode(array('pika' => 'chu', 'plop' => array(1, 2, 3)));
    
    $query = "CREATE TABLE interests(id VARCHAR(25) PRIMARY KEY, interests VARCHAR(500))";
    pg_query($query) or die('creating table failed.');
    
    $sql = "INSERT INTO interests (id, interests) VALUES ($1, $2)";
    pg_prepare('my_query', $sql);
    pg_execute('my_query', array($id, $int)) or die("Error while inserting.");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code below can migrate the two variables from one file into the other.
I have two php files, one manages database connection and the other retrieves data
I have a PHP page which accepts two variables <?php $formurl = listen.html ;
I have two float variables that are equal, I retrieve them by PHP and
I have two PHP apps which have twho class with the same name. -
I am confused with & and && . I have two PHP books. One
I have one php function that receives drop down variables through javascript. The variables
I have two file one is index.php another one is dbsubmit.php In my index.php
I have two PHP functions to calculate the relation between two texts. They both
I have two folders php and perl . They contain index.php and index.pl ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.