0

A PHP file has been set up in order to contact a SQL database and retrieve the article with the corresponding ArticleID which I append to the end of the URL.

For example, www.example.com/retrieveArticle.php?ArticleID=1

Currently, the webpage does not show any text output whereas I expected to find one row output as a JSON format.

Note that my table is called Customer.

Code

<?php
if(issert($_REQUEST["ArticleID"]))
    {
    $con = mysql_connect("localhost","createyo_james","password");
    if(!$con)
    {
    die("Could not connect: " .mysql_error());
    }
    mysql_select_db("createyo_TestDatabase", $con);

    $articleID = $_REQUEST["ArticleID"];

    $result = mysql_query("SELECT * FROM Customer WHERE ID = "$articleID" ") or die("Errant query:");

    while($row = mysql_fetch_assoc($result))
    {
    $output[]=$row;
    }

    print(json_encode($output));

    mysql_close($con);

    }

    else
    {
    $output = "not found";
    print(json_encode($output));
    }

?>

Edit:

Since many of the solutions have not worked it may be due to incorrect database information. I know the connection details are correct as I have similar PHP files connected to the same DB. However, I might be wrong about the table structure when getting ID etc...

Database

Code Attempt 2

<?php
if(isset($_GET["ArticleID"]))
{
$con = mysql_connect("localhost","createyo_james","password");
if(!$con)
{
die("Could not connect: " .mysql_error());
}
mysql_select_db("createyo_TestDatabase", $con);
$articleID = mysql_real_escape_string($_GET["ArticleID"]);

$result = mysql_query("SELECT * FROM Customer WHERE ID = "$ArticleID"") or die("Errant query:");

while($row = mysql_fetch_assoc($result)) { array_push($output,$row); } echo json_endode($output);die;

mysql_close($con);

}

else
{
$output = "not found";
echo json_encode($output);
}

?>
3
  • It should be echo json_encode($output); or print json_encode($output); try to use it to check output print_r(json_encode($output)); also change to it if(isset($_REQUEST["ArticleID"])) Commented Jul 14, 2014 at 9:21
  • @Rajlaksh, parentheses are not mandatory, but can be used with print and echo. Commented Jul 14, 2014 at 9:24
  • @Rajlaksh I do not think this is the issue since i have used in in a similar PHP file with success. I have tried your recommendation however, but still no output is being shown. Commented Jul 14, 2014 at 9:26

3 Answers 3

1

You have a typo on the first line:

if(issert($_REQUEST["ArticleID"]))

Should be:

if(isset($_REQUEST["ArticleID"]))

Just noticed something else. Your query:

$result = mysql_query("SELECT * FROM Customer WHERE ID = "$articleID" ") or die("Errant query:");

Shouldn't include double quotes around $articleID:

$result = mysql_query("SELECT * FROM Customer WHERE ID = '$articleID' ") or die("Errant query:");
Sign up to request clarification or add additional context in comments.

3 Comments

It was like "Where is Waldo?"
Thanks, I had to copy the PHP code from a YouTube video by hand. However, this has not fixed the problem as I still receive the same output.
I've edited my original post as your query also contains an error which you've introduced to your second code example.
0

Edited Code

<?php

if (isset($_GET["id"])) {
    $con = mysql_connect("localhost", "USER_NAME", "PASSWORD");
    if (!$con) {
        die("Could not connect: " . mysql_error());
    }
    if (!mysql_select_db("DB_NAME", $con)) {
        die("Database error " . mysql_error());
    }
    $id = $_GET["id"];
    $query="SELECT * FROM table WHERE ID =".$id;
    $result = mysql_query($query) or die("Errant query:");
    $output = Array();
    while ($row = mysql_fetch_assoc($result)) {
        $output[] = $row;
    }
    echo json_encode($output);
    mysql_close($con);
    die;
} else {
    $output = "not found";
    echo (json_encode($output));
    die;
}
?>

12 Comments

Edited code :-) Check In both cases i am returning array.
I have tried your code but still receiving same result.
use var_dump($result) to check the query result.. Are you sure you are getting query result
Tried var_dump($result) but no success. What do you mean by "Are you sure you are getting query result"?
Means, it is possible that your query is wrong and you are not getting any result.So you will not get any result in jquery
|
0
<?php
if(isset($_GET["ArticleID"]))
    {
    $con = mysql_connect("localhost","createyo_james","password");
    if(!$con)
    {
    die("Could not connect: " .mysql_error());
    }
    mysql_select_db("createyo_TestDatabase", $con);

    $articleID = mysql_real_escape_string($_GET["ArticleID"]);

    $result = mysql_query("SELECT * FROM Customer WHERE ID = '$articleID' ") or die("Errant query:");

    while($row = mysql_fetch_assoc($result))
    {
$id = $row['id'];
    $output[]= array('id' => "$id");
    }

    echo json_encode($output);

    mysql_close($con);

    }

    else
    {
    $output = "not found";
    echo json_encode($output);
    }

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.