Retrieving proper value from SQL query

I want to perform a redirect based on a status code, howver only the first statement gets evaluated. If the statuscode is ac, the first page still gets called. I probably shouldnt be loading this into an array since only one values gets returned, TIA

$stmt="select statuscode from memMaster where eMail = '$memStat'";
$result=mysql_query($stmt);
while ($results=mysql_fetch_array($result)) {

print ($results[statuscode]);      
      
      
if ($results[statuscode] == 'ai')  {
print "<script>\n";
print "  document.location=\"http://www.aace.com/org/duesINTL.php\";";
print "</script>\n";

}

if  ($results[statuscode] == 'ac') {

print "<script>\n";
print "  document.location=\"http://www.aace.com/org/duesDOM.php\";";
print "</script>\n";
}
}
dprasadAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

snoyes_jwCommented:
If only one value is returned, I'd rewrite your code a little.

$result = mysql_query($stmt) or die(mysql_error());
$results = mysql_fetch_array($result);
switch($results['statuscode']) {
    case 'ai':
        print "document.location=\"http://blah blah blah";
        break;
    case 'ac':
        print "blah blah blah";
        break;
    default:
        print "Invalid data";
}
0
Richard QuadlingSenior Software DeveloperCommented:
Or ...

<?php

$aLocations = array(
 'ai' => 'http://www.aace.com/org/duesINTL.php',
 'ac' => 'http://www.aace.com/org/duesDOM.php',
 );

mysql....

if (isset($aLocations[$results['statuscode']]))
 {
 print 'document.location = "' . $aLocations[$results['statuscode']] . '";';
 }
else
 {
 print 'It has all gone horribly wrong.'
 }

?>

Richard.
0
Richard QuadlingSenior Software DeveloperCommented:
Maybe an even better option would be to store a lookup table for the locations in the DB and return that as part of the query rather than a code.

Richard.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

dprasadAuthor Commented:
Ok, I've tried both and in both cases, the statements aren't getting evaluated for some reason.

 snoyes_jw: I can print $results['statuscode'] and it gives the correct code, but nothing happens in the switch statement, I just get a blank page.

RQuading: I get the error message and the page doesn't get forwarded. The weird thing is if I print out $result (after doing $result=mysql_query($stmt) or die(mysql_error());  ) it prints out:  Resource id #6

thanks.. any ideas guys?

0
dprasadAuthor Commented:
snoyes_jw: also, if I print out text in the swtich statement it doesn't get printed, so for some reason its not getting performed.
0
snoyes_jwCommented:
This Resource id #6 is the pointer to the MySQL results.  Having variables names "$result" and "$results" is confusing, and may be part of your problem.  Try this and see what it says:

$row = mysql_fetch_array($result);
echo $row['statuscode'];
0
dprasadAuthor Commented:
snoyes: ok, I did that and am getting the correct status code. still cannot get the case or if statements to evaluate properly..
0
snoyes_jwCommented:
ok, now do

$aLocations = array(
 'ai' => 'http://www.aace.com/org/duesINTL.php',
 'ac' => 'http://www.aace.com/org/duesDOM.php',
 );
$row = mysql_fetch_array($result);
echo $aLocations[$row['statuscode']];
0
dprasadAuthor Commented:
hmm, ok I tried this and am not getting anything back, just a blank screen

$stmt="select statuscode from memMaster where eMail = '$memStat'";
$result=mysql_query($stmt);
$row = mysql_fetch_array($result);



$asLocations = array(
 'ai' => 'http://www.aace.com/org/duesINTL.php',
 'ac' => 'http://www.aace.com/org/duesDOM.php',
 );

echo $aLocations[$row['statuscode']];
0
snoyes_jwCommented:
$asLocations vs $aLocations...
0
dprasadAuthor Commented:
damn sorry, unfortunately it is still not evaluating. I have

$aLocations = array(
 'ai' => 'http://www.aace.com/org/duesINTL.php',
 'ac' => 'http://www.aace.com/org/duesDOM.php',
 );


//print ($aLocations[$results['statuscode']]);

if (isset($aLocations[$results['statuscode']]))
 {
       print "<script>\n";
    print 'document.location = "' . $aLocations[$results['statuscode']] . '";';
    print "</script>\n";

 
 }
else
 {
print "error";
       
 }

I get the 'error' printed out
0
dprasadAuthor Commented:
Here's my updated code. When I try to print out the matching row from the array nothing gets printed


$stmt="select statuscode from memMaster where eMail = '$memStat'";
$result=mysql_query($stmt);
$row = mysql_fetch_array($result);




$aLocations = array(
 'ai' => 'http://www.aace.com/org/duesINTL.php',
 'ac' => 'http://www.aace.com/org/duesDOM.php',
 );


print ($aLocations[$row['statuscode']]);


if (isset($aLocations[$row['statuscode']]))
 {
       print "<script>\n";
    print 'document.location = "' . $aLocations[$row['statuscode']] . '";';
    print "</script>\n";

 
 }
else
 {
print "error";
       
 }
0
snoyes_jwCommented:
But if you print out $row['statuscode'], you get either 'ai' or 'ac'?  Do you have some whitespace character in the database, such as a newline?  Try
SEELCT trim(statuscode) FROM memMaster WHERE...
0
snoyes_jwCommented:
obviously, you'd want to spell "SELECT" correctly...
0
dprasadAuthor Commented:
cool, I got it working using if statements finally,  (($x == 'ai') || ($x == 'in') || ($x == 'ri') || ($x == 'fi') || ($x == 'ei') )


Thanks a lot!!!

Dinesh
0
Richard QuadlingSenior Software DeveloperCommented:
Sorry about my initial mistake.

I used $result instead of $row.

Thanks for the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.