Re: MySql Check if Value Exists in Table
| From: | (Dave Daniels) | Date: | Fri, 18 Aug 2000 18:02:08 +0000 |
| Subject: | Re: MySql Check if Value Exists in Table | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-12509@lists.php.net to get a copy of this message | ||
Course, this is really an SQL problem, not a PHP problem, but I would do
something like:
"select count(*) from table where col_1 = $id_1 and col_2 = $id_2"
You could handle it better by defining keys so there's no need to check
yourself if it exists or not.
----- Original Message -----
From: "Pam Joseph" <pjoseph@gpc.peachnet.edu>
To: "Php-General@Lists. Php. Net" <php-general@lists.php.net>
Sent: Friday, August 18, 2000 12:49 PM
Subject: [PHP] MySql Check if Value Exists in Table
> PHPer's,
>
> Can you help me with this code? I'm trying to see if an id already exists
> in the table, if so give an error message. I'm getting "0 is not a MYSQL
> result index".
>
> function check_exists($table, $col_1, $col_2, $id_1, $id_2) {
>
> $SQL = "SELECT $col_1, $col_2";
> $SQL .= " FROM $table";
> $SQL .= " WHERE $col_1 = $id_1 and $col_2 = $id_2";
> $result = mysql_query($SQL);
> echo ("Result in check_exists: $result");
> if (mysql_num_rows($result) > 0) {
> echo ("<h3>Combination of:<br> $col_1 $id_1 and
$col_2
> $id_2 already exists in $table. Please reselect.</h3>");
> exit;
> }
> else return -1;
>
> }
>
> Thanks,
>
> pj
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>