npsari Posted September 3, 2007 Share Posted September 3, 2007 Hi there, How can i see if $Number is available in the last 10 rows from the database I used this (but it prints the last 10 rows which contain $Number) $q = "SELECT * FROM people WHERE Number='$Number' ORDER BY Date DESC, Time DESC LIMIT 0, 10;"; $res = @mysql_query($q,$con); if(mysql_num_rows($res)>0){ print"You number exists"; }else{ print"Welcome new user"; } I actually want to see if $Number exists in the last 10 rows what should the query be guys Quote Link to comment Share on other sites More sharing options...
Fadion Posted September 3, 2007 Share Posted September 3, 2007 Using a simple syntax: $number = 10; $numberFound = false; $results = mysql_query("SELECT * FROM people ORDER BY id DESC LIMIT 10") or die(mysql_error()); while($values = mysql_fetch_array($results)){ if($values['row'] == $number){ $numberFound = true; break; } } The query will select the last 10 rows and browse through them until it founds one value which is equal to $number and make $numberFound true. Hope it helps. Quote Link to comment Share on other sites More sharing options...
npsari Posted September 3, 2007 Author Share Posted September 3, 2007 hey thanks Can i do it like that: $number = 10; $results = mysql_query("SELECT * FROM people ORDER BY id DESC LIMIT 10") or die(mysql_error()); while($values = mysql_fetch_array($results)){ if($values['row'] == $number){ $numberFound = true; }else{ $numberFound = false; } Quote Link to comment Share on other sites More sharing options...
Fadion Posted September 3, 2007 Share Posted September 3, 2007 Actually your code will make $numberFound = false if the last row isnt equal to $number. I made it so that $numberFound is initialized as false and if even one row is equal to $number, $numberFound is made true and the while is stopped. In that way you will know if any of the rows is equal to $number. Quote Link to comment Share on other sites More sharing options...
npsari Posted September 3, 2007 Author Share Posted September 3, 2007 ohh, i got it now i will try it out and see how it goes so what does break mean if u have time to explain to me and what is row should i only write the world row i got confued on it cus i dont know where is it coming from Quote Link to comment Share on other sites More sharing options...
Fadion Posted September 3, 2007 Share Posted September 3, 2007 break will stop the execution of a for loop, while and switch. row was just a placeholder for the column name. U should change it to your column name. Imagine having a table like this: (table 'users') id -- name -- age 1 -- john -- 30 2 -- smith -- 25 $results = mysql_query("SELECT * FROM users"); while($values = mysql_fetch_array($results)){ echo $values['name'] . " " . $values['age'] . "<br />"; } The code will echo all the fields of the table for the columns 'name' and 'value'. Does it make sense now? Quote Link to comment Share on other sites More sharing options...
npsari Posted September 3, 2007 Author Share Posted September 3, 2007 hey, thanks for the info GuiltyGear! i am on the case byez Quote Link to comment Share on other sites More sharing options...
Fadion Posted September 3, 2007 Share Posted September 3, 2007 If u dont have any other question pls mark the topic as solved. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.