How does PHP control MySQL multi-condition query?

Source: Internet
Author: User
PHP controls MySQL multi-condition query and makes a PHP database query page. now you can enter the version number and select the line for query. the selection status is unavailable. the page is as follows,


Currently, the problem is that when you open the page for the first time, all data is automatically displayed and two errors are Prompted. how to modify the error? that is, the page is blank, the message is displayed only after you click "query". The second question is how to make the "select status" take effect. select "finish execution SQL1" and select "unfinished execution SQL2". The media version number and line are required,
The one who helped change.

The code for selecting status is written in this way, but the code cannot be executed and a syntax error is prompted.
If % {$ _ REQUEST ['zhuangtai']} % = "finished" then
$ Q = "SELECT * FROM research where banben like '% {$ _ REQUEST ['banben']} %' AND linename like '% {$ _ REQUEST ['xianlu']} % 'AND zhuangtai = 'Fin' order by busname asc ";
Elseif % {$ _ REQUEST ['zhuangtai']} % = "unfinished" then
$ Q = "SELECT * FROM research where banben like '% {$ _ REQUEST ['banben']} %' AND linename like '% {$ _ REQUEST ['xianlu']} % 'AND zhuangtai = 'new' order by busname asc ";


Complete code:



Data Query



 




$ Link = mysql_connect ("localhost", "root", "12345678 ");
If (! $ Link) echo "no connection successful! ";
Else echo "connection successful! ";
Mysql_select_db ("busap", $ link); // select a database

// Control query conditions

// If % {$ _ REQUEST ['zhuangtai']} % = "finished" then

$ Q = "SELECT * FROM research where banben like '% {$ _ REQUEST ['banben']} %' AND linename like '% {$ _ REQUEST ['xianlu']} % 'AND zhuangtai = 'Fin' order by busname asc ";
// Elseif % {$ _ REQUEST ['zhuangtai']} % = "unfinished" then
// $ Q = "SELECT * FROM research where banben like '% {$ _ REQUEST ['banben']} %' AND linename like '% {$ _ REQUEST ['xianlu' ]} % 'AND zhuangtai = 'new' order by busname asc ";

// Mysql_query ("set names GB2312"); // prevents garbled characters
$ Rs = mysql_query ($ q); // Obtain a dataset
$ Assoc = mysql_num_rows ($ rs); // The number of affected rows. mysql_fetch_assoc was used.
Echo "$ assoc ";

$ Row = mysql_fetch_array ($ rs); // you only need to add this row back.

If (! $ Rs) {die ("Valid result! ");}


?>





























Do {?>

Line

Center No.

Line

Vehicle ID

Transmission mode

Media version

Date

Time

Status



Reply to discussion (solution)

$ Q = "SELECT * FROM research where xianlu =". $ _ GET ['select']. you can add multiple conditions by yourself.

Syntax error prompted with. $ _ GET

The new progress is that "select status" is available, but when you open the page for the first time, all content is displayed, and three errors are Prompted. how to modify and open the page without querying, only click query and then query


The complete code is as follows:



Data Query



 




$ Link = mysql_connect ("localhost", "root", "12345678 ");
If (! $ Link) echo "no connection successful! ";
Else echo "connection successful! ";
Mysql_select_db ("busap", $ link); // select a database

// Control query conditions

$ Q = "SELECT * FROM research where banben like '% {$ _ REQUEST ['banben']} %' AND linename like '% {$ _ REQUEST ['xianlu']} % 'AND zhuangtai like' % {_ _ REQUEST ['zhuangtai']} % 'order by busname asc ";

// Mysql_query ("set names GB2312"); // prevents garbled characters
$ Rs = mysql_query ($ q); // Obtain a dataset
$ Assoc = mysql_num_rows ($ rs); // The number of affected rows. mysql_fetch_assoc was used.
Echo "$ assoc ";

$ Row = mysql_fetch_array ($ rs); // you only need to add this row back.

If (! $ Rs) {die ("Valid result! ");}


?>





























Do {?>

Line

Center No.

Line

Vehicle ID

Transmission mode

Media version

Date

Time

Status





Data Query



 




If ($ _ REQUEST ['submit '])
{
$ Link = mysql_connect ("localhost", "root", "12345678 ");
If (! $ Link) echo "no connection successful! ";
Else echo "connection successful! ";
Mysql_select_db ("busap", $ link); // select a database

// Control query conditions

$ Q = "SELECT * FROM research where banben like '% {$ _ REQUEST ['banben']} %' AND linename like '% {$ _ REQUEST ['xianlu']} % 'AND zhuangtai like' % {_ _ REQUEST ['zhuangtai']} % 'order by busname asc ";

// Mysql_query ("set names GB2312"); // prevents garbled characters
$ Rs = mysql_query ($ q); // Obtain a dataset
$ Assoc = mysql_num_rows ($ rs); // The number of affected rows. mysql_fetch_assoc was used.
Echo "$ assoc ";

$ Row = mysql_fetch_array ($ rs); // you only need to add this row back.

If (! $ Rs) {die ("Valid result! ");}
}

?>





























Do {?>

Line

Center No.

Line

Vehicle ID

Transmission mode

Media version

Date

Time

Status



$ Q = "SELECT * FROM research where banben like '% {$ _ REQUEST ['banben']} %' AND linename like '% {$ _ REQUEST ['xianlu']} % 'AND zhuangtai like' % {_ _ REQUEST ['zhuangtai']} % 'order by busname asc ";

Make judgments on $ _ REQUEST ['banben'], $ _ REQUEST ['xianlu'], $ _ REQUEST ['zhuangtai ']
For example:
$ Sqlstr = "";
If (! Empty ($ _ REQUEST ['banben']) {
$ Sqlstr = "and banben like '% {$ _ REQUEST ['banben']} % '";
}
If (! Empty ($ _ REQUEST ['xianlu']) {
$ Sqlstr. = "linename like '% {$ _ REQUEST ['xianlu']} % '";
}
Similarly, $ _ REQUEST ['zhuangtai']

$ Q = "SELECT * FROM research where 1 = 1 $ sqlstr order by busname asc ";

If (! Empty ($ _ REQUEST ['xianlu']) {
$ Sqlstr. = "and linename like '% {$ _ REQUEST ['xianlu']} % '";
}

Now the problem is basically solved, but an error is generated, but it is not delayed. how can this problem be left blank?



The code for Line 2 is:



The complete code is as follows:



Data Query



 



If ($ _ REQUEST ['submit ']! = "")
{
$ Link = mysql_connect ("127.0.0.1", "root", "12345678 ");
If (! $ Link) echo "no connection successful! ";
Else echo "connection successful! ";
Mysql_select_db ("busap", $ link); // select a database

// Control query conditions

$ Q = "SELECT * FROM research where banben like '% {$ _ REQUEST ['banben']} %' AND linename like '% {$ _ REQUEST ['xianlu']} % 'AND zhuangtai like' % {_ _ REQUEST ['zhuangtai']} % 'order by busname asc ";

// Mysql_query ("set names GB2312"); // prevents garbled characters
$ Rs = mysql_query ($ q); // Obtain a dataset
$ Assoc = mysql_num_rows ($ rs); // The number of affected rows. mysql_fetch_assoc was used.
Echo "$ assoc ";

$ Row = mysql_fetch_array ($ rs); // you only need to add this row back.

If (! $ Rs) {die ("Valid result! ");}
}
?>




























Do {?>

Line

Center No.

Line

Vehicle ID

Transmission mode

Media version

Date

Time

Status


Mysql_fetch_array. you can add a judgment.

How to add judgment? no, new guy.

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.