Help me debug this... mysql_insert_id()
Posted: Thu Jun 29, 2006 1:36 pm
I'm trying to figure out why this code isn't pulling an insert id. The first query is inserted just fine. All subsequent queries are inserted with an insert_id of 0. It seems like mysql_insert_id() just quit working. It starts with a standard insert...
Here are the query builder and database functions...
Code: Select all
$sql_data_array = array('customers_id' => $customer_id,
'customers_name' => $order->customer['firstname'] . ' ' . $order->customer['lastname'],
// 30 lines of other fields here
'currency_value' => $order->info['currency_value']);
tep_db_perform(TABLE_ORDERS, $sql_data_array); // this calls the query builder
$insert_id = tep_db_insert_id(); // this is always returning 0Code: Select all
function tep_db_query($query, $link = 'db_link') {
global $$link;
/////////////////////////////////////////////////////////////////////
//echo '<div style="border-width: 1px; border-style: solid; border-color: #ff0000;">' . $query . '</div>';
//if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
// error_log('QUERY ' . $query . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
// }
$result = mysql_query($query, $$link) or tep_db_error($query, mysql_errno(), mysql_error());
//if (defined('STORE_DB_TRANSACTIONS') && (STORE_DB_TRANSACTIONS == 'true')) {
// $result_error = mysql_error();
// error_log('RESULT ' . $result . ' ' . $result_error . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);
//}
return $result;
}
function tep_db_input($string) {
return addslashes($string);
}
function tep_db_perform($table, $data, $action = 'insert', $parameters = '', $link = 'db_link') {
reset($data);
if ($action == 'insert') {
$query = 'insert into ' . $table . ' (';
while (list($columns, ) = each($data)) {
$query .= $columns . ', ';
}
$query = substr($query, 0, -2) . ') values (';
reset($data);
while (list(, $value) = each($data)) {
switch ((string)$value) {
case 'now()':
$query .= 'now(), ';
break;
case 'null':
$query .= 'null, ';
break;
default:
$query .= '\'' . tep_db_input($value) . '\', ';
break;
}
}
$query = substr($query, 0, -2) . ')';
} elseif ($action == 'update') {
$query = 'update ' . $table . ' set ';
while (list($columns, $value) = each($data)) {
switch ((string)$value) {
case 'now()':
$query .= $columns . ' = now(), ';
break;
case 'null':
$query .= $columns .= ' = null, ';
break;
default:
$query .= $columns . ' = \'' . tep_db_input($value) . '\', ';
break;
}
}
$query = substr($query, 0, -2) . ' where ' . $parameters;
}
return tep_db_query($query, $link);
}
function tep_db_insert_id() {
return mysql_insert_id();
}