Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 10-05-2010
Member
 
Join Date: Mar 2010
Posts: 200
PHP mysql select unique records from one table

Hello,
I am looking for a php code to generate a unique record from sql database. I have no idea how to do this. If you know then please post here, it would really help me a lot. Thank you for your help.
Reply With Quote
  #2  
Old 10-05-2010
Member
 
Join Date: Nov 2009
Posts: 359
Re: PHP mysql select unique records from one table

Hello,
I think this can solve your problem, just give it a try

Code:
$qry = "select DISTINCT name FROM table WHERE name LIKE string"; 
$rsl = my_qry($qry) or die (mysql_error()); 
//$row = mysql_fetch_array($rsl); 
while ($row = mysql_fetch_array($rsl)) { 
echo $row["name"]; 
}
Reply With Quote
  #3  
Old 10-05-2010
Member
 
Join Date: Nov 2009
Posts: 335
Re: PHP mysql select unique records from one table

Hello,
You can try the following code , though I am not sure that this is the perfect code that your are looking for
Code:
<?php 
class appmd extends md {

function isunq($field, $value, $id)
    {
        $flds[$this->name.'.'.$field] = $value;
        if (empty($id))
            // add 
            $flds[$this->name.'.id'] = "<> NULL"; 
        else
            // edit
            $flds[$this->name.'.id'] = "<> $id"; 
        
        $this->recursive = -1;
        if ($this->hasAny($flds))
        {
            $this->invalidate('unique_'.$field); 
            return false;
        }
        else 
            return true;
   }

?>
Reply With Quote
  #4  
Old 10-05-2010
Member
 
Join Date: Nov 2009
Posts: 333
Re: PHP mysql select unique records from one table

Hello,
Alternatively you can use this code
Code:
<?php 
if ($this->User->unq('username', $this->data['User']['username'], $user_id))
    { 
        if ($this->User->save($this->data))
        {
           $this->flash('User has been saved.','/users/index');
        }
     }
?>
Reply With Quote
  #5  
Old 10-05-2010
Member
 
Join Date: Nov 2009
Posts: 446
Re: PHP mysql select unique records from one table

Hello,
Take a look at the following code
Code:
if (empty($id)){
// add
$cnd = $this->name.".".$flds." = '".$value."'";
}else{
// edit
$flds[$this->name.'.id'] = "<> $id";
$conditions = '('.$this->name.".".$flds." = '".$value."') AND (".$this->name.".id <> $id)";
}
Reply With Quote
  #6  
Old 12-05-2010
Member
 
Join Date: Nov 2009
Posts: 518
Re: PHP mysql select unique records from one table

Hello,
Try the following code
Code:
if (empty($id))
// add
$flds[$this->name.'.id'] = "!= NULL";
else
// edit
$flds[$this->name.'.id'] = "!= $id";
Reply With Quote
  #7  
Old 13-05-2010
Member
 
Join Date: Nov 2009
Posts: 518
Re: PHP mysql select unique records from one table

Hello,
Check the modified code

Code:
$qry = "select DISTINCT name FROM `atable` WHERE name LIKE '%john%'";
$rst = mysqlqry($qry) or die (mysql_error());
$row = mysqlarry($rst);
while ($row = mysqlarry($rst)) {
echo $row["name"];
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "PHP mysql select unique records from one table"
Thread Thread Starter Forum Replies Last Post
PHP - I want to retrieve records from specific field from a MySQL table and build array using PHP bond007dev Software Development 3 19-07-2010 04:23 PM
Select unique records from a table Conraad Software Development 5 13-05-2010 12:01 AM
Count unique records with sql kyosang Software Development 3 25-12-2009 12:45 PM
How to select multiple table in MySql Doroteo Software Development 4 05-11-2009 09:28 PM
SELECT UNIQUE and SELECT DISTINCT TAARIQ Software Development 3 03-06-2009 10:00 PM


All times are GMT +5.5. The time now is 11:48 AM.