donglan6967
2009-09-07 19:47
浏览 26
已采纳

Dreamweaver Recordset默认值(PHP) - 如何返回所有记录?

Greetings! Although I am working in Dreamweaver, this question may be general enough for PHP/MySQL experts to comment on.

$colname_get_voice_search_men = "-1";
if (isset($_POST['search_type'])) {
  $colname_get_voice_search_men = $_POST['search_type'];
}
mysql_select_db($database_conn_talent, $conn_talent);
$query_get_voice_search_men = sprintf("SELECT * FROM view_voice_search_men WHERE voice_type_id = %s", GetSQLValueString($colname_get_voice_search_men, "int"));
$get_voice_search_men = mysql_query($query_get_voice_search_men, $conn_talent) or die(mysql_error());
$row_get_voice_search_men = mysql_fetch_assoc($get_voice_search_men);
$totalRows_get_voice_search_men = mysql_num_rows($get_voice_search_men);

Dreamweaver sets the default value to "-1" to ensure no records are returned by default. However, I want all records to be returned by default. So, I naturally changed

$colname_get_voice_search_men = "-1"

to

$colname_get_voice_search_men != "-1"

But this doesn't change the results, which is no records are returned. I've also tried > "0" but no joy.

Your feedback is greatly appreciated.

Thanks.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxin8610 2009-09-10 15:33
    已采纳

    Probably the easiest way to accomplish your goal would be to wrap the recordset code with an if statement that checks to see if $colname_get_voice_search_men is not equal to -1 (you are supplied a "search_type" and therefore should limit the results) and then in an else branch copy the recordset code but remove the WHERE clause and the variable replacement so that you can get all of the results.

    Something like the following:

    $colname_get_voice_search_men = "-1";
    if (isset($_POST['search_type'])) {
      $colname_get_voice_search_men = $_POST['search_type'];
    }
    
    if($colname_get_voice_search_men != -1){ // search_type specified
    mysql_select_db($database_conn_talent, $conn_talent);
    $query_get_voice_search_men = sprintf("SELECT * FROM view_voice_search_men WHERE voice_type_id = %s", GetSQLValueString($colname_get_voice_search_men, "int"));
    $get_voice_search_men = mysql_query($query_get_voice_search_men, $conn_talent) or die(mysql_error());
    $row_get_voice_search_men = mysql_fetch_assoc($get_voice_search_men);
    $totalRows_get_voice_search_men = mysql_num_rows($get_voice_search_men);
    }
    else{ // return all
    mysql_select_db($database_conn_talent, $conn_talent);
    $query_get_voice_search_men = "SELECT * FROM view_voice_search_men";
    $get_voice_search_men = mysql_query($query_get_voice_search_men, $conn_talent) or die(mysql_error());
    $row_get_voice_search_men = mysql_fetch_assoc($get_voice_search_men);
    $totalRows_get_voice_search_men = mysql_num_rows($get_voice_search_men);
    }
    

    If you weren't using Dreamweaver, I'd probably put the if statement around the line creating the SQL statement ( $query_get_voice_search_men = sprintf(....) ) , but Dreamweaver will often not recognize recordsets when you modify the code it generates. As it is, the edits I'm suggesting may cause Dreamweaver to not "see" your recordset.

    So if you no longer see your recordset in the Server Behaviors panel, or entries in the Bindings panel, then Dreamweaver isn't properly recognizing the recordset code. If that is the case, then my suggestion is to build out the page using the original recordset code (and the associated repeating regions and items from the binding panel), and once you're "done" with the page, add in the conditional code. That will allow you to make the most of Dreamweaver's recordset tooling before you add in the "return all" code.

    One note: I'm assuming that your return all branch doesn't return a huge number of records. If it does, then you should probably add a LIMIT clause to the SQL so that you only get say the top 1000 or so. Check out the MySQL docs for a SELECT statement, which includes some discussion of LIMIT.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题