me again!! gettin the last row in a table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
gasoga
Forum Commoner
Posts: 36
Joined: Thu Apr 17, 2003 4:15 am
Location: Ireland

me again!! gettin the last row in a table

Post by gasoga »

hi!

I have a piece of code that works properly if i use it in the query analyseer but when i use it in my code it doesnt work!

I want it to take the last call id just entered and unsert it into another table!
It will work for the very first row but then every row inserte dinto that table after has the exact same value!!!

Code: Select all

<?php
$Q03 = "INSERT INTO $dblog VALUES('$emp_username','$cust_username','$company_name','$date','$time','$call_type','$description','$priority','$res_call_imm','$solution','$assigned_to','$resolved','$solution2','$call_closed_date')";
$R03 =  odbc_exec($db,$Q03) or die("Bad Q03:".mysql_error()); 
 
$Q93 = "SELECT TOP 1 Call_ID from calls_logged order by Call_ID desc; ";
$R93 = odbc_exec($db,$Q93) or die("Bad Q93:".mysql_error());
$result = odbc_fetch_into($R93,$A93);

$Q73 = "INSERT INTO 
$dbcall_log
VALUES('$result','$emp_username','$cust_username','$date')";
$R73 =  odbc_exec($db,$Q73) or die("Bad Q03:".mysql_error());
odbc_free_result($R73);


?>

Code: Select all

Table one

Call_ID
1
2
3
4

Table two
1
1
1
1
Any ideas would be great thanx!!
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

try

Code: Select all

$Q93 = "SELECT MAX(Call_ID) from calls_logged order by Call_ID";
or if Call_ID is an auto increment column you can get the Call_ID value by using

mysql_insert_id directly after the original insert statement.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Which database are you using?

Mac
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

good point mac, sorry I missed that!

why are you using odbc calls and mysql_error() ????
gasoga
Forum Commoner
Posts: 36
Joined: Thu Apr 17, 2003 4:15 am
Location: Ireland

Post by gasoga »

sorry forgot to say!! its an mssql database!!

Im new to using odbc and odbc_error doesnt not work for me!
However the mysql error message does???!!!!!!
Strange!
Post Reply