2011-01-05 55 views
0

我在Drupal用下面的代碼顯示可編輯的表格mysql_num_rows()錯誤

function _MYMODULE_sql_to_table($sql) { 
    $html = "";  
    // execute sql 
    $resource = db_query($sql);  
    // fetch database results in an array 
    $results = array(); 
    while ($row = db_fetch_array($resource)) { 

    $results[] = $row; 
     $id = $row['id']; 
     $email = $row['email']; 
     $comment = $row['comment']; 
//  drupal_set_message('Email: '.$email. ' comment: '.$comment. ' id: '.$id); 
    } 
    // ensure results exist 
    if (!count($results)) { 
    $html .= "Sorry, no results could be found."; 
    return $html;  
    } 

    // create an array to contain all table rows 
    $rows = array(); 
    // get a list of column headers 
    $columnNames = array_keys($results[0]); 

    // loop through results and create table rows 
    foreach ($results as $key => $data) { 
    // create row data 
    $row = array(

    'edit' => l(t('Edit'),"admin/content/test/".$data['id']."/ContactUs", $options=array()),); 

    // loop through column names 
    foreach ($columnNames as $c) { 
     $row[] = array(
     'data' => $data[$c], 
     'class' => strtolower(str_replace(' ', '-', $c)), 
    ); 
    } 

    // add row to rows array 
    $rows[] = $row; 

    } 

    // loop through column names and create headers 
    $header = array(); 
    foreach ($columnNames as $c) { 
    $header[] = array(
     'data' => $c, 
     'class' => strtolower(str_replace(' ', '-', $c)), 
    ); 
    } 

    // generate table html 
    $html .= theme('table', $header, $rows); 

    return $html; 

} 

// then you can call it in your code... 
function _MYMODULE_some_page_callback() { 

    $html = ""; 

    $sql = "select * from {contact3}"; 

    $html .= _MYMODULE_sql_to_table($sql); 

    return $html; 
} 

不過,我不斷收到mysql_num_rows()誤差

警告:mysql_num_rows():提供參數不是有效的MySQL結果資源。是什麼造成的?

+0

當發生這種情況就意味着查詢失敗,請查看相關的查詢'mysql_num_rows' – RobertPitt 2011-01-05 06:31:33

回答

0

您正在使用哪個版本的Drupal?

嘗試db_affected_rows()

db_num_rows()

+0

mysql_num_rows在代碼中不使用。 – 2011-01-05 13:23:46

+0

我知道,確切的錯誤是警告:mysql_num_rows():提供的參數不是C:\ wamp \ www \ drupal-6.19 \ includes \ database.mysql.inc在線176上的有效MySQL結果資源。當我重新加載頁面錯誤消失,但是當我提交我的表單時。我需要改變什麼?我假設我需要將db_affected_rows放在某個地方 – user550265 2011-01-05 14:47:29

+0

當'select'查詢運行時,它工作正常。只有在運行'update'查詢時,mysql_num_row()錯誤纔會出現。如何修改「更新」查詢? – user550265 2011-01-05 19:40:37