Re: sql query successful
| From: | Christopher Ostmo | Date: | Wed, 18 Jul 2001 19:42:38 +0000 |
| Subject: | Re: sql query successful | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-58503@lists.php.net to get a copy of this message | ||
Tyler Longren pressed the little lettered thingies in this order...
> Hello everyone,
>
> I've been writing database enabled site for quite a while now. One thing I
> have never figured out it how to determine if a sql query is successful.
>
> This works:
> if ($connection = mysql_connect("host","username","password")) {
> print "Successful connection":
> }
> else {
> print "No connection";
> }
>
> But this won't work:
>
> if ($sql = mysql_query("SELECT * FROM table ORDER BY rand()")) {
> print "SQL executed successfully.";
> }
> else {
> print "SQL not executed successfully.";
> }
>
> Can anyone tell me how I can tell if the SQL executed successfully? Is
> there any method besides OR DIE?
>
What's wrong with "or die(..." ? If you get no error, the query was
successful. If you take your "$sql =" out it *should* work. mysql_query
returns TRUE or FALSE depending on whether or not the query
succeeded. This isn't tested, but it should work:
if (mysql_query("SELECT * FROM table ORDER BY rand()")) {
echo "Success!";
} else {
echo "Failure!";
}
Your statement above is checking to see if the fact that $sql is equal to
mysql_query("SELECT * FROM table ORDER BY rand()") is TRUE, but
you're using an assignment operator (=) and not a comparison operator
(==), so it should always return FALSE. (Can you use an assignment
operator in an if() statement?)
Christopher Ostmo
a.k.a. tech@AppIdeas.com
AppIdeas.com
Innovative Application Ideas
Meeting cutting edge dynamic
web site needs since the
dawn of Internet time (1995)
Business Applications:
http://www.AppIdeas.com/
Open Source Applications:
http://open.AppIdeas.com/