2011-01-05 53 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