Monday 3 September 2018

Adding parameters to PHP URL does not return anything, but hard coding works perfectly

AAlright, so here goes nothing.

the URL: http://tandemenvoy.michaeldvinci.com/forum/repliesJSON.php?rID=2returns me absolutely nothing. if I hard code the SQL query to
"SELECT * FROM replies WHERE replyID = 2"

I get the correct JSON formatted request, but when I try passing it as a parameter in the URL. is just displays nothing -
now the reason I'm doing this is because I'm working on an iOS app that is transversing multiple tables and I need it to only display the matching 'replyTopic's to the 'categoryID's and I'm guessing if I can just append the categoryIDs to the end of the url with the correct query, it'll be a heck of a lot easier
repliesJSON.php:
<?php

$con=mysqli_connect("localhost","","","");

if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$rID = $GET_['rID'];

$sql = "SELECT * FROM replies WHERE replyID = '".$rID."' ";

if ($result = mysqli_query($con, $sql))
{
    $resultArray = array();
    $tempArray = array();

    while($row = $result->fetch_object())
    {
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }

    echo json_encode($resultArray);
}

mysqli_close($con);
?>


Because your Mysql statement is wrapped in double quotes, PHP will parse it for variables, meaning that '".$rID."' is outputted as '2' (with the single quotes still there) because PHP ignores the double quotes and concatenates the single quotation marks and the number 2.
Simply changing: WHERE replyID = '".$rID."' to: WHERE replyID = '$rID' will fix this problem.
Edit:
you can also leave out the single quotes altogether as @steven has mentioned:
WHERE replyID = $rID

0 comments:

Post a Comment