Jump to content


Photo

is this reliable


  • Please log in to reply
6 replies to this topic

#1 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 14 September 2006 - 06:31 PM

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.
<?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 />";
                } */
}
?>

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 14 September 2006 - 06:45 PM

???
<?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();
}
?>

Tell me the problem, I will try tell you the solution

#3 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 14 September 2006 - 06:53 PM

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 14 September 2006 - 06:56 PM

I think this is the best solution:

<?php

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

?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#5 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 14 September 2006 - 07:02 PM

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
Tell me the problem, I will try tell you the solution

#6 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 14 September 2006 - 07:04 PM

Good idea, that is what I have been using, but I was making sure, I had a few instances, where it seemed inaccurate, thanks.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#7 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 14 September 2006 - 07:09 PM

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


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 syntax 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 onlyican 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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users