PDA

View Full Version : MySQL free ID


HidroDF
17/12/2016, 10:05 PM
Hello, I'm trying to combine two tables in MySQL.

Code:


new query[400];
mysql_format(handle, query, sizeof(query), "INSERT INTO Phones SET Number = '%i'", phone);
mysql_tquery(handle, query, "", ""); // Tried InsertPhone here but same result
InsertPhone(number, cache_insert_id());

public InsertPhone(number, lastid)
{
new query[400];
mysql_format(handle, query, sizeof(query), "INSERT INTO ServerObjects (number, phoneid) VALUES ('%i', '%d')", number, lastid);
mysql_query(handle, query);
}


But the last id is 0 everytime. In the table Phones I have an ID row with AUTO_INCREMENT.
In Server I can't make the 'phoneid' as AUTO_INCREMENT because I don't insert only phones...

Any help?

SickAttack
17/12/2016, 10:08 PM
cache_insert_id() must be used in the callback specified with mysql_tquery, but you didn't specify one. You're using the callback/function wrong. Let mysql_tquery call it.

HidroDF
17/12/2016, 10:09 PM
I've tried

mysql_tquery(handle, query, "InsertPhone", "id", number, cache_insert_id());

And don't work.

SickAttack
17/12/2016, 10:10 PM
I've tried

mysql_tquery(handle, query, "InsertPhone", "id", number, cache_insert_id());

And don't work.

Put it directly:

mysql_format(handle, query, sizeof(query), "INSERT INTO ServerObjects (number, phoneid) VALUES ('%i', '%d')", number, cache_insert_id());

HidroDF
17/12/2016, 10:15 PM
Worked, thanks!