您的位置:程序门 -> web 开发 -> php



新手问题!同一页面两次查询数据库问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


新手问题!同一页面两次查询数据库问题[已结贴,结贴人:shenhong420]
发表于:2007-04-09 16:34:21 楼主
小弟是新手,刚刚开始学习php,在调试程序中需要对数据库进行两次查询,建立了三个页面conn.php、getsettingphp、index.php,需要在index.php中调用conn.php和getsetting.php,代码如下:

conn.php
<?php
//这里设置连接数据库
$myconn=mysql_connect( 'localhost ', 'root ', '123456 '); //连接数据库服务器
mysql_select_db( "tdababase "); //连接指定的数据库
mysql_query( "set   names   'gbk ' "); //设置编码类型
?>

getsetting.php
<?php
//这里获得数据库中的系统信息
$str_sql= "select   *   from   setting ";
$result=mysql_query($str_sql,$myconn);
while   ($row=mysql_fetch_object($result)){
global   $sysnane,$syscompany,$sysinfo,$sysediter,$sysemail;
$sysnane=$row-> l_sysname;
$syscompany=$row-> l_company;
$sysinfo=$row-> l_sysinfo;
$sysediter=$row-> l_sysediter;
$sysemail=$row-> l_sysemail;
}
mysql_free_result($result);
//这里获取数据库中的样式信息
$str_sql= "select   *   from   style   where   l_stylemain=1 ";
$result=mysql_query($str_sql,$myconn);
while   ($row=mysql_fetch_object($result)){
global   $stylenane,$stylepatch,$stylefile;
$stylenane=$row-> l_stylename;
$stylepatch=$row-> l_stylepatch;
$stylefile=$row-> l_stylefile;
}
mysql_free_result($result);
?>

index.php
<?php
require_once   "conn.php ";
require_once   "getsetting.php ";

echo   $sysnane;
echo   $syscompany;
echo   $sysinfo;
echo   $sysediter;
echo   $sysemail;

echo   $stylenane;
echo   $stylepatch;
echo   $stylefile;
?>

但是运行后提示:
warning:   mysql_fetch_object():   supplied   argument   is   not   a   valid   mysql   result   resource   in   /3wdoc/getsetting.php   on   line   17

warning:   mysql_free_result():   supplied   argument   is   not   a   valid   mysql   result   resource   in   /3wdoc/getsetting.php   on   line   23

代码上是不是那里还需要做什么?请老鸟指点迷津!!!!
发表于:2007-04-09 16:43:181楼 得分:20
select   *   from   style   where   l_stylemain=1   ????
1、确认   style   表中存在   l_stylemain   字段
2、确认   l_stylemain   字段是数字型的
3、建议写做
$str_sql= "select   *   from   style   where   l_stylemain=1 ";
$result=mysql_query($str_sql,$myconn)   or   die(mysql_error());

发表于:2007-04-09 17:05:132楼 得分:0
根据xuzuning(唠叨)的指点,使用了or   die(mysql_error())后出现如下提示:
unknown   column   'l_stylepatch '   in   'field   list '
最开始的时候设置的sql语句为:
$str_sql= "select   l_stylename,l_stylepatch,l_stylefile   from   style   where   l_stylemain=1 ";
提示上面的语句

但是在数据库中有l_stylepatch字段,后来我把sql语句改为$str_sql= "select   *   from   style   where   l_stylemain=1 ";
则查询成功

我在l_stylepatch中存放的是文件路径,内容为skin/red/   不知道是不是由于存放内容的问题!请高人再指点1下,谢谢
发表于:2007-04-09 17:21:103楼 得分:0
支持一下   !
发表于:2007-04-09 17:25:574楼 得分:0
l_stylepatch的设置问题?
发表于:2007-04-09 17:27:315楼 得分:0
贴出表结构
发表于:2007-04-09 17:42:196楼 得分:0
仔细看看l_stylepatch列名称是不是拼错了
发表于:2007-04-09 17:43:397楼 得分:0
l_stylepath??
发表于:2007-04-09 17:47:008楼 得分:0
还是ls的强啊,呵呵~~
发表于:2007-04-09 17:57:369楼 得分:0
我看了一下,数据库结构应该没有问题,数据库sql代码如下
create   table   `ls_style`   (
    `id`   int(11)   not   null   auto_increment   comment   '自动编号 ',
    `l_stylename`   varchar(50)   not   null   comment   '样式名称 ',
    `   l_stylepatch`   varchar(255)   not   null   comment   '文件位置 ',
    `l_stylefile`   varchar(255)   not   null   comment   '文件名称 ',
    `l_stylemain`   tinyint(1)   not   null   comment   '是否是主用样式 ',
    primary   key     (`id`)
)   engine=myisam     default   charset=gbk   auto_increment=2   ;
发表于:2007-04-09 18:12:3610楼 得分:0
不可能是关键字啊,等待高手指点。
发表于:2007-04-10 00:50:0011楼 得分:0
解决方法,在你的$str_sql= "select   l_stylename,l_stylepatch,l_stylefile   from   style   where   l_stylemain=1 ";下面加一行 echo   $str_sql;把输出的东西复制下来直接在mysql下运行。根据提示一更改相关东西。
解决方法之二,把你的$str_sql= "select   l_stylename,l_stylepatch,l_stylefile   from   style   where   l_stylemain=1 ";改成$str_sql= "select   `l_stylename`,`l_stylepatch`,`l_stylefile`   from   style   where   l_stylemain=1 ";注意字段大小写
然后就是,给分。
发表于:2007-04-10 10:37:0112楼 得分:0
把require_once   "conn.php ";这句放到getsetting.php文件里试试
发表于:2007-04-10 12:16:1913楼 得分:0
你再改回去,看看还错吧~   怎么看怎么像是笔误
发表于:2007-04-12 11:02:1114楼 得分:0
问题已得到解决   散分


快速检索

最新资讯
热门点击