This is a discussion on problem with validation within the MySQL Database forums, part of the Database Forums category; Hi everyone, I have a small problem with validation, and cannot seem to find the problem. I have googled for ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi everyone,
I have a small problem with validation, and cannot seem to find the problem. I have googled for different ways to do this, but mostly they all come back with the same code that i am using, yet its not working!! this is the code: $result = mysql_query("SELECT * FROM `code_tbl` WHERE `code` ='$product' ") or die(mysql_error()); $description = mysql_fetch_array( $result ); $check = mysql_num_rows( $result ); if($check !=="0" ){ echo "The product is"; echo $description['product_desc']; exit; } else { echo "It does not exist"; } When i type in a valid code, it comes back with the description (great), but when i type in one that does not exist, it still comes back with the first line (The product you have chosen is) with nothing after it (obviously because it doesnt exist) Any clues what i have done wrong? |
|
|||
|
On 30 Jul, 11:47, Dave <david.greenh...@praybourne.co.uk> wrote:
> Hi everyone, > > I have a small problem with validation, and cannot seem to find the > problem. I have googled for different ways to do this, but mostly they > all come back with the same code that i am using, yet its not > working!! > > this is the code: > > $result = mysql_query("SELECT * FROM `code_tbl` > WHERE `code` ='$product' ") > or die(mysql_error()); > > $description = mysql_fetch_array( $result ); > $check = mysql_num_rows( $result ); > if($check !=="0" ){ > echo "The product is"; > echo $description['product_desc']; > exit; > > } else { > > echo "It does not exist"; > > } > > When i type in a valid code, it comes back with the description > (great), but when i type in one that does not exist, it still comes > back with the first line (The product you have chosen is) with nothing > after it (obviously because it doesnt exist) > > Any clues what i have done wrong? I suspect it is because of: $check !=="0" The !== is a strict operator. Its result is only true if both sides are equal in value and of the same type. Try $check !== 0 OR $check != '0' (note the use of "" is unnecessary here as you have nothing in the string to parse) |
|
|||
|
On 30 Jul, 12:11, Captain Paralytic <paul_laut...@yahoo.com> wrote:
> On 30 Jul, 11:47, Dave <david.greenh...@praybourne.co.uk> wrote: > > > > > > > Hi everyone, > > > I have a small problem with validation, and cannot seem to find the > > problem. I have googled for different ways to do this, but mostly they > > all come back with the same code that i am using, yet its not > > working!! > > > this is the code: > > > $result = mysql_query("SELECT * FROM `code_tbl` > > WHERE `code` ='$product' ") > > or die(mysql_error()); > > > $description = mysql_fetch_array( $result ); > > $check = mysql_num_rows( $result ); > > if($check !=="0" ){ > > echo "The product is"; > > echo $description['product_desc']; > > exit; > > > } else { > > > echo "It does not exist"; > > > } > > > When i type in a valid code, it comes back with the description > > (great), but when i type in one that does not exist, it still comes > > back with the first line (The product you have chosen is) with nothing > > after it (obviously because it doesnt exist) > > > Any clues what i have done wrong? > > I suspect it is because of: > $check !=="0" > > The !== is a strict operator. Its result is only true if both sides > are equal in value and of the same type. > > Try > > $check !== 0 > > OR > > $check != '0' > (note the use of "" is unnecessary here as you have nothing in the > string to parse)- Hide quoted text - > > - Show quoted text - Cheers for that, the latter one works perfectly. Dave. |