日期:2014-05-17  浏览次数:20747 次

求教一个php调用数据库输出的问题
目的是做一个 选择范围然后 通过这些范围查询数据库并格式化输出结果的

但是做到输出就出问题了 弄了半天很不理解
以下是错误部分代码
PHP code

mysql_query('set names gbk',$conn);
mysql_select_db('htc',$conn);
$count = 'select count(*) from phone';
$query = mysql_query($count,$conn);
//记录总数
$recordcount = mysql_result($query, 0,0);
//每页多条
$pagesize = 5;
//总页数
$pagecount = ceil($recordcount/$pagesize);
//当前页
$currpage = 1;
if($_GET){
    $currpage = (int)$_GET['p'];
    }
    $currpage = $currpage<1 ? 1 : $currpage;
    $currpage = $currpage>$pagecount ? $pagecount : $currpage;
                    
    $start = ($currpage-1)*$pagesize;
    $sql = "select * from phone order by id desc limit $start,$pagesize";
    $result = mysql_query($sql,$conn);
    while($row=mysql_fetch_array($result)){
    foreach ($row as $k=>$va){
    //echo '<pre>';
    //print_r($row);
                            
    echo "<div class='p_list'>";
    echo "<div class='p_list_pic'><a><img src="."'image/'.$va[13]".">"."</img></a></div>";
    echo "<div class='p_list_con'>
        <p class='p_list_con_title'>$va[1]</p><br>
            $va[4],$va[5]<br>
            $va[6],$va[7]<br>
            $va[8],$va[9]<br>  
            <p class='p_list_con_state'>$va[10]</p></div>";
            echo "<div>售价:¥$va[3]<br>
            <img src='image/gm.gif'></img></div>";
            echo "</div>";
            //echo '</pre>';
            }
                        
            }
            echo '<a href=?p=1>首页</a>&nbsp;';
            echo '<a href=?p='.($currpage+1).'>下页</a>&nbsp;';
            echo '<a href=?p='.($currpage-1).'>上页</a>&nbsp;';
            echo "<a href=?p={$pagecount}>末页</a>&nbsp;&nbsp;";
            echo "[{$currpage}/{$pagecount}][共{$recordcount}记录,每页{$pagesize}条]";
            ?>
            <select onchange="location.href='?p='+this.value">
            <?php for($i=1;$i<=$pagecount;$i++){?>
            <option value="<?php echo $i;?>" <?php if($i==$currpage) echo 'selected';?>>第<?php echo $i;?>页</option>
            <?php }?>
            </select>


错误提示是很多行  
但基本上都是提示说


Notice: Uninitialized string offset: 6 in D:\Program Files\Apache\htdocs\pro\phone_zone.php on line 183

Notice: Uninitialized string offset: 7 in D:\Program Files\Apache\htdocs\pro\phone_zone.php on line 183

求教

------解决方案--------------------
while($row=mysql_fetch_array($result)){//mysql_fetch_array($result)返回的是一维数组
foreach ($row as $k=>$va){//所以$va是单值数据
//echo '<pre>';
//print_r($row);

echo "<div class='p_list'>";
echo "<div class='p_list_pic'><a><img src="."'image/'.$va[13]".">"."</img></a></div>";
echo "<div class='p_list_con'>
<p class='p_list_con_title'>$va[1]</p><br>
$va[4],$va[5]<br>
$va[6],$va[7]<br>//所以$va[6],$va[7]只能是单个字符,如果$val没有那么长,则就要报错了

我认为你的本意应该是

while($val=mysql_fetch_array($result)){
//foreach ($row as $k=>$va){

也就是说:$va[6],$va[7]是第6,7个字段的值
------解决方案--------------------
去掉foreach那层循环。
while($va = mysql_fetch_row($result) ){
// ...
}

至于你#2说的问题,我不会正则表达式所以帮不上忙