homepage Welcome to WebmasterWorld Guest from 54.166.14.218
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Help needed with a conditional response - if else
Jamier101



 
Msg#: 4678219 posted 7:25 pm on Jun 7, 2014 (gmt 0)

Hello,

I was wondering if someone could point me in the right direction. I've got a page which calls up records from a basic mySQL database. I've got results being displayed according to what I pass to it but when there are no results I get a blank page. Therefore I thought I would expand the coding a little to give a message but I just don't appear to have got it quite correct. Can anyone give me a few pointers please?

<?php

// Make a connection
$con = mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("beekeeping") or die(mysql_error());

// Get values from form
$hive_num=$_POST['hive_num'];

$result = mysql_query("SELECT * FROM inspections WHERE hive_num=$hive_num");

$num_rows($mysql_num_rows($result);

if($num_rows>1) {
echo "<table border='1'>";
echo "<tr> <th>Brood Frames</th> <th>Queen Seen</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array($result)) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['brood_frames'];
echo "</td><td>";
echo $row['queen_seen'];
echo "</td></tr>";
}

echo "</table>";
}

elseif($num_rows)==0) {
echo "There were not records to be found.";
}

mysql_close($con);

?>

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4678219 posted 9:02 pm on Jun 7, 2014 (gmt 0)

I really, really hope this database is about beekeeping.

$num_rows($mysql_num_rows($result);
...
if($num_rows>1)
...
elseif($num_rows)==0)

What happens if there is exactly one row? Someone else is going to explain the hierarchy of parentheses to me.

Jamier101



 
Msg#: 4678219 posted 9:26 pm on Jun 7, 2014 (gmt 0)

I really, really hope this database is about beekeeping.
It is, I'm a beekeeper but I also love messing around building website, etc. so I thought I'd try and bring the two together somehow by storing all my records online.

If there is only one row then I'd really like it just to display one row.

else($num_rows)=0) {
echo "There were not records to be found.";
}

coopster

WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4678219 posted 3:11 pm on Jun 9, 2014 (gmt 0)

First, I realize this may be for internal use only but you should get in the habit anyway, good coding practices and all, you know? I'm referring to escaping user-supplied data so that you won't be vulnerable. See the mysql_real_escape_string() method and scrub up your form data here ...

// Get values from form
$hive_num=$_POST['hive_num'];

$result = mysql_query("SELECT * FROM inspections WHERE hive_num=$hive_num");


Next, I'm thinking your problems may be starting here, it looks like you missed the assignment of the value. Replace the parenthesis with an equal sign:

$num_rows($mysql_num_rows($result);

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4678219 posted 3:55 pm on Jun 9, 2014 (gmt 0)

If there is only one row then I'd really like it just to display one row.

I'd have thought so too. That's why I wondered-- tangentially-- about "> 1" instead of "> 0" or "== 1".

else($num_rows)=0)

If the first open-parenthesis (the one coopster homed in on) is a typo, then we now have a leftover close-parenthesis. Was the original question cut-and-paste or typed-in from scratch?

if (some value > 1)
do stuff
elseif (some value == 0)
do otherstuff

vs.

if (some value > 0)
do stuff
else
do otherstuff

I don't want to consider the possibility of $num_rows coming out as either a non-integer or less than zero, but it probably does no harm to code for it.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved