Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从所有列名和值中获取数据属性的较短方法_Php_Mysql - Fatal编程技术网

Php 从所有列名和值中获取数据属性的较短方法

Php 从所有列名和值中获取数据属性的较短方法,php,mysql,Php,Mysql,表用户有14列-id、src链接、标题 function get_atitles(){ global $db; $st = $db->query("select * from users order by name asc"); $arr = $st->fetchAll(); $ht = ''; foreach($arr as $el){ $ht .= "<div class='atitle' data-id= " . $

表用户有14列-id、src链接、标题

function get_atitles(){
    global $db;
    $st = $db->query("select * from users order by name asc");
    $arr = $st->fetchAll();
    $ht = '';
    foreach($arr as $el){
        $ht .= "<div class='atitle' data-id= " . $el['id'] . " data-src='" . $el['src'] . "... and so on for all of the 14 columns...>" . $el['title'] . "</div>\n";
    }
    echo $ht;
}
函数get_atitles(){ 全球$db; $st=$db->query(“按名称从用户订单中选择*asc”); $arr=$st->fetchAll(); $ht=''; 外汇($arr为$el){
$ht.=“可能是这样的:

foreach($arr as $el){
            $ht .= "<div class='atitle' 
                 //foreach column in users $ht .= "data-" . column_name = column_value
            $ht .= ">" . $el['title'] . "</div>\n";
        }
function get_atitles(){
    global $db;
    $st = $db->query("select * from users order by name asc");
    $arr = $st->fetchAll(PDO::FETCH_ASSOC);
    $ht = '';
    foreach($arr as $el){
        $ht = $ht . "<div class='atitle' ";
        foreach($el as $key=>$col)
            if($key != 'title')
                $ht .= "data-".$key.'="'.$col.'" '; 
        $ht.= '>'.$el['title'] . "</div>\n";
    }
    echo $ht;
}
函数get_atitles(){ 全球$db; $st=$db->query(“按名称从用户订单中选择*asc”); $arr=$st->fetchAll(PDO::FETCH_ASSOC); $ht=''; 外汇($arr为$el){
$ht=$ht。“它可以工作,但是有两个数据-数据id=0 data-0=0…等等。您可以发布$arr的示例,以便我可以在我的电脑上测试它吗?或者在您的问题上发布生成的html代码@qadenza@qadenza这是因为PDO获取数据()如何,考虑使用<代码> -FETCH(PDO::FETCHYAsSOC);< /COD>而不是< > > -FEJACT()使用
->fetch(PDO::fetch_ASSOC);
我得到错误-
为foreach()提供的参数无效。
@qadenza抱歉,我是说
->fetchAll(PDO::fetch_ASSOC);