带有可选参数的php mysql函数 [英] php mysql function with optional parameter

查看:77
本文介绍了带有可选参数的php mysql函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提供有关php函数的可选参数的帮助,这些参数将在MySQL数据库中搜索匹配条件.

I need help with giving optional parameters for php function which will search in MySQL database for the matching criteria.

方案:前端= 将有6个html输入表单字段,分别命名为authorName,authorCountry,authorState等

Scenario: Front-end = There will be 6 html input form field named as authorName, authorCountry, authorState and so on

当用户填写此字段时,字段值将作为mysql参数传递以搜索db.这里的问题是,如果用户想知道美国的作者列表,他将在国家/地区字段中进入美国,然后离开其他字段.但这会中断mysql查询,因为其他字段留为空白.

When user fills this fields, field values will be passed as a mysql parameters for searching db. Here the problem is, if user wants to know the list of authors in USA, he will enter USA in country field and leave other fields. But this will break the mysql query since other fields are left blank.

我尝试这样做,但对我来说没什么作用.

i tried doing but it dint work for me.

function getAuth(authName=NULL, authCoun=NULL, authSate=NULL) {
$query = mysql_query("SELECT * FROM authTable WHERE authName='authName'
AND authCoun='authCoun' AND authSate='authSate'");
return $query; }

请帮助我传递可选参数.

Please help me to pass optional parameter.

谢谢..

推荐答案

function getAuth($authName=NULL, $authCoun=NULL, $authSate=NULL) {
    $sql = "SELECT * FROM authTable WHERE 1 = 1";
    if ($authName) {
        $sql .= " AND authName='$authName'";
    }
     if ($authCoun) {
        $sql .= " AND authCoun='$authCoun'";
    }
     if ($authSate) {
        $sql .= " AND authSate='$authSate'";
    }
    return mysql_query($sql);
 }

在传递给函数之前,请确保已对这些变量进行了转义.

And make sure you have these variable escaped before passing to the function.

这篇关于带有可选参数的php mysql函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆