Page 1 of 1

MySQL queries - do I need to build a PHP function for this?

Posted: Wed Feb 02, 2005 6:37 am
by mjseaden
Hi,

I have a table, and I wish to return (for e.g.) row 1 and row 30 from the table, regardless of the index associated with each. In other words, for a given query, I want to return row 1 and 30 into a couple of variables.

I could do this using PHP and using a while() loop and index counter to retrieve rows 1 and 30 for a given query, however this seems to be a bit of an inefficient way of doing it.

Is it possible to do this using a straight query, for a example using LIMIT (a MySQL function I am finding it very difficult to find any online guide to!?)

Many thanks in advance

Mark

Posted: Wed Feb 02, 2005 6:56 am
by MrKnight

Code: Select all

SELECT * FROM table LIMIT 5,10;  # Retrieve rows 6-15
for further info, look at http://dev.mysql.com/doc/mysql/en/select.html

Posted: Wed Feb 02, 2005 8:42 am
by mjseaden
Hi there,

Thanks for your help. Can you tell me what is wrong with the syntax of this query (it's giving me a syntax error towards the end of the query statement).

Code: Select all

SELECT * FROM Properties WHERE (ConsultancyID=''1'') ORDER BY PropertyTitle LIMIT 0,1
Many thanks

Mark

Posted: Wed Feb 02, 2005 8:54 am
by MrKnight
correct it that

Code: Select all

SELECT * FROM Properties WHERE ConsultancyID='1' ORDER BY PropertyTitle LIMIT 0,1

Strange 'Invalid MySQL resource' warning

Posted: Wed Feb 02, 2005 9:46 am
by mjseaden

Code: Select all

$result = mysql_query($query);
            
            if(!$result)
            {
                do_html_message('Error: Could not perform MySQL query - '.mysql_error());
                do_html_footer();
                exit();
            }

            while( $row = mysql_fetch_array( $result ) )
            ...
This produces an 'Invalid MySQL resource' warning for $result. The query however seems to complete, so I'm a little confused. The error is cause by the while($row = mysql_fetch_array($result)) line, however the query itself does not generate a mysql_error().

Any idea what could be causing this? $result is the correct variable returned by mysql_query.

Cheers

Mark

Posted: Wed Feb 02, 2005 11:00 am
by MrKnight
probaby you send wrong query to mysql,
paste more code here, especially the line that you send the query database... i meant $query = ...

Posted: Wed Feb 02, 2005 11:17 am
by pickle
Echo the query right before you call mysql_query(). I'm betting it's not quite what you thought it was.