mpausch Posted April 10, 2007 Share Posted April 10, 2007 I'm a total newbie to php and desperatley need help. Searched forums but couldnt find the answer. I have a price field (integer) and if it's $0, then I dont want it to display at all. It should only display if its >0. At this time if the price is $0 in the database, the page displays $0 and I want it to show nothing. My code... <?php $result = mysql_query("SELECT breedingprice FROM animals WHERE (animals.ari=123076)"); $fresult=(mysql_result($result,0)); $newresult=(number_format($fresult,0)); printf($sign.$newresult,0); ?> any help will be forever appreciated!!! Link to comment https://forums.phpfreaks.com/topic/46495-solved-help-a-newbie-with-a-simple-if-else/ Share on other sites More sharing options...
JSHINER Posted April 10, 2007 Share Posted April 10, 2007 Replace $result = mysql_query("SELECT breedingprice FROM animals WHERE (animals.ari=123076)"); With $result = mysql_query("SELECT breedingprice FROM animals WHERE breedingprice >= 1 AND (animals.ari=123076)"); Link to comment https://forums.phpfreaks.com/topic/46495-solved-help-a-newbie-with-a-simple-if-else/#findComment-226221 Share on other sites More sharing options...
TEENFRONT Posted April 10, 2007 Share Posted April 10, 2007 I know this is more or less solved, but Just to add to this and explain .... it should be >1 ( as stated above ) not >0 .... ">" means greater than or equal to. So this means if the price is 0 this is equal to 0 and therefore it will show a price. Link to comment https://forums.phpfreaks.com/topic/46495-solved-help-a-newbie-with-a-simple-if-else/#findComment-226237 Share on other sites More sharing options...
kenrbnsn Posted April 10, 2007 Share Posted April 10, 2007 No, the ">" operator mean "greater than", ">=" means "greater than or equal" Ken Link to comment https://forums.phpfreaks.com/topic/46495-solved-help-a-newbie-with-a-simple-if-else/#findComment-226300 Share on other sites More sharing options...
mpausch Posted April 10, 2007 Author Share Posted April 10, 2007 Well guys I am very impressed with the speed of your replies and thanks also. I replaced the code as stated and now am getting the following error on the page.. Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 19 in /home/lcalpaca/public_html/animals/incatest.php on line 146 ... Link to comment https://forums.phpfreaks.com/topic/46495-solved-help-a-newbie-with-a-simple-if-else/#findComment-226371 Share on other sites More sharing options...
JSHINER Posted April 11, 2007 Share Posted April 11, 2007 Please post full code. Link to comment https://forums.phpfreaks.com/topic/46495-solved-help-a-newbie-with-a-simple-if-else/#findComment-227074 Share on other sites More sharing options...
per1os Posted April 11, 2007 Share Posted April 11, 2007 $fresult=mysql_result($result,1); // should be one try that. Link to comment https://forums.phpfreaks.com/topic/46495-solved-help-a-newbie-with-a-simple-if-else/#findComment-227076 Share on other sites More sharing options...
kenrbnsn Posted April 11, 2007 Share Posted April 11, 2007 That means that you didn't get any rows returned. A much better way of writing this would be: <?php $query = "SELECT breedingprice FROM animals WHERE breedingprice >= 1 AND animals.ari=123076"; $result = mysql_query($query) or die("Problem with the query <pre>$query</pre><br>" . mysql_error()); if (mysql_num_rows > 0) { $rw = mysql_fetch_assoc($result); echo $sign . number_format($rw['breedingprice']); } ?> Ken Link to comment https://forums.phpfreaks.com/topic/46495-solved-help-a-newbie-with-a-simple-if-else/#findComment-227079 Share on other sites More sharing options...
mpausch Posted April 12, 2007 Author Share Posted April 12, 2007 Tried that Ken and the result is that with the field 'breedingprice' populated with an amount such as '5000' or with '0' or with NULL, nothing displays on the page (the dollar sign also is not displayed). Seems like it should work. Could it have something to do with the field type in the database? This field is set to... integer nulls are allowed default value is null This is my opening code at the start of the page... <?php - mysql_select_db($database_LCA, $LCA); $query_Icedancer = "SELECT * FROM animals WHERE animals.ARI=123076"; $Icedancer = mysql_query($query_Icedancer, $LCA) or die(mysql_error()); $row_Icedancer = mysql_fetch_assoc($Icedancer); $totalRows_Icedancer = mysql_num_rows($Icedancer); $sign="$"; ?> Man, this is killin' me! Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/46495-solved-help-a-newbie-with-a-simple-if-else/#findComment-227913 Share on other sites More sharing options...
kenrbnsn Posted April 12, 2007 Share Posted April 12, 2007 I goofed on the "if". Try this: <?php if (mysql_num_rows($result) > 0) { $rw = mysql_fetch_assoc($result); echo $sign . number_format($rw['breedingprice']); } else echo 'Nothing to display'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/46495-solved-help-a-newbie-with-a-simple-if-else/#findComment-227920 Share on other sites More sharing options...
mpausch Posted April 12, 2007 Author Share Posted April 12, 2007 Ken, Outstanding!! That works like a charm! I owe you... Link to comment https://forums.phpfreaks.com/topic/46495-solved-help-a-newbie-with-a-simple-if-else/#findComment-228057 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.