doutidi5037 2014-08-27 14:29
浏览 19
已采纳

如何将两个sql行添加到php数组中

I want to add two values from two rows to an array. One is a path and the other is a tag. I'm then encoding the array. So I can display the image and then I'm going to using the tags to order the new javascript array dynamically. I would like "echo $data;" to say something like [{path, tag}, {path, tag}]

<?php

include("mysqlconnect.php");

$select_query = "SELECT `ImagesPath`,`Tag` FROM `offerstbl` ORDER by `ImagesId` DESC";
$sql = mysql_query($select_query) or die(mysql_error());   
$data = array();
while($rows = mysql_fetch_array($sql,MYSQL_BOTH)){
$data[] = $rows['ImagesPath']['Tag'];
}
echo json_encode($data);
echo $data[0];
?>
  • 写回答

1条回答 默认 最新

  • doubi4814 2014-08-27 14:53
    关注

    I'm going to guess that "h" is the first character in the ImagePath string that is being returned from $rows['ImagesPath']['Tag']. I'm guessing that $rows['ImagePath'] is a string and you've asked for the ['Tag'] key within that string. Which isn't found for obvious reasons and the first character is then returned.

    Since ImagePath and Tag are separate columns in the table you can't access both simultaneously. If you want the output to be [{path, tag}, {path, tag}] then you will need to change:

    $data[] = $rows['ImagesPath']['Tag'];
    

    to

    $data[] = array($rows['ImagesPath'], $rows['Tag']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置
  • ¥15 Java计划序号重编制功能,此功能会对所有序号重新排序,排序后不改变前后置关系。
  • ¥15 关于哈夫曼树应用得到一些问题