0

I am trying to update only one column of MySQL table using the code below, but for some reason it always fails and gives me Invalid query error! What am I doing wrong here?

$query = "UPDATE dbTester SET username ='$MediaUserName' WHERE ID = '".$row['ID']."'"; 
$result2 = mysql_query( $con, $query ); 
echo $query; 
if (!$result2) {
   die('Invalid query: ' . mysql_error());
}

Error:

UPDATE dbTester SET username ='cindy' WHERE ID = '10796'Invalid query:

7
  • check the variables also dont concat query use pdo Commented Nov 25, 2013 at 6:00
  • @Roopendra Doesn't matter Commented Nov 25, 2013 at 6:01
  • @Roopendra That won't result in an error. Commented Nov 25, 2013 at 6:02
  • @Roopendra mysql default cast Commented Nov 25, 2013 at 6:03
  • thanks all for reply. If i copy and paste same query from echo to phpmyadmin the query updates the record successfully! so what might be the problem that it doesn't work in php ? Commented Nov 25, 2013 at 6:03

2 Answers 2

2

The error occurs because mysql_query expects first argument to be your query in string format and an optional second argument to be connection resource.

resource mysql_query ( string $query [, resource $link_identifier = NULL ] )
Sign up to request clarification or add additional context in comments.

2 Comments

could you tell me why(mysqli_affected_rows($result2) == 1){ echo "success";} is not working even i know that one row is updated?
@user1788736 Because you are confusing between mysql_* functions and mysqli_* ones.
0

you should use, mysql_query($query); instead of mysql_query( $con, $query );

Your syntax suits for mysqli_query($con, $query );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.