Calc element with a SQL Statement

Status
Not open for further replies.

jmdc

Member
Hello,

I am using a calc element with the following php code (including a SQL statement), and the result should be zero (0), and it always return 1:

$mysql = new mysqli('localhost', 'database', 'databasepassword', 'etc') or die('your\'re dead');
$db = JFactory::getDbo();
$query = "SELECT COUNT(Userid) FROM fichasaude WHERE (Alunoaescolher='Teste2' and userid={$my->id})";
$result = $mysql->query($query) or die($mysql->error);
$num = $result->num_rows;
echo $num;

What?s worng with it?

Thank you in advance for your help.
 
You don't need to set up your own MySQL session, the JFactory::getDbo() does that for you, returning J!'s default connection to the site database.

Also, in calcs you don't echo the result, you return it.

And it's good to get in the habit of using the query builder.

And best not to use common variable names like $db or $query, as those may clash with variables in J! or Fabrik.

Code:
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery->select("COUNT(Userid) as usercount")->from("fichasaude")->where("Alunoaescolher='Teste2' AND userid= " . $myDb->quote({$my->id}));
$myDb->setQuery($myQuery);
return $myDb->loadResult();

-- hugh
 
Thank you for your answer.

And how if i would like to change
where("Alunoaescolher='Teste2' by the data of that row?s ?

Thank you.

I have found the answer... where("Alunoaescolher='{inscriptions___Alunoainscrever_raw}'

Isn?t it?
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top