查询是空的PHP MySQL表错误

时间:2014-05-16 18:13:19

标签: php mysql

在这个php脚本中,我访问了一个名为“album_name”的主表,从而根据表'album_name'访问另一个动态表。当我试图通过嵌套2循环访问它时出现错误。然后我尝试从主表'album_name'中检索值并将其存储到数组中。然后只将它放入另一个循环中以访问从主表中收到名称的动态表。但它显示“查询为空”

<?php       
    $i=0;
    $querya = mysqli_query($con,"SELECT * FROM album_name");
    while($data = mysqli_fetch_array($querya)) {
        $alb['$i']=str_replace(' ', '', $data['album_id']);
        $i++;
        //echo '<div class="tab-pane fade in active" id="'.str_replace(' ', '', $data['album_id']).'">';
    }
    foreach ($alb as $albu ) {
            # code...
        $queryb = mysqli_query($con,"SELECT * FROM ".$albu);
        $result = mysql_query($queryb) or die(mysql_error());
        while($row = mysql_fetch_array($result )) {
            echo '<div class="tab-pane fade in active" id="'.$albu.'">';
            echo '<a href="" data-toggle="modal" data-target=".'.$alb['$i'].'">';   
            echo '<img src="images/gallery/'.$row['img_title'].'.jpg" class="img-rounded col-md-3 gal-img" /></a>';
            echo '<!-- Modal -->
                    <div class="modal fade '.$row['album_name'].'" id="myModal-1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                    <h4 class="modal-title" id="myModalLabel">'.$row['img_title'].'</h4>
                                </div>
                                <div class="modal-body">
                                <img src="images/doctors/dr.maya/'.$row['img_title'].'.jpg" class="gal-img-pop" />
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                </div>
                            </div>
                        </div>
                    </div>';
        }
        $i--;
    }                  
?>

1 个答案:

答案 0 :(得分:1)

删除$ i周围的引号。改变这个:

 $alb['$i']=str_replace(' ', '', $data['album_id']);

进入这个:

 $alb[$i]=str_replace(' ', '', $data['album_id']);

这是完整的代码:

<?php       
    $i=0;
    $querya = mysqli_query($con,"SELECT * FROM album_name");
    while($data = mysqli_fetch_array($querya)) {
        $alb[$i]=str_replace(' ', '', $data['album_id']);
        $i++;
        //echo '<div class="tab-pane fade in active" id="'.str_replace(' ', '', $data['album_id']).'">';
    }
    foreach ($alb as $albu ) {
            # code...
        $queryb = mysqli_query($con,"SELECT * FROM ".$albu);

        while($row = mysqli_fetch_array($queryb)) {
            echo '<div class="tab-pane fade in active" id="'.$albu.'">';
            echo '<a href="" data-toggle="modal" data-target=".'.$alb['$i'].'">';   
            echo '<img src="images/gallery/'.$row['img_title'].'.jpg" class="img-rounded col-md-3 gal-img" /></a>';
            echo '<!-- Modal -->
                    <div class="modal fade '.$row['album_name'].'" id="myModal-1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                    <h4 class="modal-title" id="myModalLabel">'.$row['img_title'].'</h4>
                                </div>
                                <div class="modal-body">
                                <img src="images/doctors/dr.maya/'.$row['img_title'].'.jpg" class="gal-img-pop" />
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                </div>
                            </div>
                        </div>
                    </div>';
        }
        $i--;
    }                  
?>