What might be the problem? [Question]
#1

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();
Reply
#2

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
Reply
#3

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.
Reply
#4

pawn Код:
mysql_query("INSERT INTO SOMETHING (SOMETHING) VALUE (SOMETHING)");
mysql_insert_id();
Reply
#5

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.
Reply
#6

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.
Reply
#7

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();
Reply
#8

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.
Reply
#9

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
Reply
#10

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!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)