计数MySQL行数 [英] Count number of MySQL rows

查看:92
本文介绍了计数MySQL行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图计算MySQL数据库中的行数,并使用PHP报告该数字。我已经阅读了所有的文档,我认为这段代码应该可以工作,但不管我尝试什么,它都会失败。

 <?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 不会返回一个数字。有任何想法吗?我缺少一个括号或分号吗?

p>

 <?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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆