I use this code for update a record in mysql
This is my code:
<?php
$query = "update seeds set status='success' where id = $x";
$result = mysql_query($query);
if(!$result){
echo 'failed'.$result;
print_r($result);
}else{
echo 'successfully';
}
?>
always successfully printed out.
But when the server is crowded, the 'failed' string is printed out without any value for 'result' variable.
This query always work correctly but sometime returns NULL.
How can I fix this?
there is a issue.
i use transaction with this query.
in fact my actual code is this:
<?php
.
.
.
$QUERY_TRANSACTION = mysql_query('START TRANSACTION');
if(!$QUERY_TRANSACTION){
echo "error 101" ;
exit;
}
$seed_query = "INSERT INTO seeds VALUES('','$username','$theTime','در انتظار')";
$seed_result = mysql_query($seed_query);
if(mysql_affected_rows()!=1){
$QUERY_TROLLBACK = mysql_query('ROLLBACK');
echo "error 102";
exit;
}
$seed_id = mysql_insert_id();
$_SESSION['SEED_ID'] = $seed_id;
$query_values = "(NULL,'$list_number[0]',0,$seed_id)";
for($i=1;$i<count($list_number);$i++){
$query_values .= ",(NULL,'$list_number[$i]',0,$seed_id)";
}
$r_query = "INSERT INTO rc_members VALUES $query_values";
$r_result = mysql_query($r_query);
if(mysql_affected_rows()<=0){
$QUERY_TROLLBACK = mysql_query('ROLLBACK');
echo "error 103";
exit;
}
$QUERY_COMMIT = mysql_query('COMMIT');
//--------------
$QUERY_TRANSACTION = mysql_query('START TRANSACTION');
if(!$QUERY_TRANSACTION){
echo "error 104" ;
exit;
}
$s_status = the_process($list_number,$m,$seed_id);
if($s_status == 'successful_proc'){
$s_query = "UPDATE seeds SET status='انجام شده' WHERE id=$seed_id";
$s_result = mysql_query($s_query);
//----------------logg file
$filename = "debug.txt" ;
$filepointer =fopen($filename ,"w") ;
fwrite($filepointer , print_r($s_result,true)) ;
fclose($filepointer) ;
//-----------------------
if(!$s_result){
echo "error 105".$s_result;
exit;
}
$QUERY_COMMIT = mysql_query('COMMIT');
echo 'successfuly process';
}else{
$QUERY_TROLLBACK = mysql_query('ROLLBACK');
echo 'error 106';
exit;
}
$QUERY_COMMIT = mysql_query('COMMIT');
?>
that 'successfully' always printed out. but sometime 'error 105' printed out.
may be transaction is the reason of this error?
that update in db is performed but return null?
If mysql_query returned NULL, then that would be a bug on PHP. How do you know that it is actually returning NULL?
For update statements mysql_query should only return TRUE or FALSE. So your error checking code is fine. As to finding out what went wrong, you will have to call other function - mysql_error() would give you a blurb about what went wrong. So print the value of mysql_error() inside your false block. Like this:
echo 'failed. SQL Err: '. mysql_error()
Do that and you will probably get a clue to as to how 'record got updated, but return value is false'. It should not have happened.
$result in this case is just a resource, it does not contain the reason.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc,
mysql_query() returns TRUE on success or FALSE on error.
Source: PHP Manual
So you can only get TRUE / FALSE from it in your update query. It won't tell you the reason. But as you said it happens when your server is overcrowded so reason probably is "too many connections"
Related
Trying to update a record using PHP and PDO statements.
The query fires with no errors, and the console reads the update was successful, but there is no change in the table.
So confused as to why this is happening:
<?php
include("../include/sessions.php");
if(isset($_POST['editcriteria']))
{
$value = $_POST['editcriteria'];
$editUID = $value['editUID'];
$editAddDelete = $value['editAddDelete'];
$editeffectiveDate = $value['editeffectiveDate'];
try
{
$update = $conn->prepare("UPDATE primary_vehicle_data SET `add_delete` = :eadddelete,
`effective_date` = :eeffectivedate WHERE `uid` = :euid");
$update->execute([
'eadddelete' => $editAddDelete,
'eeffectivedate' => $editeffectiveDate,
'euid' => $editUID
]);
if($update)
{
echo "Success: Record Updated";
}
else
{
echo "Error: The Vehcile was not updated.";
}
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
}
?>
I simplified the above code as much as possible. There were several more parameters, but when I removed the parameters and left it with the 3 parameters above, I still get "Success: Record Updated". But the table is literally unaffected.
Why is this happening and how do I fix it?
* UPDATE *
I already confirmed the connection to the database is good. I'm lost.
$update is a PDOStatement object, so when you test
if ($update)
it will always succeed as a PDOStatement object is equivalent to true.
You should be:
checking the result of $update->execute e.g.
if ($update->execute([ /* params */])) {
checking the value in $update->rowCount, which will tell you if any rows were affected by the query.
Data Updated Or Not , It is showing Sorry not inserted. Please check it out. Thank You
<?php
if(isset($_POST['submit'])){
include 'connection.php';
$id = $_POST["id"];
$company = $_POST["company"];
$model = $_POST["model"];
$price = $_POST["price"];
$engno = $_POST["engno"];
$query = "UPDATE products SET company='".$company."', model='".$model."',`price`='".$price."', `engno`=$engno WHERE `id`=$id";
$result=mysqli_query($conn,$query);
if($conn->query($result)===TRUE){
echo 'Data Updated Successfully';
}
else{
echo 'Sorry Not Updated';
}
mysqli_close($conn);
}
?>
Check success status like below, mysqli_query will return true, you are trying to run query again $conn->query($result) where $result have either TRUE or FALSE.
if($result){
echo 'Data Updated Successfully';
}
For mysqli_query
Return Value: For successful SELECT, SHOW, DESCRIBE, or EXPLAIN
queries it will return a mysqli_result object. For other successful
queries it will return TRUE. FALSE on failure
From w3school
This Update Query seems to work inside MySQL WorkBench but when applying it to my php application doesn't want to work... Both the $TotalSeats & $PerfID parameters have been tested and they output the desired number.
Is this a minor syntax error or am I missing a trick here?
$deductSeats = "UPDATE perf SET Seats=(SELECT SUM(Seats -'$TotalSeats')) WHERE PerfID = '$PerfID'";
if (mysqli_query($conn,$deductSeats))
{
echo 'Query Worked!<br>';
}
else
{
echo 'Query Didnt Work<br>';
}
$deductSeats = "deductSeats(`$TotalSeats`,`$PerfID`)";
You can try :
$deductSeats = "UPDATE perf SET `Seats` = `Seats` - ".$TotalSeats." WHERE PerfID = ".$PerfID;
if (mysqli_query($conn,$deductSeats)) {
echo 'Query Worked!<br>';
} else {
echo 'Query Didnt Work<br>';
}
For some reason, the while loop below never fires.
All of this is inside a class.
$code = $this->get_postal_state_no('Western Cape');
private function get_postal_state_no($psn)
{
$sql = "
SELECT
no
FROM
ct_state
WHERE
name LIKE('".$psn."');";
$stmt = sqlsrv_query($this->conn1, $sql);
// This statement is not false, so the error handling does not happen, this is expected.
if($stmt === FALSE)
{
if(($errors = sqlsrv_errors()) != NULL)
{
foreach($errors as $error)
{
$sqlstate = $error['SQLSTATE'];
$code = $error['code'];
$sqlmessage = $error['message'];
}
}
$msg = 'Error in $stmt in get_postal_state_no() method.';
$this->do_error_log($error_msg, $sqlstate, $code, $sqlmessage, $msg, __LINE__, __FILE__, __FUNCTION__, __CLASS__, __METHOD__);
sqlsrv_free_stmt($stmt);
}
//This loop is never entered. This is not expected.
while($obj = sqlsrv_fetch_object($stmt))
{
echo "I am here now";
break;
if(!empty($obj->no) && $obj->no != '')
{
echo "Hello, I exist";
break;
// This break never happens
return $obj->no;
}
else
{
echo "Hello, I don't exist";
break;
// This break never happens
$code = $this->sp_aa_iud_ct_state($psn);
return $code;
}
sqlsrv_free_stmt($stmt);
}
}
Does anyone have an idea why? I am using the php_sqlserv driver. The SQL server profiler shopws the query for the first part executing.
Thanks
J
Looking at the documentation page for sqlsrv_fetch_object it says this about the return value:
Returns an object on success, NULL if there are no more rows to return, and FALSE if an error occurs or if the specified class does not exist.
I suspect the function is either returning NULL or FALSE, though the reasons are a little unclear. Try adding this above the while loop and see what the output is:
$obj = sqlsrv_fetch_object($stmt)
var_dump($obj);
die;
while($obj = sqlsrv_fetch_object($stmt))
{ // ...
Another helpful debugging trick is to dump out the query and then copy/paste it into your DBMS system and run the query you are executing in code directly against your database to see if any results are actually being returned:
$sql = "
SELECT
no
FROM
ct_state
WHERE
name LIKE('".$psn."');";
var_dump($sql);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
update in mysql_query sometime return null
I use update in mysql_query in my PHP code. On localhost (in xampp) my code works correctly, but on the production host, the code often works correctly but sometime returns no result for update query.
If the update failed, it must return 'error' or '0' but it does not return any result. Why?
This is my code:
<?php
.
.
.
$QUERY_TRANSACTION = mysql_query('START TRANSACTION');
if(!$QUERY_TRANSACTION){
echo "error 101" ;
exit;
}
$seed_query = "INSERT INTO seeds VALUES('','$username','$theTime','در انتظار')";
$seed_result = mysql_query($seed_query);
if(mysql_affected_rows()!=1){
$QUERY_TROLLBACK = mysql_query('ROLLBACK');
echo "error 102";
exit;
}
$seed_id = mysql_insert_id();
$_SESSION['SEED_ID'] = $seed_id;
$query_values = "(NULL,'$list_number[0]',0,$seed_id)";
for($i=1;$i<count($list_number);$i++){
$query_values .= ",(NULL,'$list_number[$i]',0,$seed_id)";
}
$r_query = "INSERT INTO rc_members VALUES $query_values";
$r_result = mysql_query($r_query);
if(mysql_affected_rows()<=0){
$QUERY_TROLLBACK = mysql_query('ROLLBACK');
echo "error 103";
exit;
}
$QUERY_COMMIT = mysql_query('COMMIT');
//--------------
$QUERY_TRANSACTION = mysql_query('START TRANSACTION');
if(!$QUERY_TRANSACTION){
echo "error 104" ;
exit;
}
$s_status = the_process($list_number,$m,$seed_id);
if($s_status == 'successful_proc'){
$s_query = "UPDATE seeds SET status='انجام شده' WHERE id=$seed_id";
$s_result = mysql_query($s_query);
//----------------logg file
$filename = "debug.txt" ;
$filepointer =fopen($filename ,"w") ;
fwrite($filepointer , print_r($s_result,true)) ;
fclose($filepointer) ;
//-----------------------
if(!$s_result){
echo "error 105".$s_result;
exit;
}
$QUERY_COMMIT = mysql_query('COMMIT');
echo 'successfuly process';
}else{
$QUERY_TROLLBACK = mysql_query('ROLLBACK');
echo 'error 106';
exit;
}
$QUERY_COMMIT = mysql_query('COMMIT');
?>
always "sucsessfully process" printed.but sometime "error 105" printed without any value for $s_result;
i try to save the $s_result in file and see the no result save in file.
in other word the $s_result is null.
thank's
The problem that MySql returns false in case of no record effected or error
You can use mysql_affected_rows() after the query instead of
if(!$s_result){
echo "error 105".$s_result;
exit;
}
you can use
if(mysql_affected_rows($s_result) == -1){
echo "error 105".$s_result;
exit;
}
link for manual of the function :
mysql_affected_rows