无法使用php中的mysqli从数据库中检索数据

时间:2015-07-13 14:14:19

标签: php database mysqli web

我无法弄清楚我的代码到底出了什么问题我似乎无法从我的数据库中检索数据。

我在php checker上完全没有错误,它在测试之前就已经工作了然后我扩展了代码以获得更好的搜索模式,现在编码的一半突然停止工作,我无法弄清楚问题。

我知道与我的数据库的连接正常,因为我将其用于我网站的另一部分。

非常感谢任何有关此事的帮助。

<?php
            include("search/dbconnect.php");
            $count;
            $searchtext= $_POST['search-box'];
            $searchbit=explode(" ", $_POST['search-box']);
            $searchsound= metaphone($_POST['search-box']);
            $sqlName="SELECT * FROM SearchPG WHERE PGName LIKE '%".$_POST['search-box']."%'";
            $sqltext="SELECT * FROM SearchPG WHERE PGName LIKE '%".$_POST['search-box']."%' OR PGKeywords LIKE '%".$_POST['search-box']."%' ";
            $sqlsound="SELECT metaphonetext.*, SearchPG.* FROM metaphonetext JOIN SearchPG ON metaphonetext.PGID=SearchPG.PGID WHERE metaphonetext.NameSound LIKE '%".$searchsound."%'";
            $qryname=mysqli_query($dbconnect, $sqlName);
            $qrytext=mysqli_query($dbconnect, $sqltext);
            $qrysound=mysqli_query($dbconnect, $sqlsound);

            if(mysqli_num_rows($qryname)>0){
                $rs=mysqli_fetch_assoc($qryname);
                do{
                    ?>
                    <div class="row resultcontent">
                        <a href="<?php echo $rs['PGUrl'];?>">
                            <div class="col-sm-8 searchRsltCont">
                                <img src="<?php echo $rs['PGImgUrl'];?>" class="searchRsltimg" alt="search result image" />
                                <div class="contshading">
                                </div>
                            </div>
                            <div class="col-sm-4 searchRslttxt">
                                <h1><?php echo $rs['PGName'];?></h1>
                            </div>
                        </a>
                    </div>
                    <?php
                    $count++;
                } while($rs=mysqli_fetch_assoc($qryname));
            } elseif(mysqli_num_rows($qrytext)>0){
                foreach($searchbit as $value){
                    $temp = $value;
                    $sqltext="SELECT * FROM SearchPG WHERE PGName LIKE '%".$temp."%' OR PGKeyword LIKE '%".$temp."%' ";
                    $qrytext=mysqli_query($dbconnect, $sqltext);
                    $rs=mysqli_fetch_assoc($qrytext);
                    do{
                    ?>
                        <div class="row resultcontent">
                        <a href="<?php echo $rs['PGUrl'];?>">
                            <div class="col-sm-8 searchRsltCont">
                                <img src="<?php echo $rs['PGImgUrl'];?>" class="searchRsltimg" alt="search result image" />
                                <div class="contshading">
                                </div>
                            </div>
                            <div class="col-sm-4 searchRslttxt">
                                <h1><?php echo $rs['PGName'];?></h1>
                            </div>
                        </a>
                    </div>
                    <?php
                    $count++;
                } while($rs=mysqli_fetch_assoc($qrytext));
                }
            } elseif(mysqli_num_rows($qrysound)>0){
                $rs=mysqli_fetch_assoc($qrysound);
                do{
                    ?>
                    <div class="row resultcontent">
                        <a href="<?php echo $rs['PGUrl'];?>">
                            <div class="col-sm-8 searchRsltCont">
                                <img src="<?php echo $rs['PGImgUrl'];?>" class="searchRsltimg" alt="search result image" />
                                <div class="contshading">
                                </div>
                            </div>
                            <div class="col-sm-4 searchRslttxt">
                                <h1><?php echo $rs['PGName'];?></h1>
                            </div>
                        </a>
                    </div>
                    <?php
                    $count++;
                } while($rs=mysqli_fetch_assoc($qrysound)); 
            }else{
                foreach($searchbit as $value){
                    $temp = metaphone($value);
                    $sqlsound="SELECT metaphonetext.*, SearchPG.* FROM metaphonetext JOIN PGName ON metaphonetext.PGID=SearchPG.PGID WHERE metaphonetext.NameSound LIKE '%".$temp."%'";
                    $sqlsoundkey="SELECT metaphonetext.*, SearchPG.* FROM metaphonetext JOIN PGKeywords ON metaphonetext.PGID=SearchPG.PGID WHERE metaphonetext.KeywordSound LIKE '%".$temp."%'";
                    $qrysound=mysqli_query($dbconnect, $sqlsound);
                    $qrysoundkey=mysqli_query($dbconnect, $sqlsoundkey);
                    if(mysqli_num_rows($qrysound)>0){
                        $rs=mysqli_fetch_assoc($qrysound);
                        do{
                        ?>
                            <div class="row resultcontent">
                                <a href="<?php echo $rs['PGUrl'];?>">
                                    <div class="col-sm-8 searchRsltCont">
                                        <img src="<?php echo $rs['PGImgUrl'];?>" class="searchRsltimg" alt="search result image" />
                                        <div class="contshading">
                                        </div>
                                    </div>
                                    <div class="col-sm-4 searchRslttxt">
                                        <h1><?php echo $rs['PGName'];?></h1>
                                    </div>
                                </a>
                            </div>
                        <?php
                        } while($rs=mysqli_fetch_assoc($qrysound));
                    }else if(mysqli_num_rows($qrysoundkey)>0){
                        $rskey=mysqli_fetch_assoc($qrysoundkey);
                        do{
                        ?>
                            <div class="row resultcontent">
                                <a href="<?php echo $rskey['PGUrl'];?>">
                                    <div class="col-sm-8 searchRsltCont">
                                        <img src="<?php echo $rskey['PGImgUrl'];?>" class="searchRsltimg" alt="search result image" />
                                        <div class="contshading">
                                        </div>
                                    </div>
                                    <div class="col-sm-4 searchRslttxt">
                                        <h1><?php echo $rskey['PGName'];?></h1>
                                    </div>
                                </a>
                            </div>
                        <?php
                        } while($rs=mysqli_fetch_assoc($qrysoundkey));
                    }else if(mysqli_num_rows($qrysound)<=0 && mysqli_num_rows($qrysoundkey)<=0){
                        ?>
                        <p>no results found</p>
                        <?php
                    }else{
                        ?>
                            <p>no more results found</p>
                        <?php
                    }
                }


            }
        ?>

1 个答案:

答案 0 :(得分:0)

感谢大家指出我的代码中的缺陷,如果我没有在这里发布,在我的数据库受到攻击之前我不会知道任何这些,我正在努力修复它们。我发现我的问题不是代码,虽然它更多地与我缺乏知识有关,你看到测试后我将我的dbconnection参数移动到一个ini文件并将它们存储在我的根文件夹之外以保证它的安全,那里有dbconnection变量然后变成一个局部变量,并且在存储变量的文件夹之外基本上无法使用。解决方案很简单我只是将我的搜索引擎移动到同一个文件夹。