-2

I hope you all have a good day.

Actually I'm deploying a query to get a complete array of data. But the fact is that I need an Id, first of all, I guess, I must get the last Id first, then I can apply a mathematic operation to get its value + 1. The fact is that I've been trying different sentences and queries with no result.

This is my code:

function obtener_Id(){
global $mysqli;
$resultado_oId = $mysqli ->query("SELECT TOP 1 'id' FROM 'pacient' ORDER BY RowID DESC ")
$id_sacada = mysqli_fetch_assoc($resultado_oId);
$id_enLaMano = $id_sacada['id'];
return $id_enLaMano;
//$id_dinamica = $id_enLaMano + 1;
//return $id_dinamica;
}

As you can see guys, I've commented on the last two lines, cause I´m looking to get at least a value (The result from a query) But Idk if that is correct. Looking on the Internet I've seen relative posts which are solved just to apply the query that we can view under global declarations...

I've tried that on phpMyAdmin with no results and a bunch of errors...

You guys know the correct way to get the max value in the Id column? Or even if I'm doing badly correct me.

A lot of hugs and Luck! Mizar ^^

Stidgeon
  • 2,673
  • 8
  • 20
  • 28
  • Why are you trying to add 1 to the id? If you're attempting to get sequential id's, then let the database do that for you with an Identity. Attempting to sequence id's yourself is not a good idea. – devlin carnate Mar 30 '20 at 19:09
  • It looks like you're using `SELECT TOP 1...` syntax, which works in Microsoft SQL Server and Sybase but not in other brands of SQL databases. In MySQL for example, you would use `ORDER BY RowID DESC LIMIT ` at the end of your query. – Bill Karwin Mar 30 '20 at 19:11
  • Another problem: you are putting your column name and table name inside straight single quotes, which are not used for identifiers. Straight single quotes are for string literals or datetime literals. See this explanation: https://stackoverflow.com/a/11321508/20860 – Bill Karwin Mar 30 '20 at 19:12
  • Thanks for helping Devlin Carnate, the fact is that I'm registering patients manually. I'm going to insert data Via Dialogflow Chatbot in a WebPage. As you mentioned, there is a way to let the database create an id on its own? – Mizar Contasti Mar 30 '20 at 19:16
  • Bill Karwin thanks for clarifying, I've seen that and I got the solution. The command to get the id is SELECT MAX(id) FROM table... – Mizar Contasti Mar 30 '20 at 19:19

1 Answers1

-2

I would suggest putting the serial number in a table. Read the serial no before insert, (lock the table, if required) insert the data, then increase the serial no by 1 and update the serial no table with increased value.

M.D
  • 270
  • 2
  • 11