Jump to content

Archived

This topic is now archived and is closed to further replies.

Pi_Mastuh

How do I...

Recommended Posts

I want to have it seach for an ID number in a table and if it finds it then to check if a value is above zero, if it is then dissplay error message one. If it's not, see if a second value is above zero and if it is display error message 2. How do I do that?

Share this post


Link to post
Share on other sites
you want it to check the value of the ID is a above 0 or a different value?

Share this post


Link to post
Share on other sites
a different value. Each row has an ID, a value, and a 2nd value. One of the 2 non-ID values is above zero and the other is zero.

Share this post


Link to post
Share on other sites
ok, i c, well could do it a few differnet ways but couldnt u just get both values, check the first one then if its above 0 display error if its not check the second one etc.
like this:
[code]
$row = mysql_fetch_array(mysql_query("SELECT * FROM `table` WHERE `id`='1'"));

if($row['value1'] > 0){
echo "error msg #1";
}elseif($row['value2'] > 0){
echo "error msg #2";
}else{
echo "they are both 0";
}
[/code]

but i am still not sure if thats what ur asking.

Share this post


Link to post
Share on other sites
Well, i'm going to explain what I think you're saying. You want to check for a row with a certain id number and display an error if another value in that row is above zero.

[code]$sql = "SELECT * FROM `table` WHERE `id` = 1 AND `othernumber` > 0";
$result = mysql_query($sql);
if (mysql_num_rows($result)
{
    echo "ERROR!";
}[/code]

Share this post


Link to post
Share on other sites
from readin ur post i would agree with ProjectFear's approach. however i would suggest modifying the code as follows:

[code]
$query = sprintf("SELECT * FROM table WHERE id='1'");
$result = mysql_query($query);
if (!$result) {
die ('Invalid Query' . mysql_error());
}

$nums = mysql_num_rows($result);
if (!$nums) {
die ('Sorry, no entries found.');
}

while ($row = mysql_fetch_array($result)) {

if($row['value1'] > 0){
die (error msg #1');
}elseif($row['value2'] > 0){
die ('error msg #2');
}else{
echo "they are both 0";
break;
}

}
[/code]

This should work. It provides better scripting practice and some error handling.

Share this post


Link to post
Share on other sites
I'm using this:
[code]$sql = "SELECT count(*) as atkItem FROM Dualequip WHERE petID = '$monopetID'";
  $result1 = mysql_query($sql);
$query_data = mysql_fetch_array($result1);
$atkItem = $query_data['atkItem'];

if ($atkItem > 0)
{
$SQL = "SELECT * FROM Dualequip WHERE petID = '$monopetID'";
$result = mysql_query($SQL, $connection) or die("Error: ".mysql_error()."");
$query_data = mysql_fetch_array($result);
$atkValue = $query_data['atkValue'];
$defValue = $query_data['defValue'];
if ($atkValue > 0)
{
$no = 0;
}
elseif ($defValue > 0)
{
$no = 1;
}
else
{
$no = 3;
}
}[/code] and [code]if ($no == 0 AND $food == "Weapon" AND $atkValue > o)
{
echo "<p align=center>$monopetName already has an attack item equipped!<br><br><a href='../myitems.php'>Back to Your Inventory</a>";
}
elseif ($no == 1 AND $food == "Weapon" AND $defValue > o)
{
echo "<p align=center>$monopetName already has an defense item equipped!<br><br><a href='../myitems.php'>Back to Your Inventory</a>";
}
elseif ($food == "Weapon")
{
$sql = "INSERT INTO Dualequip (preuserID, petID, itemID, atkValue, defValue)
VALUES ('$preuserID', '$monopetID', '$itemID', '$atkValue', '$defValue')";
mysql_query("DELETE FROM myitemschibi WHERE itemID = '".$_POST['itemID']."'");

$result = mysql_query($sql) or die("There was an error equiping the weapon. Sorry.");
echo "<p align=center>You have equipped <b>".$monopetName."</b> with <b>".$itemName."</b>!<br><br><a href='../myitems.php'>Back to Your Inventory</a>";
}[/code] but the pet i'm using to test has 3 items equipped when it should only be 1. It's not blocking it.

Share this post


Link to post
Share on other sites
i read most of it but i think i can c an eror. ur using 'o' instead if '0' in the if statements for your second code. take a look. but i am not sure if thats ment to b like that.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.