I am trying to send login information to this php script and update certain fields in the database if the username and password exists. But I'm getting an error with mysqli_fetch_array. There are a lot of similar questions here already and I know that the problem is with "$result"(when I print out the value of result it is 1), probably because of the query failure ! But when I execute just the query in mysql it updates the fields without any problem so I'm really clueless. Would really appreciate any help !
Thank you.
This is the php code :
if(isset($_POST) == true && empty($_POST) == false){
$latitude = $_POST['Latitude'];
$longitude = $_POST['Longitude'];
$timestamp = $_POST['Timestamp'];
$Password=$_POST['Password'];
$name=$_POST['userName'];
$dbname = "android_db";
$servername = "localhost";
$username = "root";
$password = "1234";
//connect to the database
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!$conn)
{
die("Connection Failed" . mysqli_error($conn));
}
$sql = "Update LocationTable SET Latitude='$latitude', Longitude='$longitude' WHERE name='$name' AND Password='$Password';";
$result = mysqli_query($conn, $sql);
echo"$result";
$response = array();
while($row = mysqli_fetch_array($result)){
$response = array("id"=>$row[0],"name"=>$row[1],"password"=>$row[2],"email"=>$row[3]);
}
echo json_encode(array("user_data"=>$response));
}
?>
This is the output:
1
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in
/opt/lampp/htdocs/friendFinder/login.php on line
28
{"user_data":[]}
These are the fields in the database table
Answer
if(isset($_POST) == true && empty($_POST) == false){
$latitude = $_POST['Latitude'];
$longitude = $_POST['Longitude'];
$timestamp = $_POST['Timestamp'];
$Password=$_POST['Password'];
$name=$_POST['userName'];
$dbname = "android_db";
$servername = "localhost";
$username = "root";
$password = "1234";
//connect to the database
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!$conn)
{
die("Connection Failed" . mysqli_error($conn));
}
$sql = "Update LocationTable SET Latitude='$latitude', Longitude='$longitude' WHERE name='$name' AND Password='$Password';";
$result1 = mysqli_query($conn, $sql); // update action
$result = mysqli_query($conn, "select *from LocationTable"); // fetch action
$response = array();
while($row = mysqli_fetch_array($result)){
$response = array("id"=>$row[0],"name"=>$row[1],"password"=>$row[2],"email"=>$row[3]);
}
echo json_encode(array("user_data"=>$response));
}
?>
No comments:
Post a Comment