Query the maximum value of a field and assign it to the variable. how can this problem be solved?

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.