0

So the admin has the choice to choose what he want to export to excel by selecting checkboxes which i stored in col[]... here's my code for exporting

 session_start();
$HOST = 'localhost';
$USERNAME = 'root';
$PASSWORD = '';
$DB = 'fyp_db';

$link = mysqli_connect($HOST, $USERNAME, $PASSWORD, $DB);

if (is_array($_POST['col'])) {
    $sql = "SELECT ";
    foreach ($_POST['col'] AS $value) {
        $sql .= "{$value}, ";
    }
    $sql = substr($sql, 0, -2);
    $sql .= " FROM account, coursedetail, coursecategory";  
    /*echo "sql= " . $sql . "<br /><br />\n";*/
} else {
    echo "No column was selected<br /><br />\n";
}

function cleanData(&$str) { $str = preg_replace("/\t/", "\\t", $str); $str = preg_replace("/\r?\n/", "\\n", $str); if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; }

$filename = "website_data.xls"; 
header("Content-Type: text/plain"); 
$flag = false; 
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
while(false !== ($row = mysql_fetch_assoc($result))) { 
    if(!$flag) { 
    // display field/column names as first row 
    echo implode("\t", array_keys($row)) . "\r\n"; 
    $flag = true; 
        } 
        array_walk($row, 'cleanData'); 
        echo implode("\t", array_values($row)) . "\r\n"; 

        }

I got the error of..

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given in C:\xampp\htdocs\project\export_successful.php on line 28

why? :(

user861896
  • 1
  • 1
  • 1
  • Maybe your SQL is invalid. Please post your SQL query with echo(). Also you don't use mysql_real_escape_string() or something like that! – ComFreek Jul 25 '11 at 15:56
  • When I echo $sql; i see this sql= SELECT name, contact_number, address, date_of_birth, email, title, category FROM account, coursedetail, coursecategory – user861896 Jul 27 '11 at 03:23
  • The SQL is valid but I think the problem is the mysql and mysqli as Marc B said. – ComFreek Jul 27 '11 at 16:01

1 Answers1

2

You're mixing up mysqli and mysql calls. The two libraries are NOT compatible and handles/statements returned by one cannot be used in the other.

$result = mysqli_query($link, $sql) or die(mysqli_error($link));
               ^--- note the 'i'
while(false !== ($row = mysql_fetch_assoc($result))) { 
                             ^--- note the LACK of an 'i'
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • When I add the i, it did work and manage to extract data but it says WARNING: Cannot Modify header Information - headers already sent by (output started at C:\xampp\htdocs\project\export_successful.php:17) in C:xampp\htdocs\project\export_successful.php on line 26 name Joe – user861896 Jul 27 '11 at 09:31
  • You cannot have ANY output before a header() call, or you get that error. – Marc B Jul 27 '11 at 14:34