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 Link to comment https://forums.phpfreaks.com/topic/67830-solved-check-if-numer-is-in-the-last-10-rows/ 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. Link to comment https://forums.phpfreaks.com/topic/67830-solved-check-if-numer-is-in-the-last-10-rows/#findComment-340937 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; } Link to comment https://forums.phpfreaks.com/topic/67830-solved-check-if-numer-is-in-the-last-10-rows/#findComment-340939 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. Link to comment https://forums.phpfreaks.com/topic/67830-solved-check-if-numer-is-in-the-last-10-rows/#findComment-340946 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 Link to comment https://forums.phpfreaks.com/topic/67830-solved-check-if-numer-is-in-the-last-10-rows/#findComment-340953 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? Link to comment https://forums.phpfreaks.com/topic/67830-solved-check-if-numer-is-in-the-last-10-rows/#findComment-340965 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 Link to comment https://forums.phpfreaks.com/topic/67830-solved-check-if-numer-is-in-the-last-10-rows/#findComment-340969 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. Link to comment https://forums.phpfreaks.com/topic/67830-solved-check-if-numer-is-in-the-last-10-rows/#findComment-340978 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.