PHP code for obtaining data from the MYSQL server from the mobile application

My PHP code gets data from the mysql server to show which is suitable for the website, but I want to use the same method to make a mobile application,
is it suitable for a mobile application? Because I use (code below) under the ajax call.

I can also use json here because I need help.

 include('connect.php');
$filmId=$_GET['filmId'];
$sql ="SELECT *, DATE_FORMAT(filmReleaseDate,'%d% b %Y') filmReleaseDate FROM films where filmId ='$filmId'";
mysqli_query("SET NAMES utf8");
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result))
{
$id=$row['filmId'];
$name=$row['filmName'];
$year=$row['filmYear'];

echo "$name, $year";

}
mysql_free_result($result);
mysqli_close($conn);
?>

This is the ajax code, when I click href, it will process the above php request to get the data.

var xmlhttp = new XMLHttpRequest();
var url = "./searchfilmsbyname.php";
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
}
xmlhttp.open("POST", url, true);
xmlhttp.send();

function my Function(response) {
var arr = JSON.parse(response);
var i;
var out = "";
for (i = 0; i out += "
  • " + arr[i].Film + "< /a>
  • ";
    if ((i + 1) == arr.length) {
    document.getElementById("searchfilmsbyname").innerHTML = out;
    $( "#searchfilmsbyname").listview('refresh');
    }
    }
    }

    I’m not sure how it works on your website, maybe you include it instead of using ajax, but the problem with the code above is that you didn’t send back valid json so:

    < /p>

    function myFunction(response) {
    var arr = JSON.parse(response);
    ...

    will fail, Because:

    $id=$row['filmId'];
    $name=$row['filmName'];
    $year=$ row['filmYear'];

    // this does not generate valid json:
    echo "$name, $year";

    What you should do is change Add all rows to an array, and only output the array encoded as json at the end:

    $rows = array();
    while($row= mysqli_fetch_array($result))
    {
    $rows[] = array('id' => $row['filmId'],
    'name' => $row['filmName '],
    'year' => $row['filmYear']);
    }
    echo json_encode($rows);

    Otherwise you have A SQL injection problem. You should use prepared statements instead of injecting variables directly into the query.

    My PHP code gets data from the mysql server to show which is suitable for the website , But I want to use the same method to make mobile applications,
    is it suitable for mobile applications? Because I use (code below) under the ajax call.

    I can also use json here because I need help.

     include('connect.php');
    $filmId=$_GET['filmId'];
    $sql ="SELECT *, DATE_FORMAT(filmReleaseDate,'%d% b %Y') filmReleaseDate FROM films where filmId ='$filmId'";
    mysqli_query("SET NAMES utf8");
    $result = mysqli_query($conn, $sql);
    while($row = mysqli_fetch_array($result))
    {
    $id=$row['filmId'];
    $name=$row['filmName'];
    $year=$row['filmYear'];

    echo "$name, $year";

    }
    mysql_free_result($result);
    mysqli_close($conn);
    ?>

    This is the ajax code, when I click href, it will process the above php request to get the data.

    var xmlhttp = new XMLHttpRequest();
    var url = "./searchfilmsbyname.php";
    xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    myFunction(xmlhttp.responseText);
    }
    }
    xmlhttp.open("POST", url, true);
    xmlhttp.send();

    function myFunctio n(response) {
    var arr = JSON.parse(response);
    var i;
    var out = "";
    for (i = 0; i out += "
  • " + arr[i].Film + "< /a>
  • ";
    if ((i + 1) == arr.length) {
    document.getElementById("searchfilmsbyname").innerHTML = out;
    $( "#searchfilmsbyname").listview('refresh');
    }
    }
    }

    I’m not sure about it How does it work on your website, maybe you include it instead of using ajax, but the problem with the above code is that you didn’t send back valid json so:

    function myFunction(response) {
    var arr = JSON.parse(response);
    ...

    will fail because:

    $id=$row['filmId'];
    $name=$row['filmName'];
    $year=$row['filmYear'];
    < br />// this does not generate valid json:
    echo "$name, $year";

    What you should do is add all rows to an array, and only at the end The output is only an array encoded as json:

    $rows = array();
    while($row = mysqli_fetch_array($result))
    {
    $rows[] = array('id '=> $row['filmId'],
    'name' => $row['filmName'],
    'year' => $row['filmYear']);
    }
    echo json_encode($rows);

    Besides, you have a SQL injection problem. You should use prepared statements instead of injecting variables directly into the query.

    < /p>

    Leave a Comment

    Your email address will not be published.