TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-16-2011, 08:41 PM   #1 (permalink)
The Wanderer
 
Join Date: Jan 2010
Posts: 13
Thanks: 0
pepelepew1962 is on a distinguished road
Default Problem with second query ...

Hello:

I am having a problem and I am not sure my method is correct. I know my sub-query isn't. What I need is to get the Max-Autonum, or last record for a specific record. Please see the example for a better understanding:



part_id_num part_manufac_code part_number
1 36 18547
2 36 57847
3 35 79811
4 36 57914
5 35 74992


If I am looking for part_manufac_code 36, then the last record (part_id_num) for that is 4. Here is the code that I have:
//
//
//
require("connect.php");
//
//
//
$query1 = "SELECT part_id_num, part_manufac_code, part_number FROM tblparts WHERE (part_manufac_code = '$frmfindpart')";
$result1 = mysql_query($query1) or die(mysql_error());
//
//
$query2 = "SELECT MAX(part_id_num) FROM '$result1'";
$result2 = mysql_query($query2) or die(mysql_error());
if(mysql_num_rows($result2) > 0)
{
while($row = mysql_fetch_assoc($result2))
{
$names[] = $row['name'];
}
}
else
{
echo 'query returned no results';
}
pepelepew1962 is offline  
Reply With Quote
Old 11-16-2011, 10:03 PM   #2 (permalink)
The Visitor
 
Join Date: Nov 2011
Posts: 2
Thanks: 0
RokD is on a distinguished road
Default

This should probs do the trick
PHP Code:
$query "SELECT * FROM tblparts ORDER BY part_manufac_code DESC, part_id_num DESC LIMIT 1"

$row mysql_fetch_arraymysql_query$query ));

echo 
"The result is {$row['part_number']}"
RokD is offline  
Reply With Quote
Old 11-17-2011, 03:03 PM   #3 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

You beat me to it RokD :P. But yeah, a combination or ORDER BY and LIMIT would do the trick.
tony is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie SQL Query problem? RallanToo Absolute Beginners 1 07-05-2011 12:59 PM
Problem executing MSSQL query in PHP trmbne2000 MySQL & Databases 2 06-30-2009 02:52 PM
Problem with mysql_real_escape_string() ETbyrne Advanced PHP Programming 0 12-17-2008 10:39 PM
Huge Session Problem Killswitch General 1 11-17-2008 02:36 AM
Problem with Query + While $a = mysql_fetch_array($b) Aaron Absolute Beginners 0 07-03-2008 07:16 PM


All times are GMT. The time now is 11:56 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design