dsovc00684 2014-08-24 23:07
浏览 59
已采纳

用SQL更新php数组

I am a noob, but trying my best to get this to work, i have been looking the entire night and still can not get it to work basically,

i need an array from SQL to be printed out like this.

$data = array(
 array(
'id' => "1",
'title' => "First image",
'url' => "http://www.example.org/1",
'width' => "200",
'height' => "283",
'image' => "../sample-images/image_1_big.jpg",
'preview' => "../sample-images/image_1.jpg"
),
 array(
'id' => "2",
'title' => "Second image",
'url' => "http://www.example.org/2",
'width' => "200",
'height' => "300",
'image' => "../sample-images/image_2_big.jpg",
'preview' => "../sample-images/image_2.jpg"
  ),
 array(
'id' => "3",
'title' => "Third image",
'url' => "http://www.example.org/3",
'width' => "200",
'height' => "252",
'image' => "../sample-images/image_3_big.jpg",
'preview' => "../sample-images/image_3.jpg"
 ),
  array(
'id' => "4",
'title' => "Fourth image",
'url' => "http://www.example.org/4",
'width' => "200",
'height' => "158",
'image' => "../sample-images/image_4_big.jpg",
'preview' => "../sample-images/image_4.jpg"
  ),
  array(
'id' => "5",
'title' => "Fifth image",
'url' => "http://www.example.org/5",
'width' => "200",
'height' => "300",
'image' => "../sample-images/image_5_big.jpg",
'preview' => "../sample-images/image_5.jpg"
  ),
  array(
'id' => "6",
'title' => "Sixth image",
'url' => "http://www.example.org/6",
'width' => "200",
'height' => "297",
'image' => "../sample-images/image_6_big.jpg",
'preview' => "../sample-images/image_6.jpg"
 ),
 array(
'id' => "7",
'title' => "Seventh image",
'url' => "http://www.example.org/7",
'width' => "200",
'height' => "200",
'image' => "../sample-images/image_7_big.jpg",
'preview' => "../sample-images/image_7.jpg"
),
 array(
'id' => "8",
'title' => "Eight image",
'url' => "http://www.example.org/8",
'width' => "200",
'height' => "200",
'image' => "../sample-images/image_8_big.jpg",
'preview' => "../sample-images/image_8.jpg"
  ),
  array(
'id' => "9",
'title' => "Ninth image",
'url' => "http://www.example.org/9",
'width' => "200",
'height' => "398",
'image' => "../sample-images/image_9_big.jpg",
'preview' => "../sample-images/image_9.jpg"
 ),
  array(
'id' => "10",
'title' => "Tenth image",
'url' => "http://www.example.org/10",
'width' => "200",
'height' => "267",
'image' => "../sample-images/image_10_big.jpg",
'preview' => "../sample-images/image_10.jpg"
  )
 );

At the moment i have managed to get a printout like this

 Array
  (
   [0] => Array
    (
        [0] => 1
        [id] => 1
        [1] => First image
        [title] => First image
        [2] => http://www.example.org/1
        [url] => http://www.example.org/1
        [3] => 200
        [width] => 200
        [4] => 283
        [height] => 283
        [5] => ../sample-images/image_1_big.jpg
        [image] => ../sample-images/image_1_big.jpg
        [6] => ../sample-images/image_1.jpg
        [preview] => ../sample-images/image_1.jpg
    )

[1] => Array
    (
        [0] => 2
        [id] => 2
        [1] => Second image
        [title] => Second image
        [2] => http://www.example.org/2
        [url] => http://www.example.org/2
        [3] => 200
        [width] => 200
        [4] => 300
        [height] => 300
        [5] => ../sample-images/image_2_big.jpg
        [image] => ../sample-images/image_2_big.jpg
        [6] => ../sample-images/image_2.jpg
        [preview] => ../sample-images/image_2.jpg
    )

  [2] => 
)

my code at the moment is this,

 <?php
 // Our data source
 $conn = mysql_connect("IP", "USERNAME", "PASSWORD");

 if (!$conn) {
     echo "Unable to connect to DB: " . mysql_error();
     exit;
 }

 if (!mysql_select_db("MYSQLDB")) {
     echo "Unable to select mydbname: " . mysql_error();
     exit;
 }
 //= Query ========================//
 $sql=mysql_query("select * from DB_TABLE");


 while($data[]=mysql_fetch_array($sql));

 echo "<pre>";

 print_r ($data);

 echo "</pre>";

 ?>
  • 写回答

3条回答 默认 最新

  • doubeishuai6598 2014-08-24 23:30
    关注

    If you want the particular formatting that you posted then this should work (I didn't test it):

    $foo = "array (
    ";
    while ($row=mysql_fetch_assoc($sql)){
        $foo.= "array (
    ";
        foreach ($row as $key => $value){
            $foo .= "'{$key}' => \"{$value}\",
    ";
        }
        $foo = substr($foo,0,strlen($foo)-2)."
    ";//removes the comma at the end
        $foo .="),
    ";
    }
    $foo = substr($foo,0,strlen($foo)-2)."
    ";//removes the comma at the end
    $foo .= ');';
    echo '<pre>'.$foo.'</pre>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c