MySQL PDO Connection, how do I retrieve the inserted ID? [duplicate] - php

This question already has answers here:
Get last insert id after a prepared insert with PDO
(3 answers)
Closed 8 years ago.
I have a PDO connection where I am inserting a new ROW in the databse:
$sql = UPDATE invoices SET ..."
Or
$sql = INSERT INTO invoices ..."
Then:
$stmt = self::getPDOConnection(self::DEFAULT_CONNECTION_NAME)->prepare($this->sql);
$stmt->execute($this->values);
And I would like to get the id of the updated, inserted element.
Thank you.

You can use this.
public string PDO::lastInsertId ([ string $name = NULL ] )
FYI Link

Related

How to count records in query result on SQLite3 using PHP [duplicate]

This question already has answers here:
Using SQlite3 in PHP how to count the number of rows in a result set?
(6 answers)
Closed 5 years ago.
I need to make a query in my SQLite3 database using PHP and this is my code and it's working:
# Set access to data base...
$db = new SQLite3('./Data/myDB.sqlite');
# Set the query ...
$q="SELECT * FROM my_table WHERE key = '123'";
# Prepare for the query ...
$stmt = $db->prepare($q);
# Execute the query ...
$results = $stmt->execute();
Now, how to count how many records there are in my result? Suggestions / examples?
You could either do it in SQL or in PHP:
Change your SQL request to
SELECT COUNT(*) FROM my_table WHERE key = 123
Or count rows in PHP (taken from here) :
$nrows = 0;
$result->reset();
while ($result->fetchArray())
    $nrows++;
$result->reset();
return $nrows;

How to extract all data in PHP OOP style which consists the result of executed query [duplicate]

This question already has answers here:
mysqli last insert id
(3 answers)
Closed 5 years ago.
I am executing the following sql code
$query = "INSERT INTO order_shipping_addresses(user_customer_id, shipping_address_details, created, modified) VALUES('".$_SESSION['user_id']."', '".serialize($address)."', '".$created."', '".$modified."')";
$connection = $this->establish_connection();
$data = $connection->query($query);
$order_shipping_address_id = last_insert_id();
$connection->close();
What i wanted was when this query is executed i wanted the row id in which this data is inserted, i am using the last_insert_id() function but i am getting an error of Call to undefined method mysqli::lastInsertId()
how to extract all the data from the $data variable and also the row id in which the data is inserted. I am using PHP OOP style.
Thanks in advance..
You want to use $connection->insert_id

PHP-PDO: Display all rows [duplicate]

This question already has answers here:
How can I use PDO to fetch a results array in PHP?
(2 answers)
Closed 2 years ago.
I'm doing var_dump since I started to learn PHP/PDO when I want to display the SQL datas, like:
$tickets = $db->query('SELECT * FROM tickets')->fetchAll();
var_dump($tickets);
And it shows everything.
But now I'd like to echo a list of all tickets name, usually I would do an echo $tickets->name; but first it doesn't work with fetchAll(); and when I use fetch(); it returns only the last row
Noob question but all the answers I get are outdated and uses depreciated mysql, how can I just display the rows with PDO ?
Thanks !
PDO fetch_all default to fetching both numeric and associative arrays
$tickets = $db->query('SELECT * FROM tickets')->fetchAll();
foreach($tickets as $ticket) {
echo $ticket['name'];
}

Getting a saved query in the database mysql [duplicate]

This question already has answers here:
How can I prevent SQL injection in PHP?
(27 answers)
Closed 8 years ago.
I have a challenge getting a query i saved into the database.
On my project, i want to save mysql update query in the database, and when another action is run on a different page, i get that query and pass it through my function and it runs. However after getting it its empty. Below is what i have done so far.
//Saving the query in database
$query = array();
$query['approved'] = "UPDATE payment SET pm_status = 'Approved' WHERE id = '69'";
$query['approved2'] = "UPDATE member SET pay_date = NOW())";
//i den serialize and addslashes so it can be saved in the database
$tosave = addslashes(serialize($query));
$sql = "INSERT INTO trans (saved_query) VALUES ($tosave)";
// I den save $tosave into the database, which i checked and it was saved.
On the page i want to use it, each time i get the value and unserialize, it returns as empty e.g after selecting the value to a row
$toprocess = unserialize($query['saved_query']);
echo $toprocess['approved'];
it returns an empty value, and when i run it in a query it doesn't run.
but if i echo directly without unserializing, it dispplays the values in it
echo $query['saved_query'];
Pls help incase i am missing something here. Thanks
I am not preventing an sql injection. I had dont that already. I just want to run the sql saved into the database. This is different to the answers in the prevention of mysql injection. Thanks
There is an error in your query
$query['approved2'] = "UPDATE member SET pay_date = NOW())";
there is an extra bracket with now() function.

get last inserted id in sql server 2008 [duplicate]

This question already has answers here:
How to get Insert id in MSSQL in PHP?
(3 answers)
Closed 10 years ago.
I want to get last inserted id of particular table. can any one tell me how i can get that?
we have mysql_insert_id() in mysql. Do we have similar kind of function in sql server 2008?
something like this works very nicely
$sql = "insert into tables(field) values (value);SELECT SCOPE_IDENTITY()";
$db = getConnection();
$results = mssql_fetch_assoc(mssql_query($sql));
$lastid=$results['computed'];
I made this function to make it easy :)... if you have more then one db resource just pass that into the mssql_query request. This will retrieve the last unique id generated on that resource connection.
mssql_query("insert into data(name) values('bob')");
$id = getLastId();
function getLastId() {
$result = mssql_fetch_assoc(mssql_query("select ##IDENTITY as id"));
return $result['id'];
}

Categories