Page 1 of 1
I don't want an error msg
Posted: Wed Feb 26, 2003 6:40 pm
by moniarde
I am trying to validate a database entry. I have a variable ($id) which is passed from page to page via GET. That is extracted then used in the sql statement everytime an entry is made into the database. I am trying to check to see if $id value has already been taken. I am using mysql_result, but everytime the entry isn't found (which is good - that is what is supposed to happen), I get an error telling me that there is no entry. Basically I don't want the error message.
Here's the code:
Code: Select all
<?php
//I am just specifying the $id value here for testing purposes
$_GET[id] = "15";
extract($_GET);
$db = mysql_connect("localhost", "root")
or die(mysql_error);
mysql_select_db("******",$db)
or die("mysql select db fail");
if ($_GET) {
$sql = "Select appdet_id from ".$formtype." Where appdet_id = '".$id."'";
$result = mysql_query($sql,$db)
or die(mysql_error());
if (mysql_result($result,0,"appdet_id")) {
echo "You cannot enter this information again.";
}
}
?>
Any ideas? Thankyou in advance.
Posted: Wed Feb 26, 2003 8:40 pm
by Rob the R
You could do a few of things.
One would be to set the
error_reporting level either globally in your script or just before the command that might error-out (after which you can restore the error_reporting level to normal).
Another would be to define your own custom error handler with
set_error_handler that would trap this specific error and handle it accordingly.
A third option would be to add the "@" symbol to the beginning of the command whose error you want to suppress. This
error control operator will keep practically all errors that command may generate from being created. This should be used with caution, though, since it may mask errors you actually do want to see.
Posted: Thu Feb 27, 2003 3:37 am
by twigletmac
A fourth option would be to test how many results were returned to decide whether the id already exists (by using
mysql_num_rows() instead of using mysql_result():
Code: Select all
if (mysql_num_rows($result) != 0)) {
echo "You cannot enter this information again.";
}
instead of
Code: Select all
if (mysql_result($result,0,"appdet_id")) {
echo "You cannot enter this information again.";
}
Mac
Posted: Thu Feb 27, 2003 8:01 am
by Rob the R
Shoot. I like twigletmac's answer better. I don't like messing with the error levels unless I have to.
Posted: Thu Feb 27, 2003 5:50 pm
by moniarde
The problem I have with that is that the id has to be carried over 6-7 individual forms AND added in to individual tables so as to link all the information together. Each time I run that piece of code, I am applying it to another table, whereby the number of items in the table may be larger than the value of $id. To simplify, I have to check for the exact value of $id each time.
So far I have tried one of Rob the R's suggestions. Just before the "if" statement i used error_reporting to do this:
Code: Select all
<?php
error_reporting(E_ALL ^ E_WARNING);
if (mysql_result($result,0,"appdet_id")) {
echo "You cannot enter this information again.";
$re_entry = "1";
}
error_reporting(E_ALL);
?>
I figure that considering I know that the syntax works, and that the output isn't going to generate any warnings, and I know what the one warning that will be generated is, then this will be ok until I find a better solution.
So if anyone has something please let me know.
Posted: Fri Feb 28, 2003 2:13 am
by twigletmac
A better solution is to count the rows returned and see how many there are - if they are none you know that ID does not exist if there is 1 or more it does.
It honestly is a much better way than messing with the error reporting. Use
mysql_num_rows() not
mysql_result() to see how many rows were returned.
Mac
Posted: Fri Feb 28, 2003 2:23 am
by moniarde
I get it now. Sorry. Thanx for your hep.