Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 402741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:09:10+00:00 2026-05-12T17:09:10+00:00

I am getting an error with this in php. What is the correct way

  • 0

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 DESC 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:09:11+00:00Added an answer on May 12, 2026 at 5:09 pm

    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.
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.