Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 返回数据库值的SQL和DataTables_Php_Mysql_Sql_Datatable - Fatal编程技术网

Php 返回数据库值的SQL和DataTables

Php 返回数据库值的SQL和DataTables,php,mysql,sql,datatable,Php,Mysql,Sql,Datatable,现在在我问之前,我知道这是我的头!我甚至不知道如何开始编辑这段代码。我被一些和我一起工作的人抛弃了,留下来独自完成一个项目!我有下面的代码,我想在那里添加 AND WHERE fieldname IS NOT NULL 现在是需要添加的原始代码 $sWhere = ""; if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" ) { $sWhere = "WHERE ("; for ( $i=0 ; $i&

现在在我问之前,我知道这是我的头!我甚至不知道如何开始编辑这段代码。我被一些和我一起工作的人抛弃了,留下来独自完成一个项目!我有下面的代码,我想在那里添加

AND WHERE fieldname IS NOT NULL
现在是需要添加的原始代码

$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
    $sWhere = "WHERE (";
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
    }
    $sWhere = substr_replace( $sWhere, "", -3 );
    $sWhere .= ')';
}

/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
    if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
    {
        if ( $sWhere == "" )
        {
            $sWhere = "WHERE ";
        }
        else
        {
            $sWhere .= " AND ";
        }
        $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
    }
}
$sWhere=”“;
如果(isset($\u GET['sSearch'])&&&$\u GET['sSearch']!=“”)
{
$sWhere=“WHERE(”;
对于($i=0;$i$iTotal,
“iTotalDisplayRecords”=>iFilteredTotal美元,
“aaData”=>array()
);
while($aRow=mysql\u fetch\u数组($rResult))
{
$row=array();
对于($i=0;$i)
尝试追加

if ($sWhere == "")
{
    $sWhere = "WHERE";
} else {
    $sWhere .= " AND"
}
$sWhere .= " fieldname IS NOT NULL";
在现有代码的最后一行之后。

尝试:

$sWhere .= " (`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' AND `".$aColumns[$i]."`IS NOT NULL) ";
也很快从我的头脑中恢复过来。我会重新检查,一旦我发现一些旧的项目,我可以修改

更新:已选中。可用于个人列筛选

下次更新:

在编码结果之前,在代码中添加类似的内容:

$output['WHERE'] = $sWhere;
echo json_encode( $output );
现在,当您键入searchterm时,可以使用firebug(或else)控制台查看来自服务器的响应,并生成查询的WHERE部分:


希望这能帮助你相处。

所以这应该是最后的答案(我希望:-)

不,另一个更新:

$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
    $sWhere = "WHERE (";
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {

        $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
    }
    $sWhere = substr_replace( $sWhere, "", -3 );
    $sWhere .= ') AND active IS NOT NULL';
}

/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
    if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
    {
        if ( $sWhere == "" )
        {
            $sWhere = "WHERE ";
        }
        else
        {
            $sWhere .= " AND ";
        }

        $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
    }
}
if ( $sWhere != "" )
{
    $sWhere .= " AND active IS NOT NULL ";
}else{
    $sWhere = " WHERE active IS NOT NULL ";
}
现在,通过此更新,
处于活动状态的行将不再显示为NULL
。无论您是进行全文搜索、单列筛选还是只是初始化表格。也许您应该检查总计数是否显示正确。我们到了吗?

您需要知道的是,如果
活动的
db字段为空,则将其替换为要忽略的字段(Carers\u Mod\u Date)。您可以在这两行中添加更多的排除项,并用
分隔


最后提示:在连接周围始终使用一些空格,因为mysql不会介意使用太多空格,但是缺少空格可能会产生错误。

感谢您的快速响应。我尝试添加了它,但得到了一个JSON数据错误。请尝试修改后的代码。这就是我从脑海中键入答案所得到的结果……让我知道这是否解决了问题。另外,我还有一个问题我假设fieldname是数据库中某个列的实际名称。对吗?嗨!别担心!我已经尝试了修订版,但它仍然可以读取所有数据。没错,fieldname与数据库中的该列相关。我还添加了一个;后面加了一个,但没有任何乐趣。再次感谢你的帮助!嗯,第二个想法:它没有看一个单独的字段是否包含搜索文本或不为null也有意义,因为一个字段可以包含可搜索文本或null,如果为null,则无论如何都不会被搜索。因此,为了优化我的答案:您想检查行中的任何字段是否为null或行中的某个特定字段是否为null吗?我将在线保留可能无用的答案因为它可能会帮助您了解更多信息。嗨!谢谢您的回复!我需要查找特定字段是否为空。您的代码很好,可以帮助我使用另一个元素,但这不是无用的!仅查找单个字段“fieldname”将不起作用是空的。你知道怎么做吗?再次感谢!很抱歉,我还是不明白,不得不再次询问。你想做如下操作:从数据库中选择*,其中名称%searchterm%和公司%searchterm%和active不为空。数据库中的特定字段是active吗?我将在一分钟内更新我的答案,以显示有什么帮助ped让我调试这些查询。我添加了一个新答案,应该可以解决这个问题。我保留了旧答案,因为我的调试提示很棒:-)不,EMPTY不是MySQL关键字。请检查是否为not NULL和!=''帮自己一个忙,安装类似phpMyAdmin的东西。它将帮助您找出数据库中的真正内容。抱歉,忽略我以前的BS。您应该使用AND(active!=''或active不为NULL)要考虑空字段和空字段。我自己开始感到困惑。HHII,再次将最后的注释更改为(Active!=)或Active不为NULL。。希望我能看到你的数据库。这对我没有帮助。当然,你不应该让陌生人访问。我今天回家前的最后想法:你用Carers\u Mod\u Date替换了active?和:时间戳也可以默认为“0000-00-00:00:00”。请尝试一下。我希望你今晚运气好。我明天一定会回到你的问题.现在我开始明白了。你根本不想在筛选中忽略Carers\u Mod\u Date中的空字段。请尝试更新更新更新后的答案:-)我希望他们不会把我们从这里赶出去。顺便问一下:Carers\u Mod\u Date中会有打字错误吗?
$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
    $sWhere = "WHERE (";
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {

        $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
    }
    $sWhere = substr_replace( $sWhere, "", -3 );
    $sWhere .= ') AND active IS NOT NULL';
}

/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
    if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
    {
        if ( $sWhere == "" )
        {
            $sWhere = "WHERE ";
        }
        else
        {
            $sWhere .= " AND ";
        }

        $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
    }
}
if ( $sWhere != "" )
{
    $sWhere .= " AND active IS NOT NULL ";
}else{
    $sWhere = " WHERE active IS NOT NULL ";
}
if ( $sWhere != "" )
{
    $sWhere .= " AND active IS NOT NULL ";
}else{
    $sWhere = " WHERE active IS NOT NULL ";
}