当前位置:Gxlcms > PHP教程 > 求教一个php调用数据库输出的有关问题

求教一个php调用数据库输出的有关问题

时间:2021-07-01 10:21:17 帮助过:3人阅读

求教一个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 '
';
    //print_r($row);
                            
    echo "";
    echo ""https://img.gxlcms.com//Uploads-s/https://img.gxlcms.com//Uploads-s/new/2019-09-21-201921/"";
    echo "
        

$va[1]


$va[4],$va[5]
$va[6],$va[7]
$va[8],$va[9]

$va[10]

"; echo "售价:¥$va[3]
"; echo ""; //echo '
'; } } echo '首页 '; echo '下页 '; echo '上页 '; echo "末页 "; echo "[{$currpage}/{$pagecount}][共{$recordcount}记录,每页{$pagesize}条]"; ?>

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


Notice: Uninitialized string offset: 6 in D:\Program Files\Apache\htdocs\pro\phone_zonehttps://img.gxlcms.com//Uploads-s/https://img.gxlcms.com//Uploads-s/new/2019-09-21-201921/php on line 183

Notice: Uninitialized string offset: 7 in D:\Program Files\Apache\htdocs\pro\phone_zonehttps://img.gxlcms.com//Uploads-s/https://img.gxlcms.com//Uploads-s/new/2019-09-21-201921/php on line 183

求教

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

echo "";
echo ""https://img.gxlcms.com//Uploads-s/https://img.gxlcms.com//Uploads-s/new/2019-09-21-201921/"";
echo "

$va[1]



$va[4],$va[5]

$va[6],$va[7]
//所以$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) ){
// https://img.gxlcms.com//Uploads-s/https://img.gxlcms.com//Uploads-s/new/2019-09-21-201921/https://img.gxlcms.com//Uploads-s/https://img.gxlcms.com//Uploads-s/new/2019-09-21-201921/https://img.gxlcms.com//Uploads-s/https://img.gxlcms.com//Uploads-s/new/2019-09-21-201921/
}

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

人气教程排行