Solved

If Else Delima, if query doen's return antying, can't get the else part to dosplay

Posted on 2012-03-16
4
284 Views
Last Modified: 2012-06-26
I'm getting frusted over an if else statememt that won't work.  It is calling one variable from the table.  If the variable is in the table it will display one thing, if the variable is absent, it should display something else.  I've tried !empty, isset, even $num_rows =1, but the content in the else doesn't display.  Here's the code:

Code: [Select]


<?
$result = mysql_query( "SELECT * FROM codeWords WHERE Mycode = '$MyCode' " )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
  while ($row = mysql_fetch_array($result))
  {
     extract($row);
     
if (!empty( $MyCode  )) {    
echo "here is the code word";
} else {
echo "you need to go back and get the code";
}
$row_count++;
}
mysql_close;
?>


When the code variable is correct the "here is the code word" displays.  when the code variable is incorrect, what should appear as "you need to go back and get the code" is just blank.

Any thoughts on how to structure the query in such a way that eh incorrect text appears?

Thanks,
0
Comment
Question by:TecTaoMC
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Scott Madeira
ID: 37730708
It looks like $MyCode will always have a value.  That is what you are passing in to look at.  What you need to look at in your if else statement is one of the fields from your query result or check to see how many rows you get back.

If the codeword is in your database then $num_rows will be greater than 0.  If the codeword is not in the database then $num_rows would be 0.
0
 
LVL 14

Expert Comment

by:Scott Madeira
ID: 37730727
A couple other things:  your query will only return rows that have the $MyCode codeword in it.  Not sure what you are trying to do with the row_count++ variable.  That should have the same value as $num_roes;


And, if you want to look at the value of the MyCode field from your database rows then you will want to reference $row['MyCode'] and not $MyCode.
0
 

Author Comment

by:TecTaoMC
ID: 37732887
Thanks for your quick reply.  I read and made some changes but still can't seem to get the variable to whow 0 if no result is returned which fowls up the if else statememt.  The if else have been written a couple of different ways if $row_count == 1 {....}else {....} and if @num_row == 1 {..} If $row_count == 0 {..}

here's the code using
<?php
include("include/session.php");
$MyCodePassed = $_POST['MyCode'];

$result = mysql_query( "SELECT * FROM codeWords WHERE Mycode = '$MyCodePassed' " )
or die("SELECT Error: ".mysql_error());
$row_count = mysql_num_rows($result);
  while ($row [Mycode]= mysql_fetch_array($result))
  {
     extract($row [Mycode]);
     
if ($row_count == 1) {    
 echo show the page for this}
 if ($row_count == 0) {    
 echo show the page for no result}  
?>

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37734921
Try it a little more like this.  It will enable you to see the data and that means you will have a better idea of what the programming is doing.  Once you have this working, then you can use the if() statement to test the possible different values of $num
<?php
error_reporting(E_ALL);

// DOES THIS CONNECT AND SELECT THE DATA BASE?
include("include/session.php");

// ESCAPE THE EXTERNAL DATA FOR SAFE USE IN A QUERY
$MyCodePassed = mysql_real_escape_string($_POST['MyCode']);

// PREPARE THE QUERY STRING
$sql = "SELECT * FROM codeWords WHERE Mycode = '$MyCodePassed' ";

// ACTIVATE THIS TO SHOW THE QUERY STRING
// var_dump($sql);

// RUN THE QUERY AND TEST FOR SUCCESS
$res = mysql_query( $sql );
if (!$res)
{
    echo "FAIL: $sql<br/>";
    die ( mysql_error() );
}

// GET THE NUMBER OF ROWS AND DISPLAY THE NUMBER
$num = mysql_num_rows($res);
echo "<br/>QUERY FOUND $num ROWS";

// USE AN ITERATOR TO SHOW WHAT THE QUERY FOUND
while ($row = mysql_fetch_assoc($res))
{
    echo "<br/>";
    print_r($row);
}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now