0

I need to update mysql table, and get the number of tables updated using php, the code look like below,

    $query = "update ACCESSUSERS set ACTIVE='111' where UPPER(USERNAME)=UPPER('firstname') and PINNUMBER='7777'";
    mysqli_query($conn, $query);
    $numrows = mysql_affected_rows();
    printf("Records updated: %d\n", $numrows);

But the issue is the above print 0 always even if changed the value for ACTIVE with different one.

What can be the issue, any help will be appreciated.

Note: I have tested the above query from phpMyadmin and it's working, only problem while executing in a php.

Thanks,
Haris

arodriguezdonaire
  • 5,396
  • 1
  • 26
  • 50
Haris
  • 13,645
  • 12
  • 90
  • 121
  • RTM http://php.net/manual/en/function.mysql-affected-rows.php - `int mysql_affected_rows ([ resource $link_identifier = NULL ] )` and you're mixing APIs; *no wonder*. Here http://php.net/manual/en/mysqli.affected-rows.php – Funk Forty Niner Aug 04 '15 at 16:19

2 Answers2

2

You are using the mysqli_ API to make your database query.

You are using the obsolete mysql_ API to count the rows.

You can't switch APIs and expect them to interact with each other. Stick to mysqli_.

Use mysqli_affected_rows instead of mysql_affected_rows

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

You need to use the mysqli version of affected_rows. Which is

$numrows = mysqli_affected_rows($conn);
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
exussum
  • 18,275
  • 8
  • 32
  • 65
  • `num rows`? wrong function. edit: well... maybe. OP's using wrong terminology. but am pretty sure they want to use affected_rows here. It's an UPDATE and not a SELECT. – Funk Forty Niner Aug 04 '15 at 16:24
  • Ah yeah. Thanks for spotting – exussum Aug 04 '15 at 16:28
  • To add to @Fred-ii- the OP is also Clearly not using OOP so `mysqli_affected_rows($conn)` – SuperDJ Aug 04 '15 at 16:29
  • thing is though, they might be using a procedural method to connect with, while you're using an object oriented method. Just in case, mixing both methods isn't always good. – Funk Forty Niner Aug 04 '15 at 16:29