0

I have an SQL query like this. When iam executing it though my php file it gives time out error. But when running the same query thoruhg php myadmin it gives results within seconds.

SELECT
    cake_apartments.id,
    cake_apartments.Headline,
    cake_apartments.Description,
    cake_apartments.photoset_id,
    cake_apartments.Rental_Price,
    cake_apartments.Bedrooms,
    cake_apartments.Bathrooms
FROM
    cake_apartments,cake_neighborhoods
WHERE
    (cake_apartments.Rented = 0)
    AND
    (cake_apartments.status = 'Active')
ORDER BY
    cake_neighborhoods.Name DESC 

I know that increasing the time out may solve the problem. but i don't want to spend more than 30 sec for this query.

6
  • something might be wrong in some loop. Commented Oct 1, 2009 at 9:03
  • I agree with Alec, if the Query gives results within seconds in PHPMyAdmin, then there's something else wrong in your script. Commented Oct 1, 2009 at 9:05
  • Your query seams ok. I would say that the main problem is in the connection to the database. Can you post that script here? phpMyAdmin is probably connecting to your MySQL database in a different way. Commented Oct 1, 2009 at 9:06
  • Did you test this with only the mysql connect code and sending the query in it? Nothing else, just connect and send the query. What happens if you use an unbuffered query (but still only the connect and send query code)? Commented Oct 1, 2009 at 9:45
  • I know it's probably not the source of your problem, but where's the join between cake_apartments and cake_neighborhoods or am I missing some shiny feature in MySQL (if so is it the REFERENCES keyword that InnoDB understands)? Commented Oct 1, 2009 at 11:30

2 Answers 2

1

the problem is you haven't specific the relationship between your two tables. it returns quickly in phpmyadmin because phpmyadmin adds a LIMIT clause that allows the mysql server to stop sending rows quickly, never getting near the timeout.

you think the query is just retrieving the rows where the apartments are not rented and are active, but what you're really getting is that numbers of rows * the number of neighborhoods in your database.

rewrite your query like this:

SELECT
    cake_apartments.id,
    cake_apartments.Headline,
    cake_apartments.Description,
    cake_apartments.photoset_id,
    cake_apartments.Rental_Price,
    cake_apartments.Bedrooms,
    cake_apartments.Bathrooms
FROM
    cake_apartments
JOIN
    cake_neighborhoods
  ON
    cake_neighborhoods.id = cake_apartments.neighborhood_id
WHERE
    (cake_apartments.Rented = 0)
    AND
    (cake_apartments.status = 'Active')
ORDER BY
    cake_neighborhoods.Name DESC

note that i only guessed at how the two tables are related in the ON clause, so if i got it wrong you'll have to adjust it.

Sign up to request clarification or add additional context in comments.

Comments

1

If you ONLY want rows where a match exists, your SQL needs to include an INNER JOIN. If cake_neighborhoods.cake_apartments_id is the name of your foreign key in the cake_neighborhoods table, I suggest rewriting the query as the following:

SELECT
    cake_apartments.id,
    cake_apartments.Headline,
    cake_apartments.Description,
    cake_apartments.photoset_id,
    cake_apartments.Rental_Price,
    cake_apartments.Bedrooms,
    cake_apartments.Bathrooms,
    cake_neighborhoods.Name
FROM
    cake_apartments,cake_neighborhoods
WHERE
    (cake_apartments.Rented = 0)
    AND
    (cake_apartments.status = 'Active')
    AND
    cake_neighborhoods.cake_apartments_id = cake_apartments.id
ORDER BY
    cake_neighborhoods.Name DESC

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.