Error Trying to get Property... PHP + Error og:image API Facebook (Share)

Asked

Viewed 110 times

0

I have other examples here but none worked for me

I have an instruction to pull an image from the database, only it’s giving the error

"Notice: Trying to get Property of non-object in ...";

$q= $db->Select("produto", "nome, imagem", "WHERE id = 21 AND masterid = 100");
if($q){
    $prod= mysqli_fetch_assoc($q);
    $thumb = $prod->imagem;
}

What do I do to fix it?

Notice: Trying to get Property of non-object in /home/.../.../public_html/sistema/.../.../.../header.php on line 6 [SOLVED THE PROBLEM ABOVE]

Another thing, guys.

Solved the above problem about Non-object, I want to pass the value of this variable:

$thumb = str_replace("../", "http://www.siteexemplohere.com/", ($prod['imagem']));

That is, the value of $Thumb, for the:

<meta property="og:image" content="<?php echo $thumb ? $thumb : " <?php echo $Raiz; ?>/assets/img/thumbs/default.png "; ?>"/>

because I want the user to click on the share button Facebook, the share box appears the product picture + product description (that’s quiet);

I followed the documentation and it doesn’t work... Thanks for the help you are giving me... Each hint an idea to formulate another solution of the problem

Remembering that when I give a var_dump(), he is picking up from the bank normally, I already var_dump $Thumb after being picked up from the bank and after being inserted in the "meta".

  • 1

    Pass the entire error code, this will show on which line exactly is the problem.

  • 1

    What the error indicates is that $prod is not an object and so can not do ->imagem. It is easy to confirm this by doing var_dump($prod); on the line before the error.

  • @Isac and how to fix it?

  • 2

    First, using the var_dump to know what we are dealing with. Then, see how it was implemented $db, to know what is the return of Select.

  • Tip: Can you replace <?php echo $variable ? > with <?=$variable ? >. Just enable short_tags in php.ini

  • Aaah yes this already tlg

  • Another thing, when I said you could change your question and add the code, I thought the error was in the same snippet, kkk

  • Ok, rsrs... But this problem Do you know how to solve? has some ideiw

  • 1

    After that instruction echo $Thumb ? $Thumb : has a mistake. The right is: <meta property="og:image" content="<?php echo $thumb ? $thumb : echo "{$Raiz}/assets/img/thumbs/default.png"; ?>"/>. You are already inside the PHP tag. No need to open another.

  • @eliangela For information purposes only, the tag <?= ?> is not a short tag, then you don’t need to enable it. It works by default since version 5.4. A short tag is just <? ?>; I commented on that in this answer.

  • I didn’t know that. Thank you, @Andersoncarloswoss

Show 6 more comments

2 answers

3


The problem is that the mysql_fetch_assoc returns an associative array. You should use with array:

while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

If you want to use with objects, you must use the mysql_fetch_object:

$result = mysql_query("select * from mytable");
while ($row = mysql_fetch_object($result)) {
    $imagem = $row->imagem;
}

About the second problem, after the echo instruction $Thumb ? $Thumb : has a mistake. The right is:

<meta property="og:image" content="<?php echo $thumb ? $thumb : "{$Raiz}/assets/img/thumbs/default.png"; ?>"/>

You are already inside the PHP tag. No need to open another.

  • Aaah understood, then it will have to be by the "fetch_assoc" same. I happen to pass this data with a function to return thumbnail. This information should go to the metatag og:image of facebook, to share understand? Using your suggestion this happens: Fatal error: Call to Undefined Function returnThumbnail() in /home/.../.../public_html/system/.../.../view/header.php on line 6

  • even so I already defined the function on the page, quiet... Problem is that always generates another error.

  • Please edit your question and put the code there so we can help you better

  • Can I do that? Like, change the scope of the question now by ignoring the answers entered here? or just add below?

  • You can add your code to your question upstairs.

2

I imagine the input 'image' should be accessed as $prod['imagem'], not as $prod->imagem since $Prod is an associative array in case of function success mysqli_fetch_assoc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.