开发者

MySql variables and php

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 @AgentCount;

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 D开发者_Python百科ESC LIMIT 50;

In php, here is how I set up the $query

      $query = "
      SELECT count(*) FROM agents INTO @AgentCount;

      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";

That exact query will work fine if I put it directly into MySql via a command line session. Do I need to issue two separate php calls to mysql_query() and store the first result?

I am getting the below error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT user_agent_parsed, user_agent_original, COUNT( user_agent_parsed ) AS thecount' at line 3

The reason for not using a sub select and instead choosing a MySql variable is to avoid a count() happening on every percentage calculation. Though it may be possible the engine is optimizing for that. So far, I have not been able to confirm that. I have also heard sub selects are almost always non optimal.

EXPLAIN tells me this:

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   PRIMARY agents  index   NULL    user_agent_parsed   28  NULL    82900   Using temporary; Using filesort
2   SUBQUERY    NULL    NULL    NULL    NULL    NULL    NULL    NULL    Select tables optimized away


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.
  }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜