Toon posts:

[php/mysql] mysql_insert_id()

Pagina: 1
Acties:
  • 62 views sinds 30-01-2008

Verwijderd

Topicstarter
Ben al een tijdje bezig om het id van een entry terug te krijgen. Maar het blijft een vage bedoeling. Helaas werkt de search nog niet, dus tja...

Ik heb hier een stuk gestripte code:
PHP:
1
2
3
4
5
6
7
8
9
10
11
function blaat() {
        require("includes/config.php");
        $mysql_link = mysql_connect($DBhost, $DBuser, $DBpass) or die(mysql_error());
        $db_select = mysql_select_db($DBname, $mysql_link) or die(mysql_error());

        // Hiertussen een heleboel niet interessante bende


        $result = mysql_insert_id();  // Dit is regel 97!!!
        return($result);
}


Alle queries in deze functie worden helemaal goed uitgevoerd, maar ik krijg toch een warning:
Warning: MySQL: A link to the server could not be established in ... on line 97
Als ik voor de zekerheid die regel verander in:

PHP:
1
        $result = mysql_insert_id($mysql_link);  // Dit is regel 97!!!


krijg ik de foutmelding:
Warning: Supplied argument is not a valid MySQL-Link resource in ... on line 97
Nergens in de code worde de verbinding met MySQL verbroken oid. En alle queries geven geen foutmeldingen. Iemand een suggestie?

  • D2k
  • Registratie: Januari 2001
  • Laatst online: 31-08 10:19

D2k

je doet het niet goed zo te zien
je moet het result van een mysql_query() voeren aan mysql_insert_id() en niet
PHP:
1
 $mysql_link = mysql_connect($DBhost, $DBuser, $DBpass) or die(mysql_error());

Doet iets met Cloud (MS/IBM)


  • DanceTrend
  • Registratie: Maart 2001
  • Laatst online: 12-05 14:34

DanceTrend

w0ei

Ik dacht dat dit alleen werkte als je op dezelfde page iets hebt geinsert, zodat je gelijk het id kon zien als je auto_increment gebruikt?

  • D2k
  • Registratie: Januari 2001
  • Laatst online: 31-08 10:19

D2k

DanceTrend schreef op 01 augustus 2002 @ 18:16:
Ik dacht dat dit alleen werkte als je op dezelfde page iets hebt geinsert, zodat je gelijk het id kon zien als je auto_increment gebruikt?
ligt eraan
mysql_insert_id
(PHP 3, PHP 4 )

mysql_insert_id -- Get the ID generated from the previous INSERT operation
Description
int mysql_insert_id ( [resource link_identifier])


mysql_insert_id() returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query using the given link_identifier. If link_identifier isn't specified, the last opened link is assumed.

mysql_insert_id() returns 0 if the previous query does not generate an AUTO_INCREMENT value. If you need to save the value for later, be sure to call mysql_insert_id() immediately after the query that generates the value.

Note: The value of the MySQL SQL function LAST_INSERT_ID() always contains the most recently generated AUTO_INCREMENT value, and is not reset between queries.


Warning
mysql_insert_id() converts the return type of the native MySQL C API function mysql_insert_id() to a type of long (named int in PHP). If your AUTO_INCREMENT column has a column type of BIGINT, the value returned by mysql_insert_id() will be incorrect. Instead, use the internal MySQL SQL function LAST_INSERT_ID() in an SQL query.
dus je kan um ook een resource voeren. en zoals Dennis al zei voert ie meerdere query's uit. dan is het imho handiger de resource erbij te zetten

Doet iets met Cloud (MS/IBM)


  • dusty
  • Registratie: Mei 2000
  • Laatst online: 21-02 00:06

dusty

Celebrate Life!

[ik praatte onzin]

Back In Black!
"Je moet haar alleen aan de ketting leggen" - MueR


Verwijderd

Topicstarter
Thanx guys.. tis (al) opgelost.

Uiteindelijk bleek het allereerste voorbeeld wel te werken. M'n servertje bleek een beetje kuren te hebben en na een reboot werkte alles weer normaal..

  • Nielsz
  • Registratie: Maart 2001
  • Niet online
why am I not surprised? >:)

  • D2k
  • Registratie: Januari 2001
  • Laatst online: 31-08 10:19

D2k

mooi dat het opgelost is
en voor dit nog verder oftopic gaat: slotje

Doet iets met Cloud (MS/IBM)

Pagina: 1

Dit topic is gesloten.