Peppifg Peppifg - 1 year ago 65
MySQL Question

After locking the table, I get warning

This is my code

$ar = ($_POST['ar']);
$query = "lock table stock write";
$res = mysqli_query ($db, $query);

$query = "SELECT SUM(quantita_vendita) AS somma FROM stock";
$res = mysqli_query ($db, $query);
$row = mysqli_fetch_array($res);
$somma = $row['somma'];

$query = "SELECT * FROM user, info WHERE info.id_user = and = '{$_SESSION['email']}'";
$res = mysqli_query ($db, $query);

if(mysqli_num_rows($res) > 0) {
$row = mysqli_fetch_assoc($res);
$azioni = $row['azioni'];
$denaro = $row['denaro'];
$id = $row['id'];

If you do not insert the lock , it works.
Instead when I insert the lock , I have a Warning (mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in..) for the
if(mysqli_num_rows($res) > 0)

Answer Source

When you use table locking, you have to lock ALL tables you will use in any query in that session (or unlock them again), see LOCK TABLES and UNLOCK TABLES Syntax

A session that requires locks must acquire all the locks that it needs in a single LOCK TABLES statement. While the locks thus obtained are held, the session can access only the locked tables. For example, in the following sequence of statements, an error occurs for the attempt to access t2 because it was not locked in the LOCK TABLES statement:

mysql> SELECT COUNT(*) FROM t1;
| COUNT(*) |
|        3 |
mysql> SELECT COUNT(*) FROM t2;
ERROR 1100 (HY000): Table 't2' was not locked with LOCK TABLES

So in your case, the query using table user and info failed. To solve that, use e.g.

lock tables stock write, user write, info write;

You got your error-message, because the query failed, and thus $res is false and not an mysqli_result-object, and therefore mysqli_num_rows($res) doesn't work. You should always check if a query failed before using the result, e.g. use

if ($res = mysqli_query($db, $query)) {
   if (mysqli_num_rows($res) > 0) {... }
} else {
   printf("Error: %s\n", mysqli_error($db));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download