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

9
  • post value of print_r($res); Commented Jun 9, 2016 at 11:49
  • Show your array by print_r($array_name); Commented Jun 9, 2016 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 instead. Commented Jun 9, 2016 at 11:51
  • what is $link?....you have not defined it here..remove $link->error from code..it is giving error Commented Jun 9, 2016 at 11:51
  • 1
    The only thing that could produce that error in the snippet is $link->error. What is the value of $link? Commented Jun 9, 2016 at 11:51

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.