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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

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

Post 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
MrKnight
Forum Newbie
Posts: 22
Joined: Fri Jan 28, 2005 8:54 am
Location: Another Planet

Post 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
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post 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
MrKnight
Forum Newbie
Posts: 22
Joined: Fri Jan 28, 2005 8:54 am
Location: Another Planet

Post by MrKnight »

correct it that

Code: Select all

SELECT * FROM Properties WHERE ConsultancyID='1' ORDER BY PropertyTitle LIMIT 0,1
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Strange 'Invalid MySQL resource' warning

Post 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
MrKnight
Forum Newbie
Posts: 22
Joined: Fri Jan 28, 2005 8:54 am
Location: Another Planet

Post 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 = ...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Echo the query right before you call mysql_query(). I'm betting it's not quite what you thought it was.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply