PDA

View Full Version : Getting MySQL INSERT ID MySQL R7 plugin


Ld Est Ld
25/07/2013, 04:09 PM
Helllo, I have another problem I can't solve.

If I insert query then How I get the ID of the INSERTED query?

The Code:
if(EDIT_OBJ_ID[playerid]) {
SetDynamicObjectPos(EDIT_OBJ_ID[playerid], x, y, z);
SetDynamicObjectRot(EDIT_OBJ_ID[playerid], rx, ry, rz);
EDIT_OBJ_ID[playerid] = objectid;

furnitureVariables[EDIT_OBJ_ID[playerid]][fFurniturePos][0] = x;
furnitureVariables[EDIT_OBJ_ID[playerid]][fFurniturePos][1] = y;
furnitureVariables[EDIT_OBJ_ID[playerid]][fFurniturePos][2] = z;

furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureRot][0] = rx;
furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureRot][1] = ry;
furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureRot][2] = rz;

furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureHouseID] = getPlayerHouseID(playerid);

furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureINT] = GetPlayerInterior(playerid);
furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureVW] = GetPlayerVirtualWorld(playerid);

if(isnull(furnitureVariables[EDIT_OBJ_ID[playerid]][fFurniturePlanter])) {
furnitureVariables[EDIT_OBJ_ID[playerid]][fFurniturePlanter] = playerVariables[playerid][pNormalName];
format(szQueryInput, sizeof(szQueryInput), "INSERT INTO furniture (objectModelID, objectPosX, objectPosY, objectPosZ, objectHouseID, objectRotX, objectRotY, objectRotZ, objectPlanterName, objectVW, objectINT) VALUES ('%d', '%f', '%f', '%f', '%d', '%f', '%f', '%f', '%s', '%d', '%d')", furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureOID],
furnitureVariables[EDIT_OBJ_ID[playerid]][fFurniturePos][0], furnitureVariables[EDIT_OBJ_ID[playerid]][fFurniturePos][1], furnitureVariables[EDIT_OBJ_ID[playerid]][fFurniturePos][2], furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureHouseID],
furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureRot][0], furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureRot][1], furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureRot][2], furnitureVariables[EDIT_OBJ_ID[playerid]][fFurniturePlanter],furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureINT],furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureVW]);
mysql_function_query(connection, szQueryInput, true, "insertFurniture", "d", playerid);
#if defined DEBUG
printf("%s", szQueryInput);
#endif
EDIT_OBJ_ID[playerid] = 0;
pEDIT_OBJ[playerid] = 0;
} else if(!isnull(furnitureVariables[EDIT_OBJ_ID[playerid]][fFurniturePlanter])) {
saveFurniture(EDIT_OBJ_ID[playerid]);
EDIT_OBJ_ID[playerid] = 0;
pEDIT_OBJ[playerid] = 0;
}
}

The public function
public insertFurniture(playerid) {
furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureID] = mysql_insert_id();
return 1;
}

And saveFunriture stck:
public saveFurniture(furnitureID) {
format(szQueryInput, sizeof(szQueryInput), "UPDATE furniture SET objectModelID = '%d', objectPosX = '%f', objectPosY = '%f', objectPosZ = '%f', objectHouseID = '%d', objectRotX = '%f', objectRotY = '%f', objectRotZ = '%f', objectPlanterName = '%s', objectVW = '%d', objectINT = '%d'",
furnitureVariables[furnitureID][fFurnitureOID], furnitureVariables[furnitureID][fFurniturePos][0], furnitureVariables[furnitureID][fFurniturePos][1], furnitureVariables[furnitureID][fFurniturePos][2], furnitureVariables[furnitureID][fFurnitureHouseID], furnitureVariables[furnitureID][fFurnitureRot][0], furnitureVariables[furnitureID][fFurnitureRot][1], furnitureVariables[furnitureID][fFurnitureRot][2], furnitureVariables[furnitureID][fFurniturePlanter],
furnitureVariables[furnitureID][fFurnitureVW], furnitureVariables[furnitureID][fFurnitureINT]);

format(szQueryInput, sizeof(szQueryInput), "%s WHRE objectID = '%d'", szQueryInput, furnitureVariables[furnitureID][fFurnitureID]);
#if defined DEBUG
printf("%s", szQueryInput);
#endif
return 1;
}

Thanks.

Xabi
25/07/2013, 04:37 PM
Just change

mysql_function_query(connection, szQueryInput, true, "insertFurniture", "d", playerid);

to

mysql_function_query(connection, szQueryInput, false, "insertFurniture", "d", playerid);

Konstantinos
25/07/2013, 04:43 PM
Whenever you use INSERT INTO or UPDATE is not necessary to use cache parameter to true. If I remember correct, it ignores it and it set it to true (always) in the newest versions.

Anyways, what you're looking for is:

mysql_insert_id( )


Like you have in a callback. That should work.


furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureID] = mysql_insert_id();

Ld Est Ld
25/07/2013, 04:48 PM
Whenever you use INSERT INTO or UPDATE is not necessary to use cache parameter to true. If I remember correct, it ignores it and it set it to true (always) in the newest versions.

Anyways, what you're looking for is:

mysql_insert_id( )


Like you have in a callback. That should work.

Yes, but I give this value to the variable:

public insertFurniture(playerid) {
furnitureVariables[EDIT_OBJ_ID[playerid]][fFurnitureID] = mysql_insert_id();
return 1;
}
But if I use it, it gives me 0.

EDIT::: Thank You both..
My stupid mistake. I made EDIT_OBJ_ID[playerid] = 0 before INSERT. That's Why it gave me 0.

Thanks. REP+. :)