-1

I am trying to script a PHP script to connect to my MySQL database to tell me how many people have registered to my forums by counting the records in the table.

enter image description here

The PHP script should display the total of records above "Users Registered"

I don't know if I am correct but I am using this PHP script to count the records:

<li>
                <?php
                    $result = mysql_query(SELECT COUNT(uid) FROM mybb_users);
                    $total = mysql_num_rows($result);
                ?>

                <span>Users registered</span>
</li>

The way the javascript allows the data to show on the webpage is when the HTML tag <strong data-number="0"></strong> is used.

I believe the HTML tag links to the "main.js" file. The JavaScript function is:

facts: function() {

    var $eleNumber;

    $('.facts li:not(.break)').each(function() {
        $eleNumber = $(this).find('strong');
        $eleNumber.animateNumbers($eleNumber.attr('data-number'));
    });

},

The only way the webpage show the values is by this script for example: Image of Website script

It would now show 100, 000 on "Players"

Like is it possible to script the "echo" or "print" in an HTML tag for example...

<strong data-number="The total number of records"></strong>

I do not know how this works, but like convert the PHP Script to work with the JAVASCRIPT function..

I am very sorry for a unclear question, thing is I can't quite explain the problem itself, I could be completely wrong and have tried researching

  • num_rows with count will always respond 0 or 1 – cske Aug 31 '15 at 15:58
  • RTM http://php.net/manual/en/function.mysql-query.php your code contains syntax errors. Don't forget to connect. – Funk Forty Niner Aug 31 '15 at 15:59
  • This is the script to connect to MySQL Database http://prntscr.com/8b0yp8 – MrKiller12 Aug 31 '15 at 16:11
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 31 '15 at 16:29

3 Answers3

0

Two issues with your PHP script:

  1. mysql_query is expecting a string (you will get a syntax error from that script). Try:
$result = mysql_query('SELECT COUNT(uid) FROM mybb_users');
  1. mysql_num_rows will return 1 because that query will only return one row. Try:
<li> <?php
$result = mysql_query('SELECT COUNT(uid) FROM mybb_users');
$total = mysql_result($result, 0, 0);
?>
<span>Users registered: <?php echo $total; ?></span>
</li>
samlev
  • 5,852
  • 1
  • 26
  • 38
  • Thank you, http://prntscr.com/8b0xhu I have done what you have said, but the value still doesn't show http://prntscr.com/8b0xud – MrKiller12 Aug 31 '15 at 16:08
0

A simple change or two will provide the count. First, you must place your query in quotes (as a string) for the function to use. Second, select everything if you want to use mysql_num_rows():

<?php
    $result = mysql_query('SELECT * FROM mybb_users') or die(mysql_error()); // don't COUNT()
    $total = mysql_num_rows($result);
?>

You're also not doing any error checking (your error logs might provide the answers), so you should add minimal error checking; placing error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); Also add error checking, such as or die(mysql_error()) to your queries.

You should never expose true error conditions to your users because doing so may give them just the clues they need to hack your database or server. You should provide for more robust error handling that only you or your fellow developers would see.

If you can, you should stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's really not hard.

Community
  • 1
  • 1
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • Thing is http://prntscr.com/8b1axy there the tags that make these numbers show http://prntscr.com/8b1b56 It's quite confusing, I have put your script in though http://prntscr.com/8b1bl4 Sorry, I am a real newb:/ – MrKiller12 Aug 31 '15 at 16:40
  • I have no idea what you're asking here. Are you sure you don't want to edit your original question and make things crystal clear @MrKiller12? Make sure to post *all* of your relevant code and markup here (not in printscreen images) so it is preserved for future SO visitors. – Jay Blanchard Aug 31 '15 at 16:44
0

I have fixed it by this code

<strong data-number="<?php include ('test.php')?>"></strong>