3
$sql = "UPDATE user SET password = '$new_password' WHERE temp_url = '$temp_code';";

$sql = "UPDATE user SET temp_url = '' WHERE user_name = '$user_name_frmdb';";

How can I run the two update queries in one line?

erikvimz
  • 5,256
  • 6
  • 44
  • 60
sanso13527
  • 33
  • 2
  • 7
  • 1
    Do they both work on the same row even though the where clause is different? – Hanky Panky Dec 20 '14 at 11:01
  • 1
    According to newfurniturey this is not supported: http://stackoverflow.com/a/12830973/381802 – erikvimz Dec 20 '14 at 11:01
  • 1
    @cxminer thats not entirely applicable to this question. For the old `mysql_*` api yes its not supported by mysqli supports mutliple queries. http://php.net/manual/en/mysqli.multi-query.php and the OP didnt mention which api they use. So it is supported and possible. – Hanky Panky Dec 20 '14 at 11:03
  • i hope Mysqli will support but i don`t know how to write that – sanso13527 Dec 20 '14 at 11:06
  • I appologize, Hanky is right, it is possible using mysqli_multi_query(), please refer to this page: http://stackoverflow.com/questions/22549284/updating-multiple-mysql-records-using-one-mysqli-query-call-in-php – erikvimz Dec 20 '14 at 11:07
  • Why do you want to? Which mysql query library are you using? And, as @Hanky웃Panky asks, is this the same row you're working on? – Matt Gibson Dec 20 '14 at 11:08
  • @sanso13527 just accept my answer if it was helpful – Avinash Babu Dec 20 '14 at 11:13
  • It's a bit unclear whether you want to combine the two queries into a single SQL query, or to have a way to run the two queries as two queries but atomically. Could you clarify whether you are affecting a single row with the two queries? – Tony Dec 20 '14 at 11:13
  • you can connect to database using mysqli to run this query, Mysqli extention should be installed to make it working $con=mysqli_connect("localhost","my_user","my_password","my_db"); – Mike Clark Dec 20 '14 at 11:35
  • @ Avinash Babu sorry its not working :( – sanso13527 Dec 20 '14 at 11:38

1 Answers1

2

Using Mysqli extention you can implement, you can run multiple queries using mysqli_multi_query:

$sql = "UPDATE user SET password = '$new_password' WHERE temp_url = '$temp_code';";
$sql .= "UPDATE user SET temp_url = '' WHERE user_name = '$user_name_frmdb';";

// Execute multi query
if (mysqli_multi_query($con, $sql)) {
    // Query successful
}
erikvimz
  • 5,256
  • 6
  • 44
  • 60
Mike Clark
  • 1,860
  • 14
  • 21
  • Separate the queries with a semi-colon `;` and fix the syntax errors – Hanky Panky Dec 20 '14 at 11:10
  • i have updated the sql string, you need to use that, make sure you have installed mysqli & connection is running on mysqli – Mike Clark Dec 20 '14 at 11:13
  • i tryed $new_password = $_POST['Password']; $sql="Update user SET password = '$new_password' WHERE temp_url='$temp_code'"; $sql.="Update user SET temp_url = '' WHERE user_name='$user_name_frmdb'"; if (mysqli_multi_query($conDB,$sql)) { echo "ok"; } But its not working – sanso13527 Dec 20 '14 at 11:18
  • @Hanky could you approve my edit so we can get rid of syntax errors and so the semi colon is shown – erikvimz Dec 20 '14 at 11:20
  • add semi colon after first query within mysql string $sql="Update user SET password = '$new_password' WHERE temp_url='$temp_code' ; "; $sql.="Update user SET temp_url = '' WHERE user_name='$user_name_frmdb'"; – Mike Clark Dec 20 '14 at 11:20