1

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

7
  • Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in --- on line 8 Warning: in_array() [function.in-array]: Wrong datatype for second argument in --- on line 47 Commented Jun 25, 2013 at 9:19
  • 1
    What are you trying to do with mysqli_fetch_array($con, $yourlike); ? see mysqli_fetch_array Commented Jun 25, 2013 at 9:20
  • what is in $postid ?? and from where the value is assigned Commented Jun 25, 2013 at 9:21
  • @Brewal I want to get all of the posts that I have "Liked" so that when they are shown I can choose to have either a Like or Unlike button depending on what I have already chosen Commented Jun 25, 2013 at 9:23
  • You have looped around all the returned records from the SQL. Then once you have finished you are trying to use mysqli_fetch_array to return another record Commented Jun 25, 2013 at 9:24

3 Answers 3

1

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))
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, this has cleared up the first error but what do I now do about if(in_array($likearray, $postid)) {?
I think that if(in_array($yourlike, $postid, true)) is batter
@user2516546 Post the error. Without knowing that we can't help you out.
I did - It's Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\--- on line 47
@user2516546 Ohh yeah it should be if(in_array($postid , $yourlike)). See edited answer also.
0

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)) {

Comments

0

$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);

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.