douhui8454 2013-01-22 18:07
浏览 49
已采纳

如何使用函数从mysql中拉出多行

How can I pull multiple rows from a database using a function?

The function I have is:

function search($subject, $table) {
    $query = "SELECT {$subject} ";
    $query .= "FROM {$table} ";
    $content = mysql_query($query);
    return $content;
}

On the page which is calling the function I have:

if (isset($_POST['search'])){
    $search = $_POST['search'];
}

$content = search($subjectName, $tableName);

while ($results = mysql_fetch_assoc($content)){
    $phrase = $results[$subjectName];
    //if phrase exists in database
    if (strpos($search,$phrase) !== false) {
        echo $phrase;
    //if phrase does not exist in database
    } else {
        echo 'fail';    
}

This setup does not work, however if I put everything into the function it works:

function search($subject, $table, $where = 0, $is = 0) {
    global $search;
    $query = "SELECT {$subject} ";
    $query .= "FROM {$table} ";
    if ($where > 0) {
        $query .= "WHERE {$where} = '{$is}' ";
    }
    $content = mysql_query($query);
    while ($results = mysql_fetch_assoc($content)){
        $phrase = $results[$subject];
        //if phrase exists in database
        if (strpos($search,$phrase) !== false) {
            echo $phrase;
        //if phrase does not exist in database
        } else {
            echo 'fail';
        }
    }
    return $content;
}

On Page:

search('main_subject', 'main_search');

The problem is that I would like to call that function again in the if statement to have it search for another phrase. Is there an easier way to do this?

EDIT: The current setup pulls the first item in an infinite loop.

  • 写回答

1条回答 默认 最新

  • duanre1891 2013-01-22 18:24
    关注

    There are a number of issues that should be addressed here:

    First, if you are trying to search a field for a specific partial match, you would likely want to use SQL LIKE construct.

    SELECT field FROM table WHERE field LIKE '%search phrase%'
    

    Doing this would eliminate the need for you to iterate through each row trying to do a string comparison for your search phrase, as you would only be returned the rows that match the search phrase and nothing more.

    Second, using global to make data available to your function is really poor practice. You really should be passing any data needed by the function to the function as a parameter. This would include your search string and probably your database connection/object.

    function search($field, $table, $search, $db) {
        ...
    }
    

    Third, You are doing nothing at all to prevent against SQL injection right now. You need to escape the input data or, better yet use prepared statements.

    Fourth, you really should not be using the mysql_* functions. They are deprecated. Try using mysqli or PDO (and I would highly recommend going ahead and learning how to use prepared statements with either of these.) You might start with mysqli ,at it provide procedural functions similar to mysql_* so the learning curve might be less steep (though really most experienced developers would prefer the object-oriented usage).

    Fifth, to your original question. If you want search for multiple phrases, there are a couple approaches you can take. You can either pass all the phrases at once like this:

    SELECT field FROM table WHERE field LIKE '%search phrase%' OR field LIKE '%another search phrase%'
    

    Or, you could just make iterative function calls to get the results you want. This really depends on whether you only want to search for the second phrase if the first is not successful (use the iterative approach) or whether you just want all possible matches in one query (use the LIKE-OR-LIKE approach).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作