Syed Hashim
Syed Hashim

Reputation: 156

Print output in php using Sql queries

I am working on PHP where i want to fetch the count of a particular column 'uid' from users table. The output window is not displaying anything. following is the code. Can anybody, help me in rectifying this code.

    <?php
    $link = mysql_connect('localhost', 'username', 'password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("rth_db");
    $data = mysql_query("SELECT COUNT(uid) AS Total FROM users", $link);
    $number = mysql_fetch_array($data);
    echo $number;
    echo 'Connected successfully';
    mysql_close($link);
    ?>

Upvotes: 1

Views: 150

Answers (4)

M.I.T.
M.I.T.

Reputation: 1042

u can also use

var_dump($number);

Upvotes: 0

user1533868
user1533868

Reputation:

try using this :

<?php
    $link = mysql_connect('localhost', 'username', 'password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("rth_db");
    $data = mysql_query("SELECT COUNT(uid) AS Total FROM users", $link);
    $number = mysql_fetch_array($data);
    echo $number['total'];
    echo 'Connected successfully';
    mysql_close($link);
    ?>

i have just edited one line if u notice...hope this works :)

Upvotes: 1

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

change this

echo $number;

to

print_r($number);

OR

echo $number['Total'];

Because $number is array not string.

Upvotes: 1

Tikkes
Tikkes

Reputation: 4689

you should address your data differently

echo $number["Total"];

Also, mysql is deprecated, have a look at PDO. This also is better for security and so on. You can check the PHP.NET page for more info on this

Upvotes: 4

Related Questions