简体   繁体   English

PHP和SQLSRV的输入和输出格式

[英]Input and output format with PHP and SQLSRV

I have a fairly specific scenario that I'm struggling with. 我有一个非常具体的情况,我正在努力。 The frustration is that it shouldn't even be complicated! 令人沮丧的是,它甚至不应该变得复杂!

I have a SQL database and I'm connecting to it using PHP + SQLSRV. 我有一个SQL数据库,并且正在使用PHP + SQLSRV连接到它。 The database structure is quite straight forward and currently has 2 varchar columns (ColumnA and ColumnB for this example). 数据库结构非常简单,目前有2个varchar列(此示例为ColumnA和ColumnB)。

My task is to read the data from this table, allow the values to be edited in a form and write back to the database. 我的任务是从该表中读取数据,允许以表格形式编辑值并写回数据库。 This part is easy enough and is all working. 这部分很容易并且可以正常工作。

The challenge now is that the database values need to be written to my (currently empty) values.php file in the following format. 现在的挑战是,数据库值需要以以下格式写入我的(当前为空)values.php文件。

<?php
    $output = array (
        "Val1" => 'aaa',
        "Val2" => 'bbb',
        "Val3" => 'ccc',
    );
?>

Ideally, I'd like this to be done at the time of the save to database command which I can do as part of an AJAX success process so that part shouldn't be an issue 理想情况下,我希望在“保存到数据库”命令时完成此操作,我可以将其作为AJAX成功过程的一部分,这样就不会成为问题

I started working on the following process; 我开始进行以下过程;

<?php     
$sql = "SELECT ColumnA, ColumnB FROM Table";
$stmt = sqlsrv_query( $conn2, $sql );

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$test[]= array($row['ColumnA'] ." => '". $row['ColumnB']."',");
$values =  implode(PHP_EOL,$test);

}

$content = '<?php
$output = array ("'.$values.'");
?>';

file_put_contents("c:/jjj/values.php", $content);

?>

This gives me a lot of "Array to string conversion" errors and the file gets populated with; 这给了我很多“数组到字符串转换”的错误,并且文件被填充了。

<?php
$output = array ("
Array
Array
);
?>

Where am I going wrong here? 我在哪里错了?

EDIT 编辑

After the various comments below, the current code reads as; 在下面的各种注释之后,当前代码为:

<?php     
$sql = "SELECT ColumnA, ColumnB FROM Table";
$stmt = sqlsrv_query( $conn2, $sql );
$test = [];
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
    $test[] = [$row['ColumnA'] => $row['ColumnB']];
}

$content = "<?php" . PHP_EOL;
$content .= "\t" . '$output = [' . PHP_EOL;
foreach($test as $key => $value) {
    $content .= "\t\t\"{$key}\" => \"{$value}\"," . PHP_EOL;
}
$content .= "\t];" . PHP_EOL;
$content .= "?>";

file_put_contents("c:/jjj/values.php", $content);
?>

This results in 3 Array to string conversion errors (One for each row) and an output file that looks like this: 这将导致3个数组到字符串的转换错误(每行一个)和一个输出文件,如下所示:

<?php
    $output = [
        "0" => "Array",
        "1" => "Array",
        "2" => "Array",
    ];
?>

I still can't find a way to export my DB table to a file in the correct format using PHP. 我仍然找不到使用PHP将数据库表以正确格式导出到文件的方法。

I think this will come closer to what you want: To echo tabs as well you could use \\t within the double quotes 我认为这将更接近您想要的内容:要也回显制表符,您可以在双引号内使用\\ t

$content = "<?php" . PHP_EOL;
$content .= "\t" . '$output = [' . PHP_EOL;
foreach($test as $key => $value) {
    foreach ($value as $subkey => $subvalue) {
        $content .= "\t\t\"{$subkey}\" => \"{$subvalue}\"," . PHP_EOL;
    }
}
$content .= "\t];" . PHP_EOL;
$content .= "?>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM