Jump to content

Archived

This topic is now archived and is closed to further replies.

Ninjakreborn

is this reliable

Recommended Posts

I want to know which of these will guarantee I get an appropriate result.  I have the whole code below, then for each part where I want to know about, I have them in comments.  I am wondering which methods for testing the database for the existence of something is 100% affective, to make sure there is no entries in the database.
[code]<?php
if (isset($email)) {
$testemail = "SELECT email FROM userinfo WHERE email = '$email';";
$testemailquery = mysql_query($testemail);
/* if ($row = mysql_fetch_array($testemailquery)) {
                $errorhandler .= "The email address was already in the database.<br />";
                } */
/* if (mysql_fetch_array($testemailquery)) {
                $errorhandler .= "The email address was already in the database.<br />";
                } */
/* if (mysql_num_rows($testemailquery)) {
                $errorhandler .= "The email address was already in the database.<br />";
                } */
/* if ($numrows = mysql_num_rows($testemailquery)) {
                $errorhandler .= "The email address was already in the database.<br />";
                } */
/* if (mysql_query($testemail))
                $errorhandler .= "The email address was already in the database.<br />";
                } */
}
?>[/code]

Share this post


Link to post
Share on other sites
???
[code]
<?php
$query = "SELECT .....";
//Too tired to type the query
$result = mysql_query($query);
if($result){
//Query worked, dont know about if there was a resullt
if(mysql_num_rows($result) != 0){
//Query worked, More than 0 Rows found
}else{
echo "There are no results found";
}
}else{
//This is if the query to the DB fails
echo ".There has been an error,<br />".mysql_error();
}
?>
[/code]

Share this post


Link to post
Share on other sites
There is an easier way that than, all the ones I listed seem to work to an extent, but a few of them seem to be circumstantial, and not totally accurate, I am trying to find out which one of those statements are the most accurate, or are totally accurate.

Share this post


Link to post
Share on other sites
I think this is the best solution:

[code]<?php

if (mysql_num_rows($testemailquery)>0){
$errorhandler .= "The email address was already in the database.<br />";
}

?>[/code]

Orio.

Share this post


Link to post
Share on other sites
ok
lets explain each one

if ($row = mysql_fetch_array($testemailquery)) {
This gets the results, and places them in an array, if there is results, this value would be true,
but naming that value to $row, if the value is false, $row would be false, so I think this would always be true

if (mysql_fetch_array($testemailquery)) {
This would be the same as above, except the naming bit

if (mysql_num_rows($testemailquery)) {
mysql_num_rows can return 0, which in most cases, is the same as false, so if false, the statement will show

if ($numrows = mysql_num_rows($testemailquery)) {
agian, naming $numrows with the value, which would always return true


if (mysql_query($testemail))
This checks if the query has been done, for this to fail, there has to be a problem like error in the query, or unable to connect to the Database

So back to my first comment, using mysql_num_rows I think is best to check the numbe of rows

Share this post


Link to post
Share on other sites
Good idea, that is what I have been using, but I was making sure, I had a few instances, where it seemed inaccurate, thanks.

Share this post


Link to post
Share on other sites
[quote author=onlyican link=topic=108055.msg434349#msg434349 date=1158260534]
if ($row = mysql_fetch_array($testemailquery)) {
This gets the results, and places them in an array, if there is results, this value would be true,
but naming that value to $row, if the value is false, $row would be false, so I think this would always be true
[/quote]

this method actually won't always be true. if you run your query, and then you try to run a fetch on it when there were no records returned, you'll get a SQL error, not a [b]syntax[/b] error, mind you, but a good ol' SQL error. if you have error reporting turned on, you'll see that every time. in my mind, that's enough reason not to use it. as [i]onlyican[/i] suggested, mysql_num_rows() is usually the best result, but you definitely want to check it against something, not just leave it in the if statement by itself.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.