Essex
Essex

Reputation: 6128

Display query result with PHP and MySQL

I'm learning this new langage PHP in order to develop modules from this software : Dolibarr

It's the first time I'm using PHP and I don't overcome to display query result in my view.

I would like to know if I wrote something wrong in my script because I don't understand all up to now. I would like to display the number of users in my software. I have to query my llx_user table and display the result in my array.

This is the part of my code :

/*
 * View
 */

//Display number of users

$sql = "SELECT COUNT(u.rowid) as total";
$sql.= " FROM ".MAIN_DB_PREFIX."user as u";

$result = $db->query($sql);

print '<table class="noborder" width="100%">';
print '<tr class="liste_titre"><th colspan="2">'.$langs->trans("Statistics").'</th></tr>';
if (! empty($conf->user->enabled))
{
        $statUsers = '<tr class="oddeven">';
        $statUsers.= '<td><a href="index.php">'.$langs->trans("Number of Users").'</a></td><td align="right">'.round($result).'</td>';
        $statUsers.= "</tr>";

}

$total=0;
if ($entity == '0')
{
        print $statUsers;
        $total=round($result);
}
print '<tr class="liste_total"><td>'.$langs->trans("Total").'</td><td align="right">';
print $total;
print '</td></tr>';
print '</table>';


print '</div></div></div>';

llxFooter();

$db->close();

As I said, it's the first time I'm handling php file and I began to learn php 3 hours ago.

This is what I got :

enter image description here

If I comment like this :

$total=0;
//if ($entity == '0')
//{
        print $statUsers;
        $total=round($result);
//}

I'm getting this :

enter image description here

But I have 2 users in my table :

enter image description here

Thank you if you could help me

Upvotes: 2

Views: 3558

Answers (1)

Twinfriends
Twinfriends

Reputation: 1987

You're doing a good job for that you just started with PHP. Anyway, there's a little mistake in your code.

You actually query the database, but you don't fetch the result.

You have to do the following after your query:

$row = $result->fetch_row();
print $row[0]; // $row[0] will contain the value you're looking for

Also it seems that your $entity is not equal to 0. I don't see you initializing this variable anywhere, are you sure you have defined it? May you want to show us some mor of your code..

Upvotes: 5

Related Questions