Results 1 to 7 of 7

Thread: PHP mysql select unique records from one table

  1. #1
    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.

  2. #2
    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"]; 
    }

  3. #3
    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;
       }
    
    ?>

  4. #4
    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');
            }
         }
    ?>

  5. #5
    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)";
    }

  6. #6
    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";

  7. #7
    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"];
    }

Similar Threads

  1. Replies: 3
    Last Post: 19-07-2010, 04:23 PM
  2. Select unique records from a table
    By Conraad in forum Software Development
    Replies: 5
    Last Post: 13-05-2010, 12:01 AM
  3. Count unique records with sql
    By kyosang in forum Software Development
    Replies: 3
    Last Post: 25-12-2009, 12:45 PM
  4. How to select multiple table in MySql
    By Doroteo in forum Software Development
    Replies: 4
    Last Post: 05-11-2009, 09:28 PM
  5. SELECT UNIQUE and SELECT DISTINCT
    By TAARIQ in forum Software Development
    Replies: 3
    Last Post: 03-06-2009, 10:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,495,457.55082 seconds with 17 queries