Re: PHP/MySQL & Control Structures -> if (mysql_query())

From: Date: Wed, 21 Mar 2001 05:45:36 +0000
Subject: Re: PHP/MySQL & Control Structures -> if (mysql_query())
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-44879@lists.php.net to get a copy of this message
Hi David, I counted the number of rows using mysql_numrows(), but I've been told that count() is way faster. Unfortunately, I can't seem to get count working.. Any ideas? <?php echo mysql_numrows(mysql_query("select * FROM phPetition",$db)); // This should be faster, but doesn't seem to work // echo count(mysql_query("select * FROM phPetition",$db)); ?> David Robley wrote: > $same_Email_query is a pointer to a result set from the query and will > always be true if the query succeeded - where succeeded means something > like 'returned 0 or more matching rows' > What you need to do is check how many matching rows are returned by your > query; if there are zero rows, the value doesn't exist in the database. Great advice. This worked, therefore eliminating duplicates: $same_Email_query = mysql_numrows(mysql_query("SELECT Email FROM phPetition WHERE Email='" . $Email . "'")); if($same_Email_query!=0){ $error = 1; $error_html .= "This email \"" . $Email . "\" has already been used to sign the petition.<br><br>\n"; } > Shouldn't $individuals_array[ be $sameIP_array[ ? Consider using extract; > if nothing else it makes code easier to read :-) This was a neat little solution with the extract code. I found that it started to mess up my existing scripts so I threw it in a function to keep the variables from getting mixed up. <? function recent_signatures () { $individuals_query = mysql_query("SELECT * FROM phPetition ORDER BY ID DESC LIMIT 0,5"); while ($individuals_array = mysql_fetch_array($individuals_query)) { echo "<LI>" . stripslashes($individuals_array["FirstName"]) . " " . stripslashes($individuals_array["LastName"]) . " from " . stripslashes($individuals_array["CityState"]); $last_email=$individuals_array["Email"]; } $sameIP_query = mysql_query("SELECT FirstName,LastName,CityState,IPAddress FROM phPetition WHERE IPAddress='" . getenv('REMOTE_ADDR') . "' ORDER BY LastName"); if (mysql_numrows($sameIP_query) != 0) { echo "<p>The following individuals have signed this petition from the same IP: " . getenv('REMOTE_ADDR') . "<br>"; while ($sameIP_array = mysql_fetch_array($sameIP_query)) { extract ($sameIP_array); echo "<LI>" . stripslashes($FirstName) . " " . stripslashes($LastName) . " from " . stripslashes($CityState); // echo "<LI>" . stripslashes($sameIP_array["FirstName"]) . " " . stripslashes($sameIP_array["LastName"]) . " from " . stripslashes($sameIP_array["CityState"]); } } } recent_signatures (); ?> Thanks Again. Mike -- Mike Gifford, OpenConcept Consulting, http://openconcept.ca Offering everything your organization needs for an effective web site. Latest Featured Site: http://www.lornenystrom.org/ No problem can be solved with the same thinking that created it - A.Einstein

« previous php.general (#44879) next »