计数MySQL行数 [英] Count number of MySQL rows
问题描述
<?php
session_start();
if(isset($ _ SESSION ['username'])){
$ username = $ _SESSION ['username'];
$ q =SELECT * FROM messages WHERE recepiants ='$ username'AND readcheck ='T';
$ count = mysql_num_rows($ q);
?>
< div class =user_info>以<?php echo $ username; ?> | < a href =admin.php>添加帖子< / a> | < a href =experiment.php>查看帖子< / a> | < a href =inbox.php>消息中心<?php echo $ count; ?> < / A> | < a href =logout.php>注销< / a>< / div>
<?php} else {?>
脚本成功报告 $ username
但 $ count
不会返回一个数字。有任何想法吗?我缺少一个括号或分号吗?
<?php
session_start();
if(isset($ _ SESSION ['username'])){
$ username = $ _SESSION ['username'];
$ q =SELECT * FROM messages WHERE recepiants ='$ username'AND readcheck ='T';
$ result = mysql_query($ q);
$ count = mysql_num_rows($ result);
?>
< div class =user_info>以<?php echo $ username; ?> | < a href =admin.php>添加帖子< / a> | < a href =experiment.php>查看帖子< / a> | < a href =inbox.php>消息中心<?php echo $ count; ?> < / A> | < a href =logout.php>注销< / a>< / div>
<?php} else {?>
可能更好的方法是运行 COUNT
在查询中,如果这就是你所需要的:
<?php
session_start();
if(isset($ _ SESSION ['username'])){
$ username = $ _SESSION ['username'];
$ q =SELECT COUNT(*)AS FROM FROM WHERE recepiants ='$ username'AND readcheck ='T';
$ result = mysql_query($ q);
$ result = mysql_fetch_assoc($ result)
$ count = $ result ['Count'];
?>
< div class =user_info>以<?php echo $ username; ?> | < a href =admin.php>添加帖子< / a> | < a href =experiment.php>查看帖子< / a> | < a href =inbox.php>消息中心<?php echo $ count; ?> < / A> | < a href =logout.php>注销< / a>< / div>
<?php} else {?>
另外,正如其他人指出的那样,您需要一个活动的数据库连接。
http://www.php .net / manual / en / function.mysql-fetch-assoc.php
Im trying to count the number of rows in a MySQL database and report that number using PHP. I've read all the documentation and I think this piece of code should be working but no matter what I try, it fails.
<?php
session_start();
if ( isset($_SESSION['username']) ) {
$username = $_SESSION['username'];
$q = "SELECT * FROM messages WHERE recepiants='$username' AND readcheck='T'";
$count = mysql_num_rows($q);
?>
<div class="user_info">Logged in as <?php echo $username; ?> | <a href="admin.php">Add a post</a> | <a href="experiment.php">View Posts</a> | <a href="inbox.php">Message Center <?php echo $count; ?> </a> | <a href="logout.php">Log Out</a></div>
<?php } else {?>
The script successfully reports $username
but $count
doesn't return a number. Any ideas? Am I missing a bracket or semi-colon somewhere?
The thing you're missing is running the actual query:
<?php
session_start();
if ( isset($_SESSION['username']) ) {
$username = $_SESSION['username'];
$q = "SELECT * FROM messages WHERE recepiants='$username' AND readcheck='T'";
$result = mysql_query($q);
$count = mysql_num_rows($result);
?>
<div class="user_info">Logged in as <?php echo $username; ?> | <a href="admin.php">Add a post</a> | <a href="experiment.php">View Posts</a> | <a href="inbox.php">Message Center <?php echo $count; ?> </a> | <a href="logout.php">Log Out</a></div>
<?php } else {?>
Probably better would be to run a COUNT
in the query, if that's all you need:
<?php
session_start();
if ( isset($_SESSION['username']) ) {
$username = $_SESSION['username'];
$q = "SELECT COUNT(*) AS Count FROM messages WHERE recepiants='$username' AND readcheck='T'";
$result = mysql_query($q);
$result = mysql_fetch_assoc($result)
$count = $result['Count'];
?>
<div class="user_info">Logged in as <?php echo $username; ?> | <a href="admin.php">Add a post</a> | <a href="experiment.php">View Posts</a> | <a href="inbox.php">Message Center <?php echo $count; ?> </a> | <a href="logout.php">Log Out</a></div>
<?php } else {?>
And, as someone else noted, you need an active database connection.
http://www.php.net/manual/en/function.mysql-fetch-assoc.php
这篇关于计数MySQL行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!