繁体   English   中英

而mysql_fetch_assoc为数组

[英]While mysql_fetch_assoc to array

我正在尝试转换为数组

我有mysql查询:

$shots = mysql_query("SELECT name, ext FROM my_images WHERE item_id=$idnumber") or die(mysql_error());
while($row = mysql_fetch_assoc($shots)) {
 echo "http://www.example.com/images/item/";
 echo $row["name"];
 echo '_thb.';
 echo $row["ext"]; 
 echo '<br>';
}

它给了我:

http://www.example.com/images/item/image1_thb.jpg
http://www.example.com/images/item/image2_thb.jpg
http://www.example.com/images/item/image3_thb.jpg

这样工作正常。

我需要把它放在:

$files_to_zip = array(
    'http://www.example.com/images/item/image1_thb.jpg',
    'http://www.example.com/images/item/image2_thb.jpg',
    'http://www.example.com/images/item/image3_thb.jpg',
);

我该怎么办? 谢谢

您应该使用PDO连接数据库,因为它更加安全: http : //php.net/manual/en/book.pdo.php

这些教程非常不错: http : //wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers http://crewow.com/PHP-MySQL-Simple-Select-using-PDO-in-Bootstrap.php


我认为您需要这样的东西: http : //php.net/manual/en/pdostatement.fetchall.php

<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>

结果将是这样的:

Fetch all of the remaining rows in the result set: 

    Array (
        [0] => Array
            (
                [name] => apple
                [0] => apple
                [colour] => red
                [1] => red
            )
        [1] => Array
            (
                [name] => pear
                [0] => pear
                [colour] => green
                [1] => green
            )
        [2] => Array
            (
                [name] => watermelon
                [0] => watermelon
                [colour] => pink
                [1] => pink
            )
    )

您可以使用array_push()

while($row = mysql_fetch_assoc($shots)) {
   array_push($a, "http://www.example.com/images/item/".$row['name']."_thb.".$row['ext']);
}

print_r($a);

而且我不建议使用mysql_因为它已被弃用

暂无
暂无

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

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