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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题