简体   繁体   中英

easiest way to fetch specific column data from mysql in php (special case)

I want to wirte a small php file, which will convert a results from mysql into a given XML template , on a production site.So what i did (before i knew how the site DB is constructed), i've written a very small mock DB with one table and tested my script, and it seemed to work , here is a sniplet from the table and the script

+----+---------------+----------------------------+--------+
| id | house         | time_cr                    | price  |
+----+---------------+----------------------------+--------+
|  1 | Villa_Niki    | 2015-01-13 13:23:56.543294 | 25000  |
|  2 | toni          | 2015-01-13 13:24:31.133273 | 34000  |
|  3 | kolio         | 2015-01-13 13:26:06.720740 | 10000  |
|  4 | aldomirovvtxi | 2015-01-13 13:26:24.226741 | 100000 |
+----+---------------+----------------------------+--------+

then the script fetched the data into the XML ,quite straightforward

$kol=$xml->addChild('realty');
while($row=mysql_fetch_array($result))
{


 $kol->addChild('id',$row["id"]);
 $kol->addChild('creation-date',$row["time_cr"]);
 $kol->addChild('last-update-date',$row["time_cr"]);
 $kol->addChild('url','leva');
 $kol->addChild('bldtype',$row["house"]);
  ........

so just using the fetch_array and then using the column indexes and looping was fine

yesterday i have opened the database on the Site, and it turned out that they put all the information not in a separate column, like for example separate column for City, State, etc. but instead they put all the information in a single column ,like this:

so is there a simple way to make it work in this case, like for a given specific listingsdb_id, to fetch the Street into the street XML tag , Area into the area XML tag etc ? All suggestions are welcome, thanks !

This was a poorly designed database. If I were you I would re-design the database after you extract the fields/data into your XML file.

I don't have PHP/MySQL/PHPMyAdmin running on this linux distro right now so I can't really write code to help you, plus I need to sleep. Maybe I will tomorrow. I have an idea in my head how I would solve the problem, but I'm having trouble expressing it as an algorithm for extracting data from these types of poorly designed databases.

You'll probably need to query the ID field for the lowest and highest values and set variables $start, $id (current pointer), $finish. Then have something like this:

  for ($id = $start; $id <= $finish; $id++) {
      while ($row->id = $id) {
          $array[$id][$field] = $row->field;
          $array[$id][$field][$value] = $row->value;
          $row->nextRow();
      }
  }

This is just pseudo code... I'm going to bed.


Let me just point out that you're using the old MySQL PHP API. Use MySQLi... or better yet, use PDO.

Here are the docs for PDO: PDO Documentation - PHP

and for for MySQLI: PDO Documentation - PHP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM