Ninjakreborn Posted September 14, 2006 Share Posted September 14, 2006 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]<?phpif (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 More sharing options...
onlyican Posted September 14, 2006 Share Posted September 14, 2006 ???[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 resulltif(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 failsecho ".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 More sharing options...
Ninjakreborn Posted September 14, 2006 Author Share Posted September 14, 2006 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 More sharing options...
Orio Posted September 14, 2006 Share Posted September 14, 2006 I think this is the best solution:[code]<?phpif (mysql_num_rows($testemailquery)>0){$errorhandler .= "The email address was already in the database.<br />";}?>[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/20757-is-this-reliable/#findComment-91897 Share on other sites More sharing options...
onlyican Posted September 14, 2006 Share Posted September 14, 2006 oklets explain each oneif ($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 trueif (mysql_fetch_array($testemailquery)) {This would be the same as above, except the naming bitif (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 showif ($numrows = mysql_num_rows($testemailquery)) {agian, naming $numrows with the value, which would always return trueif (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 DatabaseSo 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 More sharing options...
Ninjakreborn Posted September 14, 2006 Author Share Posted September 14, 2006 Good idea, that is what I have been using, but I was making sure, I had a few instances, where it seemed inaccurate, thanks. Link to comment https://forums.phpfreaks.com/topic/20757-is-this-reliable/#findComment-91908 Share on other sites More sharing options...
obsidian Posted September 14, 2006 Share Posted September 14, 2006 [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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.