View Full Version : I need a very simple php command
lexington 07-28-2006, 11:48 AM Hello, sorry but I am having a problem with something simple. I am trying to make it so that if two db rows are 0, then an error message will present itself. The problem I am having is that it is displaying the error even if one of the rows is not 0. Here is my code (it is phpBB type format and I replaced names with "blah" on this forum)
if($row["blah_one"] || $row["blah_two"] == 0) {
$message = "blah error";
The problem is that if blah one is 0, but blah two is 1, it still errors. I want it to only error if both blah one and blah two equal 0. What is the code for that please? Thanks
FluffyTigger 07-28-2006, 11:54 AM Your code is incorrect
if($row['blah_one'] == 0 && $row['blah_two'] == 0)
$message = 'blah error';
The way it's currently wrote mean if blah_one is true or if blah_two is 0 then the message would show up.
lexington 07-28-2006, 12:01 PM The funny thing is that is the exact code I used the first time until I noticed that if one of them is not 0 it still errors. So I changed it to the code I posted and it still has the same problem. It works partially though, since if in the db:
blah_one = 0
blah_two = 0
Then it displays the correct message. However if
blah_one = 1
blah_two = 0
or
blah_one = 0
blah_two = 1
it still displays that same message when it shouldn't. Actually re-reading your post I think I am just using the wrong type of code for this function. I want it to only display the message when both blah_one and blah_two equal 0. If one or the other is greater than 0, I want the message to not display.
Replace (&&) by (||) in FluffyTigger's code
lexington 07-28-2006, 12:09 PM I tried that too, that is why I have no idea why this isn't working.
before the if statement, print out the values of blah_one and blah_two ... to check if this varible really reads your DB.
FluffyTigger 07-28-2006, 12:14 PM What kind of field is it? Is it an integer field? If it's like a floating point field, 0 isn't represented the same way in binary. If it's a floating field, you use <= 0, == should only be used if you are assured of the representation of 0 to be precise.
|| is or, if you use || then the message will show up if one condition is true.
lexington 07-28-2006, 12:14 PM Sorry but how do I do that? :P I believe it is doing something though since when I change == 0 to > 0 for example the message doesn't appear at all. However at the same time it doesn't work either.
echo $row['blah_one'];
echo $row['blah_two'];
if($row['blah_one'] == 0 || $row['blah_two'] == 0)
$message = 'blah error';
lexington 07-28-2006, 12:22 PM There is no output displaying for the echo. Does that mean it is not connecting to the db?
can you post your file here?
lexington 07-28-2006, 12:25 PM You know what you are right I bet I am missing the php code to pull that data. Thanks Oras, is it ok if I pm you the file?
lexington 07-28-2006, 12:33 PM BTW I am willing to pay for other minor tweaks to current scripts as well. If you are not interested that is fine, and if I can pm you the short code of the file that would be great.
webadpro 07-28-2006, 12:35 PM Here's a script I've made just for you:
// Change the #### For your table name
define("TBL_FROM", "####");
function getinfo(){
$q = "SELECT * FROM ".TBL_FROM;
$result = mysql_query($q, $this->connection);
/* Error occurred, return given name by default */
if(!$result || (mysql_numrows($result) < 1)){
return NULL;
}
/* Return result array */
$dbarray = mysql_fetch_array($result);
return $dbarray;
}
$row = getInfo();
echo $row['blah_one'];
echo $row['blah_two'];
if($row['blah_one'] == 0 && $row['blah_two'] == 0)
$message = 'blah error';
Best Regards,
Pat
PRDScripts.com (http://prdscripts.com)
FluffyTigger 07-28-2006, 01:17 PM do a print_r($row) it'll print all the results from $row to see if you're fetching the correct data or if there's some issues with what get returned. The code at the bottom is correct as far as I'm aware.
horizon 07-28-2006, 01:25 PM The function, above, should be changed to:
// Change the #### For your table name
define("TBL_FROM", "####");
function getinfo(){
$q = "SELECT * FROM ".TBL_FROM;
$result = mysql_query($q, $this->connection);
/* Error occurred, return given name by default */
if(!$result || (mysql_num_rows($result) < 1)){
return false;
}
/* Return result array */
$dbarray = mysql_fetch_array($result);
return $dbarray;
}
$row = getInfo();
print_r($row['blah_one'])."<br />";
print_r($row['blah_two'])."<br />";
if (empty($row['blah_one']) || empty($row['blah_two']) || $row['blah_one'] <= 0 || $row['blah_two'] <= 0) {
$message = 'blah error';
}
This should do it. ;)
lexington 07-28-2006, 01:57 PM Thanks a lot guys, right now Oras is helping me :)
|