I am getting an error with this in php. What is the correct way to format this string to pass to mysql_query() in php?
SELECT count(*) FROM agents INTO @AgentCo
If you are using mysql_query
, then yes, you need to send each query separately. From the description at the top of mysql_query
's entry in the PHP manual: "mysql_query() sends a unique query (multiple queries are not supported) to the currently active database..."
As for subqueries, you'd be surprised; the query optimizer generally handles them very well.
You can only have one query at a time in PHP.
$query1 = "SELECT count(*) FROM agents INTO @AgentCount"
$query2="
SELECT user_agent_parsed, user_agent_original, COUNT( user_agent_parsed ) AS thecount,
COUNT( * ) / ( @AgentCount) AS percentage
FROM agents
GROUP BY user_agent_parsed
ORDER BY thecount DESC LIMIT 50";
UPDATE
I have a DAL that contains all my queries. A typical function in my DAL looks like this:
// These functions are reusable
public function getAllRows($table)
{
$sql =" SELECT * FROM $table";
$this->query($sql);
return $this->query_result;
}
Then in my BLL (Business Layer) I have the following:
public function getUserAgents()
{
$result = parent::getAllRows();
$row = mysql_fetch_array($result);
return $row[0]; // Retrieves the first row
// Then you take this value and to a second request. Then return the answer / rows.
}