请教下php读数据记录集问题

mjt2000mjt 2010-08-13 04:43:12
SELECT e.id AS id, t.name AS name, Count(*) AS num from Ecdsc as e left join E_Type as t on e.type = t.Id where group by e.sub_type order by num desc limit 4

这个是我的sql语句在 数据库中使用查询出来结果没问题,然后我用php连数据库就出错,读不到正确的记录,请问可以这样写吗?
...全文
88 点赞 收藏 10
写回复
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mjt2000mjt 2010-08-13
上面 where 是手误,参考后来发的语句

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource
这样就出错了,我找了个普通的代码测试了下
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource

以下是代码,


// 连接到数据库
$conn=mysql_connect($mysql_server_name, $mysql_username,
$mysql_password);

// 从表中提取信息的sql语句
$strsql="select e.Id as `id`, t.name as `name`, (select count(*) from Ecdsc as e where e.type = t.Id and e.type=2 ) as `num` from E_Type as t order by num";
// 执行sql查询
$result=mysql_db_query($mysql_database, $strsql, $conn);
// 获取查询结果
$row=mysql_fetch_row($result);

echo '<font face="verdana">';
echo '<table border="1" cellpadding="1" cellspacing="2">';

// 显示字段名称
echo "\n<tr>\n";
for ($i=0; $i<mysql_num_fields($result); $i++)
{
echo '<td bgcolor="#000F00"><b>'.
mysql_field_name($result, $i);
echo "</b></td>\n";
}
echo "</tr>\n";
// 定位到第一条记录
mysql_data_seek($result, 0);
// 循环取出记录
while ($row=mysql_fetch_row($result))
{
echo "<tr>\n";
for ($i=0; $i<mysql_num_fields($result); $i++ )
{
echo '<td bgcolor="#00FF00">';
echo "$row[$i]";
echo '</td>';
}
echo "</tr>\n";
}

echo "</table>\n";
echo "</font>";

mysql_free_result($result);

mysql_close();



//$sql = sprintf("SELECT e.evt_sub_type sub, e.evt_sub_type AS id, t.subtypename AS name, Count(*) AS num from Event as e left join Evt_Sub_Type as t on e.evt_sub_type = t.Id where e.happentime >= from_unixtime(%d) and e.happentime < from_unixtime(%d) group by e.evt_sub_type order by num desc limit 4", $startTimestamp, $endTimestamp);

//print $sql;

//$sql="SELECT t.Id, t.typeid, t.subtypename AS name, Count(*) AS num from Event as e left join Evt_Sub_Type as t on e.evt_sub_type = t.Id where t.typeid=2 group by t.Id order by num desc";
// $sql="SELECT t.Id, t.typeid, t.subtypename AS name,count(t.Id) as num from Event as e left join Evt_Sub_Type as t on t.id = e.evt_sub_type where t.typeid=2 group by t.id order by num desc limit 10 ";
//$sql = sprintf("SELECT t.subtypename AS name, Count(*) AS num FROM Event AS e Left Join Evt_Sub_Type AS t ON e.evt_sub_type = t.Id where e.evt_type=2 GROUP BY t.SubTypeName order by num desc limit 10");


//$sql="SELECT t.Id, t.typeid, t.subtypename AS name,e.evt_sub_type,count(t.Id) as num from Event as e left join Evt_Sub_Type as t on t.id = e.evt_sub_type where t.typeid=2 group by t.id ";

//$sql="SELECT t.subtypename AS name, count(e.e_id) as num FROM `Event` AS e Left Join `Evt_Sub_Type` AS t ON e.evt_sub_type = t.Id where e.evt_type=2 GROUP BY e.evt_sub_type order by num desc limit 10";
//print $sql."<br>";
//$result=$objCache->cache($sqlA, 'getall', 't');
// $sql = "SELECT t.subtypename AS name FROM `Event` AS e Left Join `Evt_Sub_Type` AS t ON t.Id = e.evt_sub_type where e.evt_type=2";

/*if($this->query($Query_String))
{


}*/

//$sql=" select t.Id as `id`, t.subtypename as `name`, (select count(*) from Event as e where e.evt_sub_type = t.Id and e.evt_type=2 and e.evt_status != 3) as `num` from Evt_Sub_Type as t order by num ";
//$sql = "SELECT t.subtypename AS name FROM `Event` AS e Left Join `Evt_Sub_Type` AS t ON t.Id = e.evt_sub_type where e.evt_type=2";


//$sql = sprintf("SELECT t.subtypename AS name, Count(e.e_id) AS num FROM Event AS e Left Join Evt_Sub_Type AS t ON e.evt_sub_type = t.Id where e.evt_type=2 GROUP BY t.SubTypeName order by num desc limit 4");

回复
superfans98 2010-08-13
where 后面不需要加条件吗?
回复
Dleno 2010-08-13
SELECT e.id AS id, t.name AS name, Count(*) AS num from Ecdsc as e left join E_Type as t on e.type = t.Id where group by e.sub_type order by num desc limit 4

你php里是不是写错了????
回复
床上等您 2010-08-13
用php echo sql语句出来一看就知了。
回复
mjt2000mjt 2010-08-13
SELECT e.id AS id, t.name AS name, Count(*) AS num from Ecdsc as e left join E_Type as t on e.type = t.Id where e.type=2 group by e.sub_type order by num desc limit 4

select e.Id as `id`, t.name as `name`, (select count(*) from Ecdsc as e where e.type = t.Id and e.type=2 ) as `num` from E_Type as t order by num

2种写法,数据库都没问题,但是在php中读取数据得不到正确的记录
回复
mjt2000mjt 2010-08-13
在数据库里面查询是没有任何问题的,就是不知道php查询为什么得不到正确记录
我就是要取排名前4条数据
回复
mjt2000mjt 2010-08-13
数据库连接肯定没问题的
$this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
while ($row = mysql_fetch_assoc($this->Query_ID))
这个是数据读取操作时的代码, mysql_fetch_assoc这里就只能读到一条记录。
回复
phf0313 2010-08-13
貌似没错!
看看别的地方有没有错。
回复
CunningBoy 2010-08-13
连接数据库的代码贴出来看看。
回复
发动态
发帖子
基础编程
创建于2007-09-28

2.1w+

社区成员

从PHP安装配置,PHP入门,PHP基础到PHP应用
申请成为版主
社区公告
暂无公告