Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to GET different rows from different columns in php/mysql, and pack them into an array. I am able to successfully GET a jason encoded array back IF all values in the GET string match. However, if there is no match, the code echos 'no match', and without the array. I know this is because of the way my code is formatted. What I would like help figuring out, is how to format my code so that it just displays "null" in the array for the match it couldn't find.

Here is my code:

include '../db/dbcon.php';
$res = $mysqli->query($q1) or trigger_error($mysqli->error."[$q1]");
if ($res) {
if($res->num_rows === 0)
{
    echo json_encode($fbaddra);
}
else
{
    while($row = $res->fetch_array(MYSQLI_BOTH)) {
    if($_GET['a'] == "fbaddra") {
        if ($row['facebook'] === $_GET['facebook']) {
            $fbaddr = $row['addr'];

        } else {
            $fbaddr = null;
        }
        if ($row['facebookp'] === $_GET['facebookp']) {
                $fbpaddr = $row['addr'];

        } else {
            $fbpaddr = null;
        }
        $fbaddra = (array('facebook' => $fbaddr, 'facebookp' => $fbpaddr));
        echo json_encode($fbaddra);
    }
    }
}
$mysqli->close();

UPDATE: The GET Request

I would like the GET request below to return the full array, with whatever value that didn't match as 'null' inside the array.

domain.com/api/core/engine.php?a=fbaddra&facebook=username&facebookp=pagename

The GET above currently returns null.

Requests that work:

domain.com/api/core/engine.php?a=fbaddra&facebook=username or    domain.com/api/core/engine.php?a=fbaddra&facebookp=pagename

These requests return the full array with the values that match, or null for the values that don't.

TL;DR I need assistance figuring out how to format code to give back the full array with a value of 'null' for no match found in a row.

share|improve this question
    
Your logic is flawed. you cannot have while(..) {... json_encode();}. You will be outputting invalid json - json string has to be a MONOLITHIC entity. YOu cann't simply stack multiple json-encoded "things" next to each other and have them work. –  Marc B Feb 22 at 22:02

3 Answers 3

up vote 0 down vote accepted

rather than assigning as 'null' assign null. Your full code as follows :

include '../db/dbcon.php';
$res = $mysqli->query($q1) or trigger_error($mysqli->error."[$q1]");
if ($res) {
if($res->num_rows === 0)
{
    echo json_encode('no match');
}
else
{
    while($row = $res->fetch_array(MYSQLI_BOTH)) {
    if($_GET['a'] == "fbaddra") {
        if ($row['facebook'] === $_GET['facebook']) {
            $fbaddr = $row['dogeaddr'];
                //echo json_encode($row['dogeaddr']);
        } else {
            $fpaddr = null;
        }
        if ($row['facebookp'] === $_GET['facebookp']) {
                $fbpaddr = $row['dogeaddr'];
                //echo json_encode($row['dogeaddr']);
        } else {
            $fbpaddr = null;
        }
        $fbaddra = (array('facebook' => $fbaddr, 'facebookp' => $fbpaddr));
        echo json_encode($fbaddra);
    }
    }
}
$mysqli->close();

You can even leave else part altogether.

share|improve this answer
    
If one or both of the values do not match in the GET string, it still returns only 'no match', instead of the full array with values that have no match as 'null'. –  user3259138 Feb 22 at 20:16
    
no match will only come if the code never entered else part. Is your else part working as expected. If you want to format if part as well you can use $fbaddra = (array('facebook' => null, 'facebookp' => null)); echo json_encode($fbaddra); in if part –  Durgesh Chaudhary Feb 22 at 20:21
    
I agree that the else statements need to be reformatted. However if I change echo json_encode('no match'); to echo json_encode($fbaddra); then all I get back is null, and no array. Suggestions? –  user3259138 Feb 22 at 20:27
    
first create empty array for $fbaddra like $fbaddra = (array('facebook' => null, 'facebookp' => null)); and then use echo json_encode($fbaddra); –  Durgesh Chaudhary Feb 22 at 20:30
    
Odd. I still get back only null, with the code above and your specified blank array values. Seems like the whole thing might need to be re-structured? Suggestions? –  user3259138 Feb 22 at 20:34

Check your code in this fragment you not use same names for variables:

if ($row['facebook'] === $_GET['facebook']) {
            $fbaddr = $row['dogeaddr'];
                //echo json_encode($row['dogeaddr']);
        } else {
            $fpaddr = 'null';
        }

$fbaddr not is same as $fpaddr, this assign wrong result to if statement.

share|improve this answer
    
Fixed, and still gives only 'no match'. No array. –  user3259138 Feb 22 at 20:19
    
Replace: echo json_encode('no match'); line to: $fbaddra = (array('facebook' => 0, 'facebookp' => 0)); echo json_encode($fbaddra); –  Joseph Collins Feb 22 at 20:22
    
This almost worked, but returns only null. No array –  user3259138 Feb 22 at 20:25
    
Putting esle $fbaddra = (array('facebook' => null, 'facebookp' => $fbpaddr)); instead of else echo null, for the else functions (with each else functions respective variables), still returns null on GET request. No array. –  user3259138 Feb 22 at 20:49

It was the mysql query that was the problem.

For those who come across this, and need something similar, you'll need to format your query like this:

** MYSQL QUERY **

if ($_GET['PUTVALUEHERE']) {
    $g = $_GET['PUTVALUEHERE'];
    $gq = $mysqli->real_escape_string($g);
    $q1 = "SELECT * FROM `addrbook` WHERE `facebookp` = '".$gq."' OR `facebook` = '".$gq."'";
}

** PHP CODE **

if($_GET['PUTVALUEHERE']{
echo json_encode($row['addr']);

}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.