0

I want to select only unique values with php/mysql.

I can do it with many line, but I forget how to do it without while... :)

Thanks a lot.

Here is the code that I want to do without while.

$request_1m = "SELECT date1, date2 from mytable";

$result_1m = mysql_query($request_1m,$db);

while($row = mysql_fetch_array($result_1m))
{
    /* Get the data from the query result */    
    $date1_1m = $row["date1"];
    $date2_1m = $row["date2"];  
}
Bizboss
  • 7,792
  • 27
  • 109
  • 174
  • 1
    You're going to need the `while` loop to load your mysql result resource into an associative or numeric indexed array with `mysql_fetch_assoc`. Are you looking for a single function call to get all results from your mysql result resource? `SELECT DISTINCT column FROM table` will get your unique values – Michael Berkowski Mar 04 '11 at 14:48

6 Answers6

3

mysql_fetch_assoc + SELECT with DISTINCT

hsz
  • 148,279
  • 62
  • 259
  • 315
  • I know nothing about your problem so I cannot provide you any code. Global examples you have in references. – hsz Mar 04 '11 at 14:47
2

I'm not sure I understand your question, but here's what I think you want to do :

$request_1m = "SELECT date1, date2 from mytable";

$result_1m = mysql_query($request_1m,$db);

list($date1_1m, $date2_1m) = mysql_fetch_row($result_1m);

Note that this will only get the first row from the result set (just as if you LIMIT 1)

Julien
  • 525
  • 3
  • 9
0

Heres how I do it and Json encode after. This will ensure it will encode only UNIQUE json Values (Without duplicates) as an example

$tbl_nm = "POS_P";
$prod_cat = "prod_cat";

//Select from the POS_P Table the Unique Product Categories using the DISTINCT syntax
$sql = "SELECT DISTINCT $prod_cat FROM $tbl_nm";

//Store the SQL query into the products variable below.
$products = mysql_query($sql);

if ($products){

    // Create an array
    $rows = array(); 

    // Fetch and populate array
    while($row = mysql_fetch_assoc($products)) { 

    $rows[]=$row; 

    } 

    // Convert array to json format
    $json = json_encode(array('Categories'=>$rows));
    echo $json;

}
//Close db connection when done
mysql_close($con);

?>
Alex McPherson
  • 3,185
  • 3
  • 30
  • 41
0

like this?

$dbresult = mysql_query("SELECT DISTINCT field FROM table");

$result = array();
while ($row = mysql_fetch_assoc($dbresult))
{
     $result[] = $row;
}

This gets you all unique values from "field" in table "table".

hcb
  • 8,147
  • 1
  • 18
  • 17
0

If you really wish to avoid the while loop, you can use the PHP PDO objects, and in particular call the PDO fetchAll() method to retrieve the complete results array in one go. PDO fetchAll() documentation

$db = new PDO('dblib:host=your_hostname;otherparams...');
$db->query("SELECT DISTINCT col FROM table");
$results = $db->fetchAll();

// All your result rows are now in $results
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
0

That is very easy, take out the while, like below

 $row = mysqli_fetch_assoc($result);
 $date1_1m = $row["date1"];
Webdeveloper_Jelle
  • 2,868
  • 4
  • 29
  • 55
Noe
  • 1
  • 1