What might be the problem? [Question] - T0pAz - 03.02.2012
The
overall variable always returning 0. What might be the problem?
pawn Код:
mysql_query("SELECT LAST_INSERT_ID() FROM `vehicles`");
mysql_store_result();
new overall = mysql_num_rows();
printf("Overall: %d", overall);
mysql_free_result();
Re: What might be the problem? [Question] -
coole210 - 03.02.2012
I tested this code out, it always turns up as 0, you probably have to insert something first and use mysql_insert_id() in pawn
Re: What might be the problem? [Question] - T0pAz - 03.02.2012
Quote:
Originally Posted by coole210
I tested this code out, it always turns up as 0, you probably have to insert something first and use mysql_insert_id() in pawn
|
Can you please explain me what to do? An example will be appreciated.
Re: What might be the problem? [Question] -
coole210 - 03.02.2012
pawn Код:
mysql_query("INSERT INTO SOMETHING (SOMETHING) VALUE (SOMETHING)");
mysql_insert_id();
Re: What might be the problem? [Question] -
Johnson_boy - 03.02.2012
To do it MySQL server wise, you have to run the on the same query.
Код:
INSERT INTO `vehicles` (`something`) VALUES ('something');
SELECT LAST_INSERT_ID() AS `insertedID`;
That will work when querying them at the same time.
Re: What might be the problem? [Question] - T0pAz - 03.02.2012
Quote:
Originally Posted by coole210
pawn Код:
mysql_query("INSERT INTO SOMETHING (SOMETHING) VALUE (SOMETHING)"); mysql_insert_id();
|
Thanks mate it worked! You deserve 3 reputation.
Re: What might be the problem? [Question] -
AndreT - 03.02.2012
No he doesn't! That's not a valid solution to the situation as it will just insert a new row, won't it?
What
SELECT LAST_INSERT_ID() FROM vehicles gives you is a single row with one value: the last increment ID generated. So what you should do instead is:
pawn Код:
mysql_query("SELECT LAST_INSERT_ID() FROM `vehicles`");
mysql_store_result();
new overall = mysql_fetch_int();
printf("Overall: %d", overall);
mysql_free_result();
Re: What might be the problem? [Question] - T0pAz - 03.02.2012
Quote:
Originally Posted by AndreT
No he doesn't! That's not a valid solution to the situation as it will just insert a new row, won't it?
What SELECT LAST_INSERT_ID() FROM vehicles gives you is a single row with one value: the last increment ID generated. So what you should do instead is:
pawn Код:
mysql_query("SELECT LAST_INSERT_ID() FROM `vehicles`"); mysql_store_result(); new overall = mysql_fetch_int(); printf("Overall: %d", overall); mysql_free_result();
|
Well that's all I want and it doesn't insert a new row. I am using it for loading vehicles with the help of id for looping.
Re: What might be the problem? [Question] -
coole210 - 03.02.2012
I was doing some trial & error in my phpmyadmin and discovered that LAST_INSERT_ID needs parameters to work
Say vehicles has 2 fields: ID(int)[AI] and name(varchar)
if you do:
pawn Код:
SELECT LAST_INSERT_ID() FROM vehicles
You will receive multiple rows stating "0"
So, you put
id inside
pawn Код:
SELECT MYSQL_INSERT_ID(id) FROM vehicles
Hmm, still a problem. It's still giving multiple rows, so order it by ID
pawn Код:
SELECT MYSQL_INSERT_ID(id) FROM vehicles ORDER BY id DESC LIMIT 1
That will retrieve the last inserted ID
Re: What might be the problem? [Question] - T0pAz - 03.02.2012
Quote:
Originally Posted by coole210
I was doing some trial & error in my phpmyadmin and discovered that LAST_INSERT_ID needs parameters to work
Say vehicles has 2 fields: ID(int)[AI] and name(varchar)
if you do:
pawn Код:
SELECT LAST_INSERT_ID() FROM vehicles
You will receive multiple rows stating "0"
So, you put id inside
pawn Код:
SELECT MYSQL_INSERT_ID(id) FROM vehicles
Hmm, still a problem. It's still giving multiple rows, so order it by ID
pawn Код:
SELECT MYSQL_INSERT_ID(id) FROM vehicles ORDER BY id DESC LIMIT 1
That will retrieve the last inserted ID
|
Oooh yeah. Same problem for me as well. I guess, I've to use the system I've used on GF. Anyway thanks!