php - Grabbing last insert id sqlsrv -
I am working on a php code where I am in two tables and want to get an ID from the table first In which I inserted, I am getting this error right now: call undefined function sqlsrv_field () . I am trying to capture routine
to routine_id
.
date_default_timezone_set ('Europe / Oslo'); $ Date = strftime ('% Y-% m-% d'); $ Time = strftime ('% H:% M:% S'); $ Value = $ _GET ['Temp']; $ Conn = sqlsrv_connect ('Bilal', $ conn_array); $ Sql = "Routine (Date, Time, Value, Amp_ID) values ('$ date', '$ time', '$ value', (Select ID from EMPS where user_name = 'ardino')); If (sqlsrv_begin_transaction ($ conn) === false) {dead (print_a (sqlsrv_errors (), true)); } $ Query = sqlsrv_query ($ conn, $ sql); If ($ query === false) {dead (print_a (sqlsrv_errors (), true)); } Sqlsrv_next_result ($ query); Sqlsrv_fetch ($ query); $ Id = sqlsrv_field ($ query, 0); $ Sql2 = "measure_lays (routine_id, measure_id, pool_id) value ('$ id', (choose the measurement from id, where title = 'a_auto_propper'), 1)"; $ Stmt = sqlsrv_query ($ conn, $ sql2); If ($ stmt === false) {die (print_as (sqlsrv_errors (), true)); }
sqlsrv_field ()
is not a function called. Instead, use:
... sqlsrv_next_result ($ query); Bool fetchStatus = sqlsrv_fetch ($ query); If (fetchStatus === false) {dead (print_a (sqlsrv_errors (), true)); } If (fetchStatus === faucet) {// do some work, there are no results in the result set} and {$ id = sqlsrv_get_field ($ query, 0); } ...
This should resolve your problem by default. However your code is weak for SQL injection. Rather than giving a direct value to the SQL query, consider using the prepared statements.
There are many articles about it, here's a quick search:
Comments
Post a Comment