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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.