Query the maximum value of a field and grant it to the variable. I can see a lot of code on the Internet, but it cannot be inserted into my file. please help me to see it, how to query the maximum value of a field and assign it to the variable? I add 1 after the query. & Lt ;? Php & nbsp; $ db_host = & quot; localhost & quot; // server & nbsp; $ db _ query the maximum value of a field and assign it to the variable
I have seen a lot of code on the Internet, but it cannot be inserted into my file. please help me to see how to query the maximum value of a field and then assign it to the variable, I add 1 after the query.
$ Db_host = "localhost"; // server
$ Db_user = "root"; // User name
$ Db_psw = "123456"; // password
$ Db_name = "test"; // database name
$ Conn = mysql_connect ($ db_host, $ db_user, $ db_psw) or die ("link error ");
Mysql_select_db ($ db_name, $ conn) or die ("link error again ");
Mysql_query ("set names UTF-8 ");
$ Line = 0;
$ File = fopen ('upload/amazon_erp.csv ', "r ");
$ StrSql = "select max (sku) FROM table ";
$ Query = mysql_query ($ strSql, $ conn );
$ Resulta = mysql_fetch_row ($ query );
$ Maxid = $ resulta;
Print_r ($ resulta );
While ($ result = fgetcsv ($ file )){
// Read a line of content in the CSV each time.
If ($ line <> 0)
{
$ Maxid ++;
$ Sku = $ result [0];
$ Name = $ result [1];
$ Address = $ result [2];
$ A = $ result [3];
$ B1 = $ result [16];
$ B2 = $ result [17];
$ B3 = $ result [18];
$ B = "$ b1, $ b2, $ b3 ";
$ C = "$ sku, $ name, $ address, $ a, $ B ";
Echo $ c;
Echo"
";
$ SQL = "INSERT INTO 'test '. 'Table' ('$ resulta', 'name', 'address', 'A', 'B') VALUES ('$ resulta', '$ name ', '$ address',' $ A', '$ B ')";
Mysql_query ($ SQL );
}
$ Line ++;
Fclose ($ file );
Echo "imported successfully ";
?>
------ Solution --------------------
SQL code
# Maximum field value: select MAX (id) from table;
------ Solution --------------------
Discussion
The problem of querying the maximum value is solved.
$ StrSql = "select max (order_no) FROM '0 _ sales_orders '";
The table name ''is a row, and I don't know why.
Also, I would like to ask, how should I write the code like four paragraphs when I want to query the maximum ID of four tables at a time ????
------ Solution --------------------
Discussion
How can I escape the code? Thank you!
------ Solution --------------------
SQL code
Select max (c. id), MAX (I. id) // c. id <=> t1.idFROM t1 AS c, // defines the abbreviation of table t1 AS ct2 AS I; // AS above, defines the abbreviation of table t2 AS I