Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 1.02 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Convert MySQL result to an array strings
  2. $result = mysql_query("SELECT name FROM products");
  3. $names=array();
  4. while ($row = mysql_fetch_row($result)) $names[]=$row[0];
  5. mysql_free_result($result);
  6.        
  7. function dbGgetCol($sql) {
  8.     $ret = array();
  9.     $res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
  10.     if ($res) {
  11.       while ($row = mysql_fetch_row($result)) {
  12.         $ret[] = $row[0];
  13.       }
  14.       mysql_free_result($res);
  15.     }
  16.     return $ret;
  17.   }
  18.        
  19. $names = dbGetCol("SELECT name FROM products");
  20.        
  21. $result = mysql_query("
  22.  
  23. SELECT `productname` FROM `products`
  24.  
  25. ")or die($result."<br/><br/>".mysql_error());
  26.  
  27.     $numrows  = mysql_num_rows($result);  
  28.  
  29.     while ($row = mysql_fetch_assoc($result))
  30.     {
  31.  
  32.     $productname[$i] = $row['productname'];
  33.  
  34.             // to print out use the following
  35.              echo $productname[$i];
  36.             $i++;
  37.  
  38.     }
  39.        
  40. $names = explode('<|>', mysql_result(mysql_query(
  41.     "SELECT GROUP_CONCAT(name separator '<|>') FROM products GROUP BY 'name'"
  42. ), 0));