Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

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";
}
}
0
dprasad
Asked:
dprasad
  • 7
  • 6
  • 3
2 Solutions
 
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 DeverloperCommented:
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 DeverloperCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 DeverloperCommented:
Sorry about my initial mistake.

I used $result instead of $row.

Thanks for the points.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 7
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now