Efrian Okta Lianda Efrian Okta Lianda - 14 days ago 10
MySQL Question

Why '$array[]' and 'array_push' can't add more value?

PLEASE HELP ME!!

I want to convert a txt file into database.

I Have 2 table and it have a relationship.

Here is my DB:

OLT
----
id_olt
onu_number
type
serial_num

ONU
----
id_onu
id_olt
name


And here is the example for my data (*.txt)

"interface gpon-olt_1/2/1
onu 1 type ZTEG-F660 sn ZTEABCDABCD
onu 2 type ZTEG-F660 sn ZTEABCDABCD
onu 3 type ZTEG-F660 sn ZTEABCDABCD
!
interface gpon-onu_1/2/1:1
name Alex
!
"


Here my code :

// some reading file's code
...
if (strpos($values, $cari='interface gpon-olt_') !== false) {
if(strpos($values, $cari=' onu ') !== false){
$sql = mysql_query("INSERT INTO olt (id_olt, onu_number, type, serial_num)
values ('','$onu_num',$type','serial_number') ");
...


So, every $sql running, i want to catch its ID, then i put this :

$simpanidd = mysql_query("SELECT max(id_olt) from olt");
$simpanid = mysql_fetch_row($simpanidd);


But every time i run that, its show:

Array ( [0] => 96 [max(id_olt)] => 96 )
Array ( [0] => 97 [max(id_olt)] => 97 )


Its look like a new value was befall the previous value. I DONT WANT it ;(

Then i try to make '$simpanid' into an array '$simpanid[]', but it shows:

Array ( [0] => Array ( [0] => 94 [max(id_olt)] => 94 ) )
Array ( [0] => Array ( [0] => 94 [max(id_olt)] => 94 )
[1] => Array ( [0] => 95 [max(id_olt)] => 95 ) )


Please somebody help me?
I'm so confuse...

(Sorry for my bad english)

Answer

what you need to do is just get the insert_id

$simpanid = mysql_insert_id();

after your mysql_query() you will get the id generated for that query

Comments