Jump to content

is this reliable


Ninjakreborn

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]
Link to comment
https://forums.phpfreaks.com/topic/20757-is-this-reliable/
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]
Link to comment
https://forums.phpfreaks.com/topic/20757-is-this-reliable/#findComment-91886
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.
Link to comment
https://forums.phpfreaks.com/topic/20757-is-this-reliable/#findComment-91893
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
Link to comment
https://forums.phpfreaks.com/topic/20757-is-this-reliable/#findComment-91905
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.
Link to comment
https://forums.phpfreaks.com/topic/20757-is-this-reliable/#findComment-91910
Share on other sites

Archived

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

×
×
  • Create New...

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.