Re: mysql_fetch_array

From: Date: Thu, 06 Sep 2001 08:54:11 +0000
Subject: Re: mysql_fetch_array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-66175@lists.php.net to get a copy of this message
Hey Nate, _____________________________________________ <nate@1feehosting.com> wrote in message news:001001c136af$7c649db0$4b9bfea9@PlanetTelex.wolfgangster.com... Can someone tell me what i'm doing wrong here? <?php //Connect to db $db = mysql_pconnect("localhost","login","pass"); mysql_select_db("database",$db); //Check for the IP $result2 = mysql_query("SELECT ip FROM ip where ip = '$REMOTE_ADDR'",$db); while($myrow<>mysql_fetch_array($result2)) { echo "Print some text here!"; } ?> Basically I just want to print text if the IP address was not found in the database, and if it was found then I want to print "Print some text here!" _____________________________________________________ <? //Check for the IP $result2 = mysql_query("SELECT ip FROM ip where ip = '$REMOTE_ADDR'",$db); // You need to check whether or not an entry exists in the database if (mysql_num_rows($result2) == 0) { echo "No results found."; } else { // match found here while($myrow = mysql_fetch_array($result2)) { echo "Print some text here!"; } } ?> Don't you want to iterate more details from the result set about the client? Otherwise, fetching the array is pointless - you're selecting from the table and you're fetching a result that you already know the details of, because you're asking for a match on the value. You need to amend the query if you want more details. I would do something like this: Say you have the details of when they were last logged on in a value in your db called "last_login": <? $check = @mysql_query("SELECT last_login FROM table WHERE ip ='$REMOTE_ADDR'", $db) or die ("Some error."); if (mysql_num_rows($check) == 0) { echo "The user wasn't found."; } else { echo "The user has been here before. See the logins below.<br><br>"; while($row = mysql_fetch_array($check)) { $last_login = $row['last_login']; echo $last_login . "<br>"; // Do some better formatting than this } } ?> I'd do more than just use $REMOTE_ADDR too, but you get the picture. HTH, James

« previous php.general (#66175) next »