简体   繁体   English

在php中从mysql获取特定列数据的最简单方法(特殊情况)

[英]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 我想在生产站点上写一个小的php文件,它将mysql的结果转换成给定的XML模板。所以我做了什么(在我不知道站点数据库是如何构造的之前),我写了一个很小的文件用一个表模拟数据库并测试了我的脚本,它似乎可以正常工作,这是该表和脚本的片段

+----+---------------+----------------------------+--------+
| 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 然后脚本将数据提取到XML中,非常简单

$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 所以只使用fetch_array然后使用列索引和循环就可以了

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 ? 那么有没有一种简单的方法可以使其在这种情况下工作,例如对于给定的特定listingsdb_id,将Street提取到street XML标记中,将Area提取到area XML标记中? 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. 如果您是我,则在将字段/数据提取到XML文件之后,我将重新设计数据库。

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. 我现在没有在此Linux发行版上运行PHP / MySQL / PHPMyAdmin,因此我无法真正编写代码来帮助您,而且我需要睡觉。 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. 您可能需要查询ID字段的最小值和最大值,并设置变量$ start,$ id(当前指针),$ 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. 让我指出您正在使用旧的MySQL PHP API。 Use MySQLi... or better yet, use PDO. 使用MySQLi ...或更好,使用PDO。

Here are the docs for PDO: PDO Documentation - PHP 以下是 PDO的文档 PDO文档-PHP

and for for MySQLI: PDO Documentation - PHP 对于MySQLI: PDO文档 -PHP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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