Jump to content

[SOLVED] Check if $Numer is in the last 10 rows


npsari

Recommended Posts

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

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.

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;
}

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.

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

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.