PHP + MySQL打印查询结果

时间:2014-05-31 17:31:45

标签: php mysql sql arrays echo

我曾经打印过这样的MySQL查询结果:

$query="SELECT fname,lname,email FROM users";
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    mysql_close();
    $i=0;
    echo '<table BORDERCOLOR=black>';
    ?>
    <tr>
    <th>Vardas</th><th>Pavarde</th><th>E-Mail</th>
    </tr>

    <?php

    while ($i < $num) {
        $field1=mysql_result($result,$i,"fname");
        $field2=mysql_result($result,$i,"lname");
        $field3=mysql_result($result,$i,"email");

        echo "<td>";
        echo $field1; 
        echo "</td>";
        echo "<td>";
        echo $field2;
        echo "</td>";
        echo "<td>"; 
        echo $field3;
        echo "</td>";
        echo "</tr>";
        $i++;
    }
    echo "</table>";

但是这次我必须使用不同的东西,我想知道如何在使用下面显示的函数/查询时打印出我的结果:

查询代码(在&#39;类DB&#39;中):

function query($querytext) {
        $rs = mysql_query($querytext, $this->_link);
        if($this->_debug) {
            $this->addDebugMessage("\t<tr>\n\t\t<td class=\"debug_nr\">".$this->_queryCount++."</td>\n\t\t<td class=\"debug_queInfo\"><b>Query: (".@mysql_num_rows($rs).")</b></td>\n\t\t<td>" . htmlspecialchars($querytext) . "</td>\n\t</tr>\n");
            if(mysql_error() != '') {
                $this->addDebugMessage("\t<tr>\n\t\t<td class=\"debug_nr\">".$this->_queryCount++."</td>\n\t\t<td class=\"debug_queInfo\"><b>Error #".mysql_errno($this->_link)." :</b></td>\n\t\t<td>" . htmlspecialchars(mysql_error($this->_link)) . "</td>\n\t</tr>\n");
            }
        }
        if($rs) {
            $num_rows = @mysql_num_rows($rs);
            if($num_rows) {
                if($num_rows > 0) {
                    $rsarray = array();
                    while($line = mysql_fetch_array($rs , MYSQL_ASSOC)) {
                        array_push($rsarray, $line);
                    }
                    mysql_free_result($rs);
                    return $rsarray;
                } else {
                    return false;
                }
            } else {
                if(mysql_affected_rows($this->_link) > 0) {
                    return true;
                } else {
                    return false;
                }
            }
        } else {
            return false;
        }
    }

(在&#39;类用户&#39;)

function findUser($phrase){
        global $DB;
        $results=$DB->query("SELECT * FROM users WHERE fname LIKE '%'$phrase'%' OR lname LIKE '%'$phrase'%' OR email LIKE '%'$phrase'%'");
            return $results;
    }

在index.php中(我用什么来打印出来?):

$USER->findUser("john");

或者它应该是

$results= $USER->findUser("john);

OR

$results=DB->findUser("john");

1 个答案:

答案 0 :(得分:0)

好像你有拼写错误

function findUser($phrase){
        global $DB;
        $DB->query("SELECT * FROM users WHERE fname LIKE '%'$phrase'%' OR lname LIKE '%'$phrase'%' OR email LIKE '%'$phrase'%'");
    }

您的查询中有额外的引号。

function findUser($phrase){
        global $DB;
        $DB->query("SELECT * FROM users WHERE fname LIKE '%$phrase%' OR lname LIKE '%$phrase%' OR email LIKE '%$phrase%'");
    }