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 6036347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:54:39+00:00 2026-05-23T05:54:39+00:00

I have a query that is working fine when I run in through phpMyAdmin

  • 0

I have a query that is working fine when I run in through phpMyAdmin directly as SQL, but when I try and do this in PHP it doesn’t work. I suspect the problem is because I’m trying to return a rownum value from a generated table and the SET command on the first line doesn’t work. Here is an example query that is working as expected as SQL:

SET @rownum =0;
SELECT * FROM (
    SELECT i.*, @rownum := @rownum +1 AS row,  (
        SELECT COUNT( * )FROM _votes WHERE i.id = image_id) AS total_votes, 
        (SELECT COUNT( * ) FROM _votes WHERE i.id = image_id AND voter_id = 67) AS voted 
        FROM _images AS i WHERE i.approved = 1 ORDER BY total_votes DESC ) 
    AS T WHERE entrant_id IN (68,69,70,71) 

(please forgive my formatting if it is incorrect)

Two things:

  • Is this query too long and inefficient?
  • How do I initialize the @rownum value to zero if this is coming from a php prepared statement?

I’ve tried adding JOIN (SELECT @rownum :=0) r to the end of the query but that doesn’t work.
Also I’ve tried this joining method unsuccessfully at various points within the query.

php

$query = "SELECT * FROM ( SELECT i.*, @rownum := ...
  • 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-23T05:54:40+00:00Added an answer on May 23, 2026 at 5:54 am

    SET queries are distinct queries, and you can only send one query at a time through PHP. So, run the SET query, then run the second query. The MySQL server will remember your setting if you use the same connection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Linq to SQL query that was working just fine with SQL
I have one Servlet that does insertion into my database. This is working fine.
I have below a working query that needs to be simplified. The reason is
I have inherited a somewhat messy query that I am working on refactoring to
I've been working on optimizing a query and have ran into a situation that's
I am using in C# MYsql .I have query that works if I run
I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
I have a query that looks like this: public IList<Post> FetchLatestOrders(int pageIndex, int recordCount)
I have a query that I'm executing from a .NET application to a SQL
I have authlogic working fine but now have the need to let administrator users

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.