seoppc
seoppc

Reputation: 2824

MYSQLI custom input query

I am using mysqli class at one of my project, i want yours help with following...

  1. How to insert custom insert query like we do in mysql

    INSERT INTO payment_slip VALUES(NULL, md5(code), 'ABC', 'tester', NOW());

  2. How to get last insert id using this class.

Providing methods would be great help, thanks.

Upvotes: 0

Views: 293

Answers (2)

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

Running mysqli_insert_id() will also work, but you should use it like vbence said.

Upvotes: 0

vbence
vbence

Reputation: 20333

Just like the plain old mysql functions mysqli has a field for this too:

mysqli->insert_id

From php docs (note that this only demonstrates getting the ID, the parameters are hardcoded into the query):

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "INSERT INTO payment_slip VALUES(NULL, md5(code), 'ABC', 'tester', NOW())";
$mysqli->query($query);
printf ("New Record has id %d.\n", $mysqli->insert_id);

Upvotes: 1

Related Questions