Medoo icon indicating copy to clipboard operation
Medoo copied to clipboard

mssql $database->id() is not work.

Open avavolcano opened this issue 8 years ago • 9 comments

$database->insert("account", [ "user_name" => "foo", "email" => "[email protected]", "age" => 25 ]);

$account_id = $database->id(); var_dump($account_id ); //this is null!

avavolcano avatar Dec 11 '17 18:12 avavolcano

Did you check out the id column is correctly configured?

catfan avatar Mar 23 '18 19:03 catfan

catfan, What do you mean by id column correctly configured? My id column is integer as PK w/ auto Increment. Using mssql. Having same problem. Want to say this was working at some point. Returns null.

bradleymyers avatar Nov 20 '18 22:11 bradleymyers

Catfan, here is a bit more information. The record gets created in the db fine. Just no id.

ARRAY $dataArray=array("mtyId"=>$_SESSION["store"],"typeId"=>null,"type"=>"photo","subType"=>$_POST["type"],"title"=>null,"description"=>$_POST["description"],"postDate"=>null,"date"=>null,"endDate"=>null,"status"=>"pending","lastModified"=>$database->dateFormatDb(),"lastModifiedBy"=>$_SESSION["user"]["Username"]);

Function public function genericInsert($dataArray, $table){ $this->debug(false); $this->insert($table,$dataArray); $error = $this->error(); if(!empty($error[2])){ $return["id"] = ""; $return["msg"] = $error[2]; $return["msgType"] = "light-blue accent-2"; }else{ $return["id"] = $this->id(); $return["msg"] = $this->tablePhrase($table)." item created."; $return["msgType"] = "green"; } return $return; }

Function Call $data = $database->genericInsert($dataArray, "approvalQ");

bradleymyers avatar Nov 20 '18 23:11 bradleymyers

I believe I have figured this out.

` public function id() { $type = $this->type;

	if ($type === 'oracle')
	{
		return 0;
	}
	elseif ($type === 'mssql')
	{
		//return $this->pdo->query('SELECT SCOPE_IDENTITY()')->fetchColumn();
		return $this->pdo->lastInsertId();
	}
	elseif ($type === 'pgsql')
	{
		return $this->pdo->query('SELECT LASTVAL()')->fetchColumn();
	}

	return $this->pdo->lastInsertId();
}

`

bradleymyers avatar Nov 21 '18 22:11 bradleymyers

Thank you, @bradleymyers . It works for me on: SQLServer 2008R2 with PHP7.1.1 SQLServer 2016 with PHP7.2.12

Hope @catfan can pull this.

philwu avatar Nov 23 '18 05:11 philwu

I ended up just commenting out mssql case and used the default return.

bradleymyers avatar Nov 24 '18 17:11 bradleymyers

Well, getting the inserted id for MSSQL is not just easy. There are a lot of problem and compatibility with this. I will test more version and find a better way for this database.

catfan avatar Nov 24 '18 18:11 catfan

Yeah I get that. I was playing around and weighing my options. The safest way I think is to use an application id that is generated and use that to select the record back. But that's not something that should be included in your code I don't think. Thanks for your help.

On Sat, Nov 24, 2018, 11:44 AM Catfan <[email protected] wrote:

Well, getting the inserted id for MSSQL is not just easy. There are a lot of problem and compatibility with this. I will test more version and find a better way for this database.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/catfan/Medoo/issues/683#issuecomment-441387620, or mute the thread https://github.com/notifications/unsubscribe-auth/ATFFDlCzgygzFn4rOk8DaVB39qXdldXWks5uyZNwgaJpZM4Q9zkG .

bradleymyers avatar Nov 26 '18 02:11 bradleymyers

I fixed that for MSSQL 89f06bb87179fe5d7fbbc8dbdf2ed87b9cf98e5a.

PS: We will drop support using dblib or odbc driver for MSSQL.

pdo_sqlsrv will be the better for all platform from now.

catfan avatar Dec 15 '18 01:12 catfan