How do I convert this script to PHP PDO

How do I convert this script to PDO?

$conn = new PDO("mysql:host=$host;dbname=$db;charset=$charset", $user, $pass);

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$name= "Backorders";

$query6 = $conn->query('SELECT orderid, qty, sku, model, description, days FROM backorders');

$export = mysql_query($select);   

$fields = mysql_num_fields($export); 

for ($i = 0; $i < $fields; $i++) { 
    $header .= mysql_field_name($export, $i) . "\t"; 
}

while($row = mysql_fetch_row($export)) { 
    $line = ''; 
    foreach($row as $value) {                                             
        if ((!isset($value)) OR ($value == "")) { 
            $value = "\t"; 
        } else { 
            $value = str_replace('"', '""', $value); 
            $value = '"' . $value . '"' . "\t"; 
        } 
        $line .= $value; 
    } 
    $data .= trim($line)."\n"; 
} 
$data = str_replace("\r","",$data);

if ($data == "") { 
    $data = "\n(0) Records Found!\n";                         
}

header("Content-type: application/x-msdownload"); 
header("Content-Disposition: attachment; filename=$name.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
print "$header\n$data";

Open in new window

lawrence_devAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
This is an amazingly common question, so E-E has an article about it.  The article maps the familiar but obsolete MySQL extension to the current MySQLi and PDO extensions.   MySQLi is the easier conversion -- with PDO you must change the actual query strings, and that means a whole new set of tests.  Sorry it's not easier, but nobody has been able to automate this, partly because of the way MySQL was so badly implemented in PHP almost 20 years ago.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

As written, I don't think the script posted above can run at all.  It relies on undefined variables.  But it looks like it might be gathering data from a MySQL table and creating a CSV file (these are almost universally associated with Excel).  If that's the case I can show you a general design.  PHP has a lot of built-in functionality to make your work easier on tasks like this.
0
lawrence_devAuthor Commented:
This script works perfectly, and is PDO based.  However, I cannot get the Excel file to generate,  Please advise how to create excel file.

// Time limit to 0 for exporting big records.
    set_time_limit(0);
    // mysql hostname
    $hostname = 'localhost';
    // mysql username
    $username = $user;
    // mysql password
    $password = $pass;
    // Database Connection using PDO with try catch method.
    try { $dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password); }
    // In case of error PDO exception will show error message.
    catch(PDOException $e) { echo $e->getMessage(); }
    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment;Filename=Backorders.xls");
    echo "<html>";
    echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
    echo "<body>";
    echo "<table border=1>";
	echo"<tr><th>Order ID</th><th>Order Date</th><th>Qty</th><th>Model</th><th>Description</th><th>Days</th></tr>";
    // We will assign variable here for entry By. you can use your variables here.
    $EntryBy = $_GET[val];
    // Get data using PDO prepare Query.
    $STM2 = $dbh->prepare("SELECT `orderid`, `orderdate`, `qty`, `model`, `description`, `days` FROM backorders");
    // bind paramenters, Named paramenters alaways start with colon(:)
    $STM2->bindParam(':EntryBy', $EntryBy);
    // For Executing prepared statement we will use below function
    $STM2->execute();
    // We will fetch records like this and use foreach loop to show multiple Results later in bottom of the page.
    $STMrecords = $STM2->fetchAll();
    // We use foreach loop here to echo records.
    foreach($STMrecords as $r)
    {
    echo "<tr>";
    echo "<td>" .$r[0] ."</td>";
    echo "<td>" .$r[1] ."</td>";
    echo "<td>" .$r[2] ."</td>";
    echo "<td>" .$r[3] ."</td>";
    echo "<td>" .$r[4] ."</td>";
    echo "<td>" .$r[5] ."</td>";
    echo "</tr>";
    }
    echo "</table>";
    echo "</body>";
    echo "</html>";
    // Closing MySQL database connection
    $dbh = null;

Open in new window

0
lawrence_devAuthor Commented:
Disregard.  Works perfect.  Needed to post this script on a second php document because of headers.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lawrence_devAuthor Commented:
I found the solution and made it work
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.