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 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?