gpt4 book ai didi

PHP回显乱序

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:02 24 4
gpt4 key购买 nike

我用 PHP 编写了一个脚本,它从 MySQL 数据库中获取值并将这些值输出为 JSON。但是,我遇到了问题。这是代码:

    <?php 
mysql_connect("localhost", "root", "");
mysql_select_db("stitchdex");
$query = mysql_query("SELECT image, price, description FROM {$_GET['table']}");
$jsonString = "{\"images\":[";
$numrows = mysql_num_rows($query);
$i=0;
while ( $row = mysql_fetch_array($query) ) {
$jsonString .= "{\"image\":\"{$row['image']}\", \"price\":\"{$row['price']}\", \"description\":\"{$row['description']}\"}";
if ( ($numrows-1) == $i ) echo "";
else echo ",";
$i++;
}
$jsonString .= "]}";
echo $jsonString;
?>

但它输出:

,,,,,,,,,,,,,,,,,{"images":[{"image":"priceguide1.jpg", "price":"", "description":"Diagram of Options"}{"image":"2ndamendfront.jpg", "price":"$109.00", "description":"Total of 3 laser etchings"}{"image":"DSC_0120.jpg", "price":"$99.00", "description":"Designer Kydex 1 Laser Etching"}{"image":"DSC_0121.jpg", "price":"", "description":"Lazer Etching on Back"}{"image":"gilbert.jpg", "price":"$109.00", "description":"Designer Kydex 2 Lazer Etchings"}{"image":"glock23-carbonfiber-red1.jpg", "price":"$79.00", "description":""}{"image":"irish1.jpg", "price":"$129.00", "description":"Designer Kydex Two Tone Leather & 3 Laser Etchings Two on front One on Back"}{"image":"irish2.jpg", "price":"$129.00", "description":"Designer Kydex Two Tone Leather & 3 Laser Etchings Two on front One on Back"}{"image":"klover-glock-2.jpg", "price":"$129.00", "description":"Designer Kydex Two Tone Leather & 3 Laser Etchings Two on front One on Back"}{"image":"priceguide1.jpg", "price":"", "description":"Diagram of Options"}{"image":"2ndamendfront.jpg", "price":"$109.00", "description":"Total of 3 laser etchings"}{"image":"DSC_0120.jpg", "price":"$99.00", "description":"Designer Kydex 1 Laser Etching"}{"image":"DSC_0121.jpg", "price":"", "description":"Lazer Etching on Back"}{"image":"gilbert.jpg", "price":"$109.00", "description":"Designer Kydex 2 Lazer Etchings"}{"image":"glock23-carbonfiber-red1.jpg", "price":"$79.00", "description":""}{"image":"irish1.jpg", "price":"$129.00", "description":"Designer Kydex Two Tone Leather & 3 Laser Etchings Two on front One on Back"}{"image":"irish2.jpg", "price":"$129.00", "description":"Designer Kydex Two Tone Leather & 3 Laser Etchings Two on front One on Back"}{"image":"klover-glock-2.jpg", "price":"$129.00", "description":"Designer Kydex Two Tone Leather & 3 Laser Etchings Two on front One on Back"}]}

如果逗号分隔“图像”数组中的对象,这将是有效的 JSON,但出于某种原因,脚本首先输出所有逗号。这是为什么?

最佳答案

我建议不要手动构建 json 字符串,为此使用 json_encode(),这就是它存在的原因。

$data = array();
while ( $row = mysql_fetch_assoc($query) ) {
$data[] = $row;
}

echo json_encode(array('images' => $data));

旁注:另外,使用改进的扩展,即 mysqli 或 PDO,并利用准备好的语句。您当前的代码直接在查询中使用 $_GET 变量,容易受到 sql 注入(inject)的攻击。绑定(bind)它们以生成更安全的查询。

关于PHP回显乱序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26051002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com