Problems with SQL row and column conversions

Source: Internet
Author: User
Table structure

ID KEY VAL
1 Sex Male
2 Day 365
4 Num 12345

Now I want the echo $a [' sex '] on this page to display the male echo $a [' Day '] to show 365

PHP query MySQL statement how to write convenient at any time echo it?


Reply to discussion (solution)

    Pre    -function Select_db_col ($sql _string)    {    unset ($this->records);    @mysql_free_result ($this->result);    $this->result= @mysql_query ($sql _string, $this->db), $i =1;while ($temp _rows= @mysql_fetch_array ($this result, Mysql_assoc) {for ($j =0; $j
 
  
   
  result, $j) $this->records["$temp _key_name" [$i] = $temp _rows["$ Temp_key_name "];} $i + +;}    return $this->records;    }
 
  

This is written as column output, formatted as $var[column [row]
And then
$a =array_combine ($var [' KEY '], $var [' VAL '];

To implement in MySQL, you need to write a stored procedure
You search for "crosstab", you can find multiple versions of the implementation algorithm

If implemented in PHP, it can be constructed when the query results are read
while ($row = Mysql_fetch_assoc ($rs)) {
$a [$row [' key ']] = $row [' Val '];
}

The second floor is mighty, so complex a problem a line of code to fix.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.