get last inserted id in sql server 2008 [duplicate] - php

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'];
}

Related

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

Typo3, How can I get the MySQL query created from the query object [duplicate]

This question already has answers here:
How to debug a query in extbase?
(11 answers)
Closed 1 year ago.
I am new to Typo3. I am using 6.1 version of it.
I need to display the MySQL query generated from the query object. Please let me know how can I do that?
Below is the code snippet that is in my repository class.
$query = $this->createQuery();
$query->getQuerySettings()->setRespectStoragePage(false);
$query->matching(
$query->equals('id',intval($id))
);
return $query->execute();
I need to display the MySQL query before executing and returning the result of the query.
Please let me know how can I do that.
Thanks in advance.
in extbase its very hard to display the last query.
you might try the normal TYPO3 way, but you have to execute the query before you can do that:
$GLOBALS['TYPO3_DB']->store_lastBuiltQuery = 1;
//query
// the complete SQL-Statement
echo $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery;
Another way is to go into the buildQuery(array $sql) just before the return statement and add this snippet:
if (in_array("your_table_name", $sql['tables'])) {
var_dump($statement);
print_r($statement);
}
You can find the buildQuery method here:
TYPO3 ver.: 4.x: typo3/sysext/extbase/Classes/Persistence/Storage/Typo3DbBackend.php
TYPO3 ver.: 6.x: typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbBackend.php
Edit:
A very good method is to just misspell the column name. For example: Column is called test, call it testsdffq. The query will fail and it will show you the whole query.
May be this will help.
Make sure the following is set in localconf.php:
$TYPO3_CONF_VARS['SYS']['sqlDebug'] = '1';
$TYPO3_CONF_VARS['FE']['debug'] = '1';
try
$res = $GLOBALS['TYPO3_DB']->SELECTquery('*', 'tx_xmluploader_xml_import_tree', 'xml_import_id='.$xml_import_id);
t3lib_div::debug($res);
Result will be output of the query in the frontend. You can then execute it in MySQL for debugging.

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

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

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.

mysql query in php not returning a value? [duplicate]

This question already has answers here:
MYSQL query returning 'resource id#12 instead of the numerical value it should return
(2 answers)
Closed 9 years ago.
EDIT::
The current code being used is :
//$username= 'lmfsthefounder';
$type = "SELECT account_type from user_attribs WHERE username='lmfsthefounder';";
$type_again = mysql_query($type);
$row = mysql_fetch_row($type_again);
$usertype = $row['account_type'];
echo $usertype;
which returns like this: Home Login Register Usertype is
When I search this query in mysql query explorer, it shows me the value is 1. I need that 1 to echo in my page.
(This should display 'Usertype is 1' in my navigation bar)
The mysql function you are looking for is mysql_fetch_assoc (link) instead of mysql_fetch_row.
But please, PLEASE don't use mysql_ functions. The link to the PHP manual tells you the same.
I would recommend trying to teach yourself how to use the mySQL PDO. There are many great tutorials online.

Categories