/    Sign up×
Community /Pin to ProfileBookmark

Php ajax with mysql if else error

I recently made an ajax/php script to query a database.
I had the ajax page and the request page seperate unlil I got it working but when I did merge then I came about a problem.

The problem arises at the merge in the if else statement. I have

[code=php]

<?php
if ($_SERVER[‘QUERY_STRING’]){…..etc…..};
else { ?>

//start of html
<html>
<head>
….etc….

[/code]

I canneot get this to work,
any help would be appreciated,
Thanks

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 05.2011 — We may need more info on what you mean by it not working: what does it do that you don't want it to do or not do that you want it to do?

One suggestion: use the empty() function to test the $_SERVER value, just in case it's not set at all, e.g.:

[code=php]
<?php
if(!empty($_SERVER['QUERY_STRING'])) {
// do something with query string
}
else {
?>
<!-- some HTML -->
<?php
}
?>
[/code]
Copy linkTweet thisAlerts:
@brian8765authorMay 05.2011 — it returns an, unexpected else, error... I have tried putting in a ; in places but no luck
Copy linkTweet thisAlerts:
@andx85May 05.2011 — I notice that you have a ";"-character at the end of your

if ($_SERVER['QUERY_STRING']){.....etc.....}

it shouldn't be there
Copy linkTweet thisAlerts:
@NogDogMay 05.2011 — For a syntax error such as that we'll probably need to see the actual code, not a "brief synopsis". ? But "andx85" has a good point if your actual code is written that way witha ";" following the "}".
Copy linkTweet thisAlerts:
@GarrettVDMay 05.2011 — Yeah, the "unexpected else" is definitely coming from that semi-colon at following the closing curly bracket at the end of the if statement.

Also, not sure what the rest of your page looks like, but it's good practice (IMO) to keep the business logic separate from the presentation. So you could do something like this...
[code=php]<?
if ($_SERVER['QUERY_STRING']) {
// something
} else include_once "./file1.html"; ?>

[/code]


Just my two cents!
Copy linkTweet thisAlerts:
@brian8765authorMay 06.2011 — This is the section of my code at the join:
[code=php]while($row = mysql_fetch_row($result))
{
echo "<tr>";

// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell){

$toreturn.= "<td>$cell</td>";}

$toreturn.= "</tr>n";
}
$toreturn.= "</table></div>";
return $toreturn;
}
else
{



?>


<html>
<style type="text/css">
h1{text-transform:capitalize;text-align:center}
th{width:100px}[/code]
Copy linkTweet thisAlerts:
@brian8765authorMay 06.2011 — It works fine if I replace the 'else' with 'if(empty($_SERVER["QUERY_STRING"]))' ......

But it still return an "unexpected else" error
Copy linkTweet thisAlerts:
@brian8765authorMay 07.2011 — [SOLVED]

I had a function in between the 'if' and the 'else' ....

Sorry for wasting everyones time(& my noobishness)

Thanks
×

Success!

Help @brian8765 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...