Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 11th, 2012, 09:06 AM
Registered User
Points: 25, Level: 1
Points: 25, Level: 1 Points: 25, Level: 1 Points: 25, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default mysql_insert_id()

am using wrox php beginig 5.3 book dataobject and classes to genarate my site. i add new comment class structure like members in book. this comment class database table have auto increment id and comment columns. after insert data to mysql i want id using mysql_insert_id(). i tried several times but i cant identify parameter to above function. please help me to identify parameter.
  #2 (permalink)  
Old February 11th, 2012, 10:34 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

You use the resource id returned by connecting to the db like so:
Code:
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
// select the db here
// then do your query here...
// then use the following to obtain the last id
$new_id = mysql_insert_id($link);
You would then need to test $new_id to see if it's false etc
The Following User Says Thank You to UseLess For This Useful Post:
manna (February 12th, 2012)
  #3 (permalink)  
Old February 12th, 2012, 02:33 PM
Registered User
Points: 25, Level: 1
Points: 25, Level: 1 Points: 25, Level: 1 Points: 25, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default

useless thanks for your reply

config.php->
define( “DB_DSN”, “mysql:dbname=mydatabase” );
define( “DB_USERNAME”, “root” );
define( “DB_PASSWORD”, “mypass” );

dataobject.class.php->
protected function connect() {
try {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$conn- > setAttribute( PDO::ATTR_PERSISTENT, true );
$conn- > setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
die( “Connection failed: “ . $e- > getMessage() );
}
return $conn;
}

extend dataobj comment.class.php->
public function insert() {
$conn = parent::connect();
$sql = “INSERT INTO “ . TBL_COMMENTS . “ (
comment
) VALUES (
:comment
)”;
try {
$st = $conn- > prepare( $sql );
$st- > bindValue( “:comment”, $this- > data[“comment”], PDO::PARAM_STR );
$st- > execute();
parent::disconnect( $conn );
} catch ( PDOException $e ) {
parent::disconnect( $conn );
die( “Query failed: “ . $e- > getMessage() );
}
}

comment.php->
$comment = new comment( array(
“comment” = > isset( $_POST[“comment”] ) ? preg_replace
( “/[^ \-\_a-zA-Z0-9]/”, “”, $_POST[“comment”] ) : “”,) );

$comment- > insert();
$id = mysql_insert_id("please help me");
displayThanks();

Last edited by manna; February 12th, 2012 at 02:36 PM..
  #4 (permalink)  
Old February 12th, 2012, 03:50 PM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

As you're using a class you could add a var to the extend class:
Code:
var $last_id;
and then after the:
Code:
$st->execute();
add:
Code:
$this->last_id = mysql_insert_id($conn);
you need to add this here as the connection is destroyed so $conn will will no longer reference the correct resource id, which is why it's not working the way you are trying to do it.

you should then be able to get the id after the insert, like so;
Code:
$comment->insert();
$id = $comment->last_id;

Last edited by UseLess; February 12th, 2012 at 03:56 PM..
  #5 (permalink)  
Old February 12th, 2012, 07:42 PM
Registered User
Points: 25, Level: 1
Points: 25, Level: 1 Points: 25, Level: 1 Points: 25, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default thanx for quick reply

after an add display some massage like dis
warning: mysql_insert_id() expects parameter 1 to be resource, object given in comment.class.php line 56
  #6 (permalink)  
Old February 13th, 2012, 06:30 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Then you'll have to use this:
Code:
SELECT LAST_INSERT_ID()
as a query to get the last id, as $conn is obviously the db object and not the resource link id.
  #7 (permalink)  
Old February 13th, 2012, 09:01 AM
Registered User
Points: 25, Level: 1
Points: 25, Level: 1 Points: 25, Level: 1 Points: 25, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default thanks useless

i changed my mind ,becuz dis method so risky and not friendly. im using another simple tric like this. create column rand to generate random num. and catch comment through this random number. then clear rand num in clomn.
plz reply about this tric thank you again






Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.