2017-10-29 27 views
-1

私は現在mysql_field_nameを使用していた関数をmysqli_fetch_field_directに変換していますが、これを行う方法だと思っていますが、データをcsvファイルに入れてもOKですが、なぜ列名を取得できないのか分かりません。mysqli_fetch_field_directエラーを使用してフィールド名を取得していますか?

警告:stripslashes()は、パラメータ1が文字列であると想定しています。オブジェクトは..............で与えられます(これは私がフィールド名)

private function buildCSV($link, $file) { 
// container to hold the CSV file as it's built 
$csv_file = ""; 

// run the MySQL query and check to see if results were returned 
$result = mysqli_query($link, $file["mySQL_query"]); 
if (!$result) { 
    die("ERROR: Invalid query \n MySQL error: " . mysqli_error($link) . "\n Your query: " . $this->mySQL_query); 
} 

// only return a non blank data set with query returns at least one record 
$num_of_rows = mysqli_num_rows($result); 
if ($num_of_rows > 0) { 
    if ($this->debugFlag) { 
    echo "Step 3 (repeats for each attachment): MySQL query ran successfully. \n\n"; 
    } 

    // store the number of columns from the results 
    $columns = mysqli_num_fields($result); 
    // Build a header row using the mysql field names 
    $header_row = ''; 
    for ($i = 0; $i < $columns; $i++) { 
    $column_title = $file["csv_contain"] . stripslashes(
     mysqli_fetch_field_direct($result, $i)) . $file["csv_contain"]; 
    $column_title .= ($i < $columns-1) ? $file["csv_separate"] : ''; 
    $header_row .= $column_title; 
    } 
    $csv_file .= $header_row . $file["csv_end_row"]; // add header row to CSV file 

    // Build the data rows by walking through the results array one row at a time 
    $data_rows = ''; 
    while ($row = mysqli_fetch_array($result)) { 
    for ($i = 0; $i < $columns; $i++) { 
     // clean up the data; strip slashes; replace double quotes with two single quotes 
     $data_rows .= $file["csv_contain"] . preg_replace('/'.$file["csv_contain"].'/', $file["csv_contain"].$file["csv_contain"], stripslashes($row[$i])) . $file["csv_contain"]; 
     $data_rows .= ($i < $columns-1) ? $file["csv_separate"] : ''; 
    } 
    $data_rows .= $this->csv_end_row; // add data row to CSV file 
    } 
    $csv_file .= $data_rows; // add the data rows to CSV file 

    if ($this->debugFlag) { 
    echo "Step 4 (repeats for each attachment): CSV file built. \n\n"; 
    } 
} else { 
    echo "Step 3 (repeats for each attachment): MySQL query ran successfully \n\n"; 
    echo "Step 4 (repeats for each attachment): NO results were returned for this query \n\n"; 
} 

// Return the completed file 
return $csv_file; 

は}

答えて

0

誰もがここで働いていた私の解決策だった同じ問題に直面してきた場合。

private function buildCSV($link, $file) { 
// container to hold the CSV file as it's built 
$csv_file = ""; 

// run the MySQL query and check to see if results were returned 
$result = mysqli_query($link, $file["mySQL_query"]); 
if (!$result) { 
    die("ERROR: Invalid query \n MySQL error: " . mysqli_error($link) . "\n Your query: " . $this->mySQL_query); 
} 

// only return a non blank data set with query returns at least one record 
$num_of_rows = mysqli_num_rows($result); 
if ($num_of_rows > 0) { 
    if ($this->debugFlag) { 
    echo "Step 3 (repeats for each attachment): MySQL query ran successfully. \n\n"; 
    } 

    // store the number of columns from the results 
    $columns = mysqli_num_fields($result); 
    // Build a header row using the mysql field names 
    $header_row = ''; 
    // Create an array of column names 
     $cols = mysqli_fetch_fields($result); 
    $colname = array(); 
    foreach ($cols as $col){ 
     array_push($colname, $col->name); 
    } 

    for ($i = 0; $i < $columns; $i++) { 
    $column_title = $file["csv_contain"] . stripslashes(
     $colname[$i]) . $file["csv_contain"]; 
    $column_title .= ($i < $columns-1) ? $file["csv_separate"] : ''; 
    $header_row .= $column_title; 
    } 

$csv_file .= $header_row . $file["csv_end_row"]; // add header row to CSV file 

    // Build the data rows by walking through the results array one row at a time 
    $data_rows = ''; 
    while ($row = mysqli_fetch_array($result)) { 
    for ($i = 0; $i < $columns; $i++) { 
     // clean up the data; strip slashes; replace double quotes with two single quotes 
     $data_rows .= $file["csv_contain"] . preg_replace('/'.$file["csv_contain"].'/', $file["csv_contain"].$file["csv_contain"], stripslashes($row[$i])) . $file["csv_contain"]; 
     $data_rows .= ($i < $columns-1) ? $file["csv_separate"] : ''; 
    } 
    $data_rows .= $this->csv_end_row; // add data row to CSV file 
    } 
    $csv_file .= $data_rows; // add the data rows to CSV file 

    if ($this->debugFlag) { 
    echo "Step 4 (repeats for each attachment): CSV file built. \n\n"; 
    } 
} else { 
    echo "Step 3 (repeats for each attachment): MySQL query ran successfully \n\n"; 
    echo "Step 4 (repeats for each attachment): NO results were returned for this query \n\n"; 
} 

// Return the completed file 
return $csv_file; 

}

+0

このソリューションあまりにも混乱。 whileループを実行し、連想配列キーから最初の反復でヘッダーを埋める –

関連する問題