RE: [PHP] MySql Check if Value Exists in Table
| From: | Pam Joseph | Date: | Fri, 18 Aug 2000 18:03:44 +0000 |
| Subject: | RE: [PHP] MySql Check if Value Exists in Table | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-12511@lists.php.net to get a copy of this message | ||
That would work with this one, but in other functions, if it exists, then I
get the id # like:
function check_author_exists($author_email) {
$SQL = "SELECT author_id, author_email FROM Author WHERE
author_email like \"$author_email\"";
$result = mysql_query( $SQL);
$Err = mysql_error();
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result);
return $row["author_id"];
}
else return -1;
}
pj
:-> -----Original Message-----
:-> From: Dave Daniels [mailto:dave@musiccity.net]
:-> Sent: Friday, August 18, 2000 2:02 PM
:-> To: pjoseph@gpc.peachnet.edu; Php-General@Lists. Php. Net
:-> Subject: Re: [PHP] MySql Check if Value Exists in Table
:->
:->
:-> 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
:-> >
:->
:->
:-> --
:-> 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
:->
:->