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

0

I have other examples here but none worked for me

I have an instruction to pull an image from the database, but it is 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/system/.../.../.../header.php on line 6 [SOLVED THE PROBLEM ABOVE]

Another thing, guys.

Solved the above problem on 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:

<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 Facebook share button, the sharing box appears in the Product image + product description (that's cool);

I followed the documentation and it does not work ... Thanks for the help you are giving me ... Each tip an idea to formulate another problem solution

Remembering that when I give a var_dump (), it is taking the bank normally, I have already done the var_dump of the $ thumb after being taken from the bank and then inserted into the "meta".

    
asked by anonymous 04.07.2018 / 18:47

2 answers

3

The problem is that 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 should use mysql_fetch_object :

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

On the second problem, after the echo $ thumb? $ thumb: has an error. The right thing is:

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

You are already inside the PHP tag. You do not need to open another one.

    
04.07.2018 / 19:39
2

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

    
04.07.2018 / 19:38