user2516546
user2516546

Reputation: 111

Finding result in a PHP Array

I'm trying to find out all of my "Likes" from a mysql table and to put them into an array but I'm really not sure if I'm doing it right as I keep getting a "Wrong Datatype" error. Here is my code:

<?php
$check_like_sql = "SELECT * FROM posts WHERE type = 'Like' && poster = '$yourid'";
$check_like_res = mysqli_query($con, $check_like_sql) or die (mysqli_error());
if(mysqli_affected_rows($con)>0){
    while($likes = mysqli_fetch_assoc($check_like_res)){
        $yourlike = $likes['media'];    
    }
    $likearray = mysqli_fetch_array($con, $yourlike); 
}
?>
<?php
if(in_array($likearray, $postid)) {
            $likethis = "<a href=\"php/unlike.php?poster=$yourid&post=$postid\">Unlike</a> . ";
        }
        else if($posttype == "Like"){
            $likethis = "";
        }
        else{
            $likethis = "<a href=\"php/like.php?poster=$yourid&lat=$yourlat&lon=$yourlon&like=$postid&user=$postusername\">Like</a> . ";
        }
?>

Could anyone please explain where there might be an error? I'm very new to this sort of php coding. Thanks

Upvotes: 1

Views: 114

Answers (3)

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

You are overriding $yourlike so first initialise it. Also there is no need for mysqli_fetch_array remove that part.

$yourlike = array();
if(mysqli_affected_rows($con)>0){
    while($likes = mysqli_fetch_assoc($check_like_res)){
        $yourlike[] = $likes['media'];    
    }
    //$likearray = mysqli_fetch_array($con, $yourlike);  // remove this part
}

Edit

Change this

if(in_array($likearray, $postid))

to

if(in_array($postid , $yourlike))

Upvotes: 1

Jahanzeb
Jahanzeb

Reputation: 613

$yourlike is not mysqli_result.

Replace this line

$likearray = mysqli_fetch_array($con, $yourlike);

with this one

$likearray = mysqli_fetch_array($con, $check_like_res);

Upvotes: 0

user2361529
user2361529

Reputation: 11

you are using mysqli_fetch_array incorrecty. The first argument is the result the second is the type to return

$result = mysqli_fetch_array($query_result,MYSQLI_ASSOC);

however it looks like you are using it incorrectlyu as you have already fetched an assoc, can you not just delete the line $likearray = mysqli_fetch_array($con, $yourlike);

and change

$yourlike = $likes['media'];

to

$yourlike[] = $likes['media'];

and

if(in_array($likearray, $postid)) {

to

if(in_array($yourlike, $postid)) {

Upvotes: 0

Related Questions