0
$stmt2 = $db->prepare("INSERT INTO friend(`uId`,`friendId`) VALUES (?,?), VALUES(?,?)");
$stmt2->bind_param('ssss', $userId,$friendId,$friendId,$userId);

I expect it would insert 2 rows with the result

uId friendId
1    2
2    1

but it returned

Call to a member function bind_param() on a non-object

Dharman
  • 30,962
  • 25
  • 85
  • 135
user3522457
  • 2,845
  • 6
  • 23
  • 24
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Mar 06 '20 at 19:49

2 Answers2

1

INSERT takes only one VALUES clause, even if the VALUES clause includes multiple tuples.

INSERT INTO friend(`uId`,`friendId`) VALUES (?,?), (?,?)

But more importantly, you should always check the return value of prepare() because it returns false if there's an error in your query. Of course you cannot call a the bind_param() method on a false value, because false is not an object with methods at all.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • I tried your format but how is the bind param look like? this $stmt2->bind_param('ssss', $userId,$friendId,$friendId,$userId); doesn't work – user3522457 May 08 '14 at 14:26
  • Did you check for errors as I suggested? What error was output? See an example here: http://php.net/manual/en/mysqli.error.php – Bill Karwin May 08 '14 at 14:27
  • yes I tried your answer but it return Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement – user3522457 May 08 '14 at 14:29
0

Try this

$a = '';
$b = '';

$stmt2 = $db->prepare("INSERT INTO friend(`uId`,`friendId`) VALUES (?,?)");
$stmt2->bind_param('ss', $a, $b);

$a = $userId;
$b = $friendId;
$stmt2->execute();

$a = $friendId;
$b = $userId;
$stmt2->execute();
Thiago França
  • 1,817
  • 1
  • 15
  • 20