0
foreach($res as $key) {
    echo $key['id'].'<br>';
    $sql= "INSERT INTO Post VALUES('".$key['id']."')";

    if (mysql_query($sql) == TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $link->error;
    }
}

For the first entry it works and adds it to the database but after that it gives the following error : Trying to get property of non-object

print_r($res) gives the following output :

Array
(
    [0] => Array
        (
            [message] =>*******
            [created_time] => DateTime Object
                (
                    [date] => 2016-01-13 20:14:32.000000
                    [timezone_type] => 1
                    [timezone] => +00:00
                )

            [id] => 1413366785545501_1686416964907147
        )

    [1] => Array
        (
            [message] =>*****
            [created_time] => DateTime Object
                (
                    [date] => 2015-11-27 15:51:16.000000
                    [timezone_type] => 1
                    [timezone] => +00:00
                )

            [id] => 1413366785545501_1674032329478944
        )

    [2] => Array
        (
            [message] => The 'AD- MAD' competition :)
            [story] => KRITA // Chalk your art. added 4 new photos.
            [created_time] => DateTime Object
                (
                    [date] => 2015-10-07 19:02:12.000000
                    [timezone_type] => 1
                    [timezone] => +00:00
                )

            [id] => 1413366785545501_1662706990611478
        )

    [3] => Array
        (
            [message] => *****
            [story] => KRITA // Chalk your art. created a poll.
            [created_time] => DateTime Object
                (
                    [date] => 2015-10-02 21:06:05.000000
                    [timezone_type] => 1
                    [timezone] => +00:00
                )

            [id] => 1413366785545501_1661659277382916
        )

I just want to store the id's in the database

Raja Panda
  • 51
  • 6
  • post value of `print_r($res)`; – Saty Jun 09 '16 at 11:49
  • Show your array by print_r($array_name); – Anand Jain Jun 09 '16 at 11:50
  • 4
    Note: The `mysql_*` functions are deprecated, they have been removed from PHP 7, your code will stop working when you upgrade to that version. You should not write new code using them, use [`mysqli_*` or PDO](http://php.net/manual/en/mysqlinfo.api.choosing.php) instead. – Gerald Schneider Jun 09 '16 at 11:51
  • what is $link?....you have not defined it here..remove $link->error from code..it is giving error – Dhara Parmar Jun 09 '16 at 11:51
  • 1
    The only thing that could produce that error in the snippet is `$link->error`. What is the value of `$link`? – cjquinn Jun 09 '16 at 11:51
  • $link = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD); even if i remove it . The data is not stored in database – Raja Panda Jun 09 '16 at 11:55
  • Possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Gerald Schneider Jun 09 '16 at 12:03
  • You need to post more of your code. – Ian Brindley Jun 09 '16 at 12:03
  • Can you post the structure of the `Post` table? If there is more than just `id` as columns you need to specify which columns you are inserting data for. – cjquinn Jun 09 '16 at 12:07

1 Answers1

1

mysql_connect() does not return an object, so you get the given error message when you try to use $link->error. Replace it with mysql_error():

echo "Error: " . $sql . "<br>" . mysql_error();

You will get a clear error message why the SQL query failed.

But I have to repeat my note from the comment: The mysql_* functions are deprecated, they have been removed from PHP 7, your code will stop working when you upgrade to that version. You should not write new code using them, use mysqli_* or PDO instead.

Gerald Schneider
  • 16,520
  • 9
  • 55
  • 76