PHP update query successful but database not updating - php

I am trying to update a record in my database and SQL query return successful but my database is not changing.

representative_id = '$login_session'
Is the representative_id column in your table a VARCHAR, or an INT? You're passing a string to MySQL, there. If the column in your DB is stored as an INT, try removing the single quotes around $login_session.
When posting questions relating to database queries, it helps to also include your table schema.

Seems like you're using PDO (hopefully)
When you're using this Data Objects, when a query is executed it will return TRUE, but it doesn't care if it changed something or not, it will return true when there is not a critical MYSQL Error.
What you want to do is to know IF A ROW AS AFFECTED / CHANGED by that query, in that case instead of using;
$PDOObject->query($thequery) == true
you should use:
$result = $PDOObject->query($thequery);
if($result->rowCount() > 0) ...
More info about how to know if a row was affected by a query with PDO
http://php.net/manual/en/pdostatement.rowcount.php

sql query return successful but my database is not changing
Are you sure that there exists at least one record that matches your filter criteria WHERE full_name = '$full_name' AND representative_id = '$login_session'. If no record updating means no any record matching the WHERE condition and so no updates occurring.

Related

Avoid updating a same value in MYSQL and displaying the error in the client side

I have come across many questions and answers related to this kind of questions, but not for exactly this question.
Well, according to the official MYSQL documentation it says
If you set a column to the value it currently has, MySQL notices this
and does not update it.
But practically, when I update a same value to a MYSQL column, the query successfully executing and displaying "Your query has been successfully executed ".
So How do I exactly know that the query hasn't updated, when I update a column with the same value?
Also Is it possible to get an error message to the client (browser), when I try to update a same value, from the browser via a submit form to the MYSQL server via a backend language like PHP?
When updating a Mysql table with identical values nothing's really affected so rowCount will return 0.
Just create your PDO object with
<?php
$p = new PDO($dsn, $user, $password, array(PDO::MYSQL_ATTR_FOUND_ROWS => true));
?>
and rowCount() will tell you how many rows your update-query actually found/matched.
Check this link also.
you may use like to avoid updating same value
UPDATE users
SET first_name = 'Michael'
WHERE id = 123
AND first_name IS DISTINCT FROM 'Michael';

MySQL MULTIPLE INSERT get rows that fail?

I'm a beginner with PDO and MySQL, so here's my question :
How can I be sure that when performing an INSERT .. SELECT (or MULTIPLE INSERT)
all data will be inserted in database?
I know there is a rowCount() function but the number of rows inserted is dynamic.
The only way I see would be to make a SELECT count(*), and then compare it with the rowCount(), but I'm not sure I'm doing it the right way.
And if not all data were inserted, is it possible to get rows which didn't work?
Also, is it possible that a SELECT query fails and retrieves only a few part of the data? (ex : It must retrieve 1000 rows but due to some failure, it retrieves 700) Or it's all or nothing?
Thanks for the help.
How can I be sure that when performing an INSERT .. SELECT (or MULTIPLE INSERT) all data will be inserted in database?
You should tell PDO to throw an exception in case of error and thus there will be a PHP error in case of a failed query.
I know there is a rowCount() function
Row count has nothing to do in your case.
And if not all data were inserted, is it possible to get rows which didn't work?
It is advised to redo all the successful yet queries instead. To do so you have to wrap your inserts in a transaction.
However, if you want to keep alll the previous inserts in place, you may wrap execute call in a try and catch operator and do whatever workaround inside.
Also, is it possible that a SELECT query fails and retrieves only a few part of the data?
No.

PHP or MYSQL not recognizing value

I'm using php to query from mysql in which either the home_team or away_team column contains a given value. I know that records of such values exist in the DB. But, for some reason either PHP or MySQL is telling me that it's not. My code is as follows.
$teams = array("St. Louis",
"NY Yankees",
"NY Mets",
"LA Dodgers",
"LA Angels");
foreach($teams as $given_team)
{
$query = mysql_query("SELECT COUNT(*) FROM current_season_games WHERE home_team = '".$given_team."' OR away_team = '".$given_team."'")or die(mysql_error());
$count = mysql_result($query, 0);
echo "".$count."<br />";
}
I know that the $count variable that's being echoed is incorrect. I'm looking at some rows in the DB that fit the conditions specified in the query.
Any ideas?
Thanks,
Lance
You are using COUNT(*). This won't give you the value of the field. COUNT(*) is usually used to measure how many results are returned.
What you'll want to do is actually select the column name or alternatively, select the entire row that matches the criteria. Something like this -
SELECT * FROM current_season_games
WHERE home_team = '".$given_team."' OR away_team = '".$given_team."'")
Here are a couple of things to check:
is PHP connecting to the same host and database? (This has been a 'DOH!' moment for some developers, when they have multiple databases... one local, and one on a test server.) Are you sure you are querying the same table?
try testing a simpler query, e.g. SELECT COUNT(*) FROM current_season_games to see if you can get any non-zero count returned
try putting the query into a string, and echoing out the string, before you execute it
is this an InnoDB table, and have the rows you are trying to count been committed? (Did the session adding the rows do a START TRANSACTION, insert the rows, and then not yet commit them? (This is a DOH! moment for some developers that are using multiple environments (e.g. SQLyog and a web server). (A separate session with transaction isolation level of REPEATABLE READ would not see the uncommitted rows)
is that column in the database using a case sensitive collation, where the equality comparison in the predicates (WHERE clause) is being handled as case sensitive vs. case insensitive?
None of those may solve your problem, but it's impossible to tell (from here), without a lot more information, what the issue is. But it's a few things you can check.
In old code that I come across still using the deprecated mysql_* functions, I'll usually come across the use of the mysql_num_rows() function.
Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().
I would recommend changing your code to something like this -
$result = mysql_query("SELECT COUNT(*) FROM current_season_games WHERE home_team = '".$given_team."' OR away_team = '".$given_team."'")or die(mysql_error());
$count = mysql_num_rows($result);
Notice that the mysql_query() function doesn't return a $query as your variable name indicates. It returns a result set that you can then inspect to retrieve that actual data.

MySQLi Prepared Statement not executing

The following code runs without any errors but doesn't actually delete anything:
$update = $mysqli->prepare('DELETE FROM table WHERE RetailerID = ? AND Amount = ? AND FXRate = ?');
$update->bind_param('iii', $rID, $base_value, $fx_rate);
$update->execute();
$update->close();
I have numerous mysqli prepared statments in this same file that execute fine, but this one is the only one that doesn't modify the table. No errors or shown, but the row isn't deleted from the table either. I have verified that $rID, $base_value, and $fx_rate are the correct values, and a row is DEFINITELY present in table that matches those values.
The only difference between this statement and the others are the parameters and the fact that it's DELETE instead of SELECT or UPDATE. I also tried doing a SELECT or UPDATE instead of DELETE using the same WHERE parameters, but no luck. The issue seems to be that it's not finding a row that fits the WHERE parameters, but like I said, the row is definitely there.
Any ideas?
Is amount an integer or a double? You're converting to integer ('iii'), but I presume it'll be $0.34 or similar. Try 'idi' instead.
Edit: same applies for rate - is that an integer or double too?

How to know if rows are deleted using mysqli dbdriver

I am using codeigniter 2.1.0 with mysqli dbdriver.
I have the below query and would like to know when it is successful
$query = $this->db->query("delete FROM abc WHERE id IN (" . $IdList . ")");
How can I know if the related rows are deleted?
$this->db->affected_rows() is what you are looking for. You should be able to compare that result with the expected number of deleted rows.
Also quoting the USER GUIDE
Displays the number of affected rows, when doing "write" type queries (insert, update, etc.).
Note: In MySQL "DELETE FROM TABLE" returns 0 affected rows. The
database class has a small hack that allows it to return the correct
number of affected rows. By default this hack is enabled but it can be
turned off in the database driver file.
From the CodeIgniter User Guide:
The query() function returns a database result object when "read" type queries are run, which you can use to show your results. When "write" type queries are run it simply returns TRUE or FALSE depending on success or failure.
Therefore your $query variable will be TRUE on success or FALSE on failure.

Categories