2

I am trying to write mysql query with php prepared query.

First, the query

    $query="delete from friends  where UserA=?, UserB=? 
    or (UserB=?, UserA=?)";

I want to delete friends entry from database. The entry can be in any order. Like, a friends relation can be entered as UserA to be friend of UserB or UserB to be friend of UserA. But there is only one entry anyway. So to check make sure the entry is deleted I am trying to find and delete it in any of both possible cases.

Second,
I am confused with passing the parameters to the above sql query. The parameters in the above query are four. So I am guessing to pass four parameters. But I think if i do it $stmt->bind_param('ii',$userAid, $userBid) this should work???

$stmt=$mysqli->stmt_init();
$stmt->prepare($query);
$stmt->bind_param('iiii',$userAid, $userBid, $userAid, $userBid);
$stmt->execute(); 
5
  • just edit your query: $query="delete from friends where (UserA=? AND UserB=?) OR (UserB=? AND UserA=?)";. you should use "AND", not ",". the rest seems fine Commented May 11, 2015 at 10:56
  • @CaptainCrunch and The number of parameters? two or four? Commented May 11, 2015 at 10:59
  • four parameters. one for each "?" Commented May 11, 2015 at 11:01
  • @CaptainCrunch if you think your comment is an answer then when don't you answer instead commenting? Commented May 11, 2015 at 11:06
  • i wasn't sure what you were asking, so i commented. thnx for you're comment, i've summed it all up to the answer bellow. please let me know if it lacks anything. Commented May 11, 2015 at 11:14

1 Answer 1

1

Lets sum it all up:

/* fixed the query */
$query="delete from friends where (UserA=? AND UserB=?) OR (UserB=? AND UserA=?)";

/* invoke the query */
$stmt=$mysqli->stmt_init();
$stmt->prepare($query);
// (1 parameter for each "?" )
$stmt->bind_param('iiii',$userAid, $userBid, $userAid, $userBid);
$stmt->execute(); 
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.