I am trying to execute multiple queries in php using mySql. But I am not getting result . Please let me know what's wrong I'm doing in the code. I am completely new in php and mySql. This is my php code below :
<?php
//-----------------------------------------------------------------------------------
$host='localhost';
$uname='root';
$pwd='welcome';
$db='database name';
//-----------------------------------------------------------------------------------
$con=mysql_connect($host,$uname,$pwd) or die("Connection Failed");
mysql_select_db($db,$con) or die("database selection failed");
//-----------------------------------------------------------------------------------
$mobile = mysql_real_escape_string($_POST['Mobile']); # Secure the input!
$password = mysql_real_escape_string($_POST['Password']);
$time = date("D M d, Y G:i a");
//-----------------------------------------------------------------------------------
// echo $time;
$flag['code']=0;
$select="select * from Insert1 where Mobile = '$mobile' AND Password = '$password'";
$select . ="update table Insert1 set Time='$time' where Mobile='$mobile',Password='$password',Time is null";
$r=mysql_query($select,$con); # This will always return something "true"
if(mysql_num_rows($r) > 0) { # You want to count rows instead.
$flag['code']=1;
}
print(json_encode($flag));
mysql_close($con);
?>
See the documentation:
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.
mysql_query
doesn't support multiple queries at once, you need to call it (or, since it is obsolete and being removed from PHP, a modern replacement for it) separately for each query.
(Even if it did, you would need a semi-colon to separate each of your queries in your string).
try this
$select="select * from Insert1 where Mobile = '$mobile' AND Password = '$password'";
$select.=";";
$select . ="update table Insert1 set Time='$time' where Mobile='$mobile',Password='$password',Time is null";