2

我正在尝试使用 php 的 odbc 连接从异地 Miscrosoft SQL 数据库中获取数据,将针对它的某些查询转换为数组,然后将这些数组转换为我的 cms 可以读取和导入的 csv。我能够成功地连接并从数据库中返回一些结果,但是我缺乏 php 和 SQL 技能正在杀死我。

我现在拥有的东西不多,但做了它应该做的事情:

    $result = odbc_tables($connect);

    $tables = array();

    while (odbc_fetch_row($result))
    {
        if(odbc_result($result,"TABLE_TYPE")=="TABLE")
        echo"<br>".odbc_result($result,"TABLE_NAME");

    }

网络上有没有关于如何做我想做的事情的明确资源?官方 php 文档似乎是有史以来最无用的文档。一个基本示例:我想将此处的条目返回为 csv 格式。我可以以数组格式获取它们:

$query = "SELECT TOP 10 * FROM Communities"; 

    $result = odbc_exec($connect, $query);

    if ( $result )
    {
        while ( ($row = odbc_fetch_array($result)) )
        {
            print_r($row);
        }
        odbc_free_result($result);
    }
    else 
    {
        echo 'Exec error: ' . odbc_errormsg();
    }

    odbc_close($conn);

希望我有更多,但我对下一步去哪里有点迷茫。

4

2 回答 2

2

使用提示,这是有效的解决方案:

$theArray = array();

while ( ($row = odbc_fetch_array($result)) )
{
    array_push($theArray, $row);
}


$header = array('Name', 'Hours', 'Fees', 'Notes', 'ShortDescription', 'URL');

$fp = fopen('array.csv', 'w');

fputcsv($fp, $header);

foreach ($theArray as $lines) 
{

    fputcsv($fp, $lines);

}
于 2013-06-24T15:57:19.030 回答
1

我刚刚完成了您所询问的确切项目。我正在运行 php 5.2,因此您可以在较新的版本中更轻松地处理 csv 文件。这是我的代码:

<?php
// Uncomment this line for troubleshooting / if nothing displays
ini_set('display_errors', 'On');

$myServer = "GSRBI";
$myUser = "webuser";
$myPass = "Webuser1";
$myDB = "GSRBI";

$dbhandle = odbc_connect($myServer, $myUser, $myPass)
    or die("Couldn't connect to SQL Server on $myServer"); 

$return = odbc_exec($dbhandle, 'select * from GSRBI.dbo.BounceBackEmail');

$subscribers_array = array(); 
$db_row = '';
$arrayrow = 0;

while ( $db_row = odbc_fetch_array($return) )
{
    $arrayrow++;
    $array[] = array(

                        'card_num' => $db_row['PlayerAccountNumber']
                        ,'last_name' => ucfirst(strtolower($db_row['LastName']))
                        ,'first_name' => ucfirst(strtolower($db_row['FirstName']))
                        ,'email' =>  $db_row['EMailAddress']
                        ,'earned_on_date' => date('m/d/Y', strtotime('-1 days'))
                        ,'free_play' => $db_row['Offer1']
                        ,'valid_through_date' => date('m/d/Y', strtotime('+15 days'))

                    );
    }
echo print_r($arrayrow, true); ## display number of rows for sql array
echo " rows in ODBC ";

// Creates an array with GSR webteams contact info
    $array1[] = array( 

                        'card_num' => "123456789"
                        ,'last_name' => "GSRwebteam"
                        ,'first_name' => "GSRwebteam"
                        ,'email' => "webteam@something.com"
                        ,'earned_on_date' => date('m/d/Y', strtotime('-1 days'))
                        ,'free_play' => "9"
                        ,'valid_through_date' => date('m/d/Y', strtotime('+15 days'))
                    );

$result = array_merge((array)$array, (array)$array1); ## merge the two arrays together


// This will convert the array to csv format then save it
 ## Grab the first element to build the header
$arr = array_pop( $result );
$temp = array();
    foreach( $arr as $key => $data ) 
    {
        $temp[] = $key;
    }
$csv = implode( ',', $temp ) . "\n";
$csv .= to_csv_line( $arr ); ## Add the data from the first element
    foreach( $result as $arr ) ## Add the data for the rest
    {   
        $csv .= to_csv_line( $arr );
    }
//echo print_r($csv, true); ## Uncomment to test output1

$f = fopen('reports/bounceback-'.date('m-d-Y').'.csv', "w");  
fwrite($f, $csv);  
fclose($f);
Echo "The report has ran";

return $csv;
function to_csv_line( $result ) 
    {
    $temp = array();
        foreach( $result as $elt ) 
        {
            $temp[] = '' . addslashes( $elt ) . '';
        }
        $string = implode( ',', $temp ) . "\n";
    return $string;
    }
于 2014-04-01T23:41:28.397 回答