简体   繁体   English

使用PHP和PDO处理MySQL数据

[英]Manipulation MySQL data using PHP and PDO

I am using PHP PDO and MySQL and would like to convert a MySQL table like this: 我正在使用PHP PDO和MySQL,并想转换这样的MySQL表:

|Listing ID | Image ID |
|  1234     |     1    |
|  1234     |     2    |
|  1234     |     3    |
|  1235     |     1    |
|  1235     |     2    |

Into a PHP array like the one below, which I can parse through and insert into another MySQL table using PDO. 放入一个类似于下面的PHP数组,我可以使用PDO解析并插入到另一个MySQL表中。

[url/path/1234-1:::url/path/1234-2:::url/path/1234-3, url/path/1235-1:::url/path/1234-2]

Here is the code I've written so far. 这是我到目前为止编写的代码。 I believe there is an issue with the way the while loops are working as it only picks up the first Listing string and causes an infinite loop. 我相信while循环的工作方式存在问题,因为它仅拾取第一个Listing字符串并导致无限循环。

//Set folder path
$target_path = realpath($_SERVER['DOCUMENT_ROOT']). "\path\uploads\\";

//Query to download all listing IDs in Markers table 
$query = $db->query("SELECT * FROM image");
$row = $query->fetch(PDO::FETCH_ASSOC);
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$temp = $target_path.$row['L_ListingID']."-".$row['L_ImageID'].".jpg";
   //ListingID equals ListingID, append to array
   $LI = substr($temp, 40, 8);
   while($LI = $LI) {
    $temp = $temp.":::".$temp;
    echo $temp;
   }
}

This is the output which is an infinite loop. 这是一个无限循环的输出。 Notice that it starts at the second item in the table and hangs there: 请注意,它从表中的第二项开始并挂在那里:

C:\path\uploads\40532208-2.jpg:::C:\path\uploads\40532208-2.jpgC:\C:\path\uploads\40532208-2.jpg...ad infinitum

while($LI = $LI) causes the infinite loop. while($LI = $LI)导致无限循环。

I guess that is what you want 我想那就是你想要的

$target_path = realpath($_SERVER['DOCUMENT_ROOT']). "\path\uploads\\";
$query = $db->query("SELECT * FROM image");
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
   $temp[] = $row;
}
foreach($temp as $i=>$v){
    $line = $v['L_ListingID']."-".$v['L_ImageID'].".jpg";
    $prod[] = $line.($temp[$i] == $temp[$i+1])?":::":",";
}

echo trim(implode($prod),",");

The code is not tested, but I think that the direction is like there. 该代码未经测试,但我认为方向是正确的。

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

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