mysql_affected_rows() always return 0 [duplicate] - php

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
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

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

You need to use the mysqli version of affected_rows. Which is
$numrows = mysqli_affected_rows($conn);

Related

Is mysql return data to php variable when error [duplicate]

This question already has answers here:
How to check if a MySQL query using the legacy API was successful?
(6 answers)
Closed 6 years ago.
so i created php variable contain mysql query inside , but is it will return data to the php variable when the table / column / data is not available ?
example :
$a1='1';
$item1nanowr = mysql_query("SELECT item_name FROM item_info WHERE item_special_number = $a1 ");
now just imagine that item_name column not available , is it will return data ?
and if i put "or die" statement like this :
$item1nanowr = mysql_query("SELECT item_name FROM item_info WHERE item_special_number = $a1 ") or die("Coming Soon");
is it will return word Coming Soon ?
or die() will run only if something goes wrong with the query. Several things could go wrong. Some of them are:
The database table does not exist
One of the specified columns does not exist
There is a syntax error in the query
The database is offline or the connection has been lost
So it will run on critical errors. It won't run if your table or a specific column is empty.
Also it is very important to stop using mysql_ functions!. The mysql extension is deprecated from PHP 5.5 and completely removed from PHP 7.
Use mysqli or PDO instead. You should also use Prepared Statements instead of directly interpolating variables in your query. More on Prepared Statements here

PHP MYSQL Select Last Inserted ID [duplicate]

This question already has answers here:
How do I get the last inserted ID of a MySQL table in PHP?
(16 answers)
Closed 6 years ago.
I tried to find an answer on here already, as well as read the PHP doc on it, however it doesn't explain it very well.
I'm just trying to get the last ID inserted by the user. I know MYSQL is deprecated, but I need to do it this way.
This is what I have so far.
$query="INSERT INTO teams (team_name)
VALUES
('$team_name')";
mysql_query($query) or die('error');
$team_id = mysql_query("SELECT LAST_INSERT_ID()");
Thanks!
Take a look at mysql_insert_id
But beware of the issues when using ON DUPLICATE KEY UPDATE statements. It is described in the comments on the php doc page.
But your solution should work, too.
Also, if you're using mysql, don't forget to mysql_real_escape your input, $team_name in your case.
[insert obligatory mysql is deprecated warning here ;)]
You can do it by PHP. You don't need a query: mysql_insert_id()

How to check if mysqli_query deleted any rows [duplicate]

This question already has answers here:
How to test if a MySQL query was successful in modifying database table data?
(5 answers)
Closed 1 year ago.
I created function that delete some data from my database. This is part of it:
mysqli_query($con, "DELETE FROM appsdata WHERE ownerID=123");
But I want to check how many rows were deleted. Mysql_query returns always true, no matter if something was deleted :/
You can use mysqli_affected_rows:
mysqli_affected_rows($mysqli_link);
or
$mysqli->affected_rows
http://php.net/manual/en/mysqli.affected-rows.php

PHP multiple MYSQL commands in one mysql_query() query [duplicate]

This question already has answers here:
How to execute two mysql queries as one in PHP/MYSQL?
(8 answers)
Closed 8 years ago.
I want to issue multiple mysql commands with one mysql_query function.
This is my code:
$query .= "INSERT INTO `users` VALUES(1,'stack','overflow');";
$query .= "INSERT INTO `posts` VALUES('other','stack','overflow');";
mysql_query($query);
If I do that I get a warning that my syntax would be incorrect.
If I echo the output, copy it and execute it in phpMyAdmin it works.
Where is the error there?
$query = "INSERT INTO `users` VALUES (1,'stack','overflow'), ('other','stack','overflow');";
mysql_query($query);
PHP does not support sending more than one query at a time via mysql_query, but you can achieve your result in a single one by using the above.
I think you need this?? http://us2.php.net/manual/en/mysqli.multi-query.php
according to http://www.php.net/manual/en/function.mysql-query mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.
but this guy said that you just have to pass flag 65536 as mysql_connect's 5 parameter
http://www.php.net/manual/en/function.mysql-query.php#91669

how to get data after sql inerst command in php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there a way to return the id of a row that was just created in MySQL with PHP?
hi, i have an inset command such as
insert (name) values($name);
where i have id column as outoincrement how can i get the id of the inserted record after inserting directly
If you are working with mysql_* function, you'll have to use mysql_insert_id() (quoting) :
Retrieves the ID generated for an
AUTO_INCREMENT column by the
previous query (usually INSERT).
With mysqli, you'll use mysqli::insert_id().
And, with PDO, you'll call PDO::lastInsertId().
mysql_query("Your query here");
$last_id = mysql_insert_id();

Categories