How PHP controls MySQL multi-conditional queries

Source: Internet
Author: User
Do a PHP Query database page, you can now enter the version number and select Line Query, select the status can not be used, the page is as follows,


At present, the problem is that the first time to open the page is to automatically display all the data, and prompted two errors, how to modify, that is, open the page is empty, only click on the query to show, the second question is how to make the "select State" to take effect, choose to complete the execution of SQL1, choose not complete execution SQL2, The media version number and line are required options,
The one who helped change it.

Select the state of the code I wrote this, but can not execute, prompt syntax error
If%{$_request[' Zhuangtai ']}%= "done" then
$q = "SELECT *" from the "where Banben like '%{$_request[' Banben ']}% ' and linename like '%{$_request[' Xianlu ']}% ' and Z Huangtai= ' fin ' ORDER by busname ASC;
ElseIf%{$_request[' Zhuangtai ']}%= "not done" then
$q = "SELECT *" from the "where Banben like '%{$_request[' Banben ']}% ' and linename like '%{$_request[' Xianlu ']}% ' and Z huangtai= ' new ' ORDER by busname ASC;


Full code:



<title>Data query</title>






$link =mysql_connect ("localhost", "root", "12345678");
if (! $link) echo "Not connected successfully!";
else echo "connected successfully!";
mysql_select_db ("Busap", $link); Select Database

Control query criteria

If%{$_request[' Zhuangtai ']}%= "done" then

$q = "SELECT *" from the "where Banben like '%{$_request[' Banben ']}% ' and linename like '%{$_request[' Xianlu ']}% ' and Z Huangtai= ' fin ' ORDER by busname ASC;
ElseIf%{$_request[' Zhuangtai ']}%= "not done" then
$q = "SELECT *" from the "where Banben like '%{$_request[' Banben ']}% ' and linename like '%{$_request[' Xianlu ']}% ' and zhuangtai= ' new ' ORDER by busname ASC;

mysql_query ("SET NAMES GB2312"); Prevent garbled characters
$rs = mysql_query ($q); Get Data Set
$ASSOC = mysql_num_rows ($rs); The number of rows affected, originally used MYSQL_FETCH_ASSOC
echo "$ASSOC";

$row =mysql_fetch_array ($RS); Just add back to this line.

if (! $rs) {die ("Valid result!");}


?>





























do {?>
Line Center number Line Vehicle self-numbering Transmission mode Media version Date Time State


Reply to discussion (solution)

$q = "SELECT * from the" where Xianlu = ". $_get[' SELECT ', the multi-condition itself plus go on it can

Syntax error with. $_get Hints

Now the new progress is "Select status" available, but the first time to open the page display all content, and prompt 3 error, how to modify the open page does not query, only click Query and then query


The complete code is as follows:



Data query






$link =mysql_connect ("localhost", "root", "12345678");
if (! $link) echo "Not connected successfully!";
else echo "connected successfully!";
mysql_select_db ("Busap", $link); Select Database

Control query criteria

$q = "SELECT *" from the "where Banben like '%{$_request[' Banben ']}% ' and linename like '%{$_request[' Xianlu ']}% ' and Z Huangtai like '%{$_request[' Zhuangtai ']}% ' ORDER by busname ASC ';

mysql_query ("SET NAMES GB2312"); Prevent garbled characters
$rs = mysql_query ($q); Get Data Set
$ASSOC = mysql_num_rows ($rs); The number of rows affected, originally used MYSQL_FETCH_ASSOC
echo "$ASSOC";

$row =mysql_fetch_array ($RS); Just add back to this line.

if (! $rs) {die ("Valid result!");}


?>





























do {?>
Line Center number Line Vehicle self-numbering Transmission mode Media version Date Time State




Data query






if ($_request[' submit '))
{
$link =mysql_connect ("localhost", "root", "12345678");
if (! $link) echo "Not connected successfully!";
else echo "connected successfully!";
mysql_select_db ("Busap", $link); Select Database

Control query criteria

$q = "SELECT *" from the "where Banben like '%{$_request[' Banben ']}% ' and linename like '%{$_request[' Xianlu ']}% ' and Z Huangtai like '%{$_request[' Zhuangtai ']}% ' ORDER by busname ASC ';

mysql_query ("SET NAMES GB2312"); Prevent garbled characters
$rs = mysql_query ($q); Get Data Set
$ASSOC = mysql_num_rows ($rs); The number of rows affected, originally used MYSQL_FETCH_ASSOC
echo "$ASSOC";

$row =mysql_fetch_array ($RS); Just add back to this line.

if (! $rs) {die ("Valid result!");}
}

?>





























do {?>
Line Center number Line Vehicle self-numbering Transmission mode Media version Date Time State


$q = "SELECT *" from the "where Banben like '%{$_request[' Banben ']}% ' and linename like '%{$_request[' Xianlu ']}% ' and Z Huangtai like '%{$_request[' Zhuangtai ']}% ' ORDER by busname ASC ';

$_request[' Banben '],$_request[' Xianlu '],$_request[' Zhuangtai '] make corresponding judgments
Such as:
$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 the" 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 run a mistake, but not delay, how can let him not show ah



The 81st line of code is:



The complete code is as follows:



Data query





if ($_request[' Submit ']!= "")
{
$link =mysql_connect ("127.0.0.1", "root", "12345678");
if (! $link) echo "Not connected successfully!";
else echo "connected successfully!";
mysql_select_db ("Busap", $link); Select Database

Control query criteria

$q = "SELECT *" from the "where Banben like '%{$_request[' Banben ']}% ' and linename like '%{$_request[' Xianlu ']}% ' and Z Huangtai like '%{$_request[' Zhuangtai ']}% ' ORDER by busname ASC ';

mysql_query ("SET NAMES GB2312"); Prevent garbled characters
$rs = mysql_query ($q); Get Data Set
$ASSOC = mysql_num_rows ($rs); The number of rows affected, originally used MYSQL_FETCH_ASSOC
echo "$ASSOC";

$row =mysql_fetch_array ($RS); Just add back to this line.

if (! $rs) {die ("Valid result!");}
}
?>




























do {?>
Line Center number Line Vehicle self-numbering Transmission mode Media version Date Time State

Mysql_fetch_array, just add a judgment.

How to add judgment, novice, not

  • 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.