PDO+PHP+SQLITE+PRINT+'ME' = Table 结果不正确不是预期的
PDO+PHP+SQLITE+PRINT+'ME' = Table result incorrect not what expecting
我试图用 pdo 创建 sqlite 数据库并在 table 中回显它有点工作,但它有一些问题,我想你们可能知道它
这是我的 php 代码
<?php
try
{
$db = new PDO('sqlite:randvalue.sqlite');
$db->exec("CREATE TABLE company (revenue_value INTEGER PRIMARY KEY, month VARCHAR)");
$start = 2000;
$end = 2000 * 1.2;
$numbers = array($start);
for($i=0; $i<10; $i++) {
$numbers[] = rand($start, $end);
$db->exec("INSERT INTO company(revenue_value) VALUES ('2000')");
}
$numbers[] = $end;
$stmt = $db->prepare('INSERT INTO company (revenue_value) VALUES (?)');
foreach($numbers as $number) {
$stmt->execute(array($number));
}
$months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
$stmt = $db->prepare('INSERT INTO company (month) VALUES (?)');
foreach ($months as $month) {
$stmt->execute(array($month));
}
print "<table border=1>";
print "<tr><td>value</td>";
print "<td>month</td></tr>";
$result = $db->query('SELECT * FROM company');
foreach($result as $row)
{
print "<tr><td>".$row['revenue_value']."</td>";
print "<td>".$row['month']."</td></tr>";
}
print "</table>";
$db = NULL;
}
catch(PDOException $e)
{
print 'Exception : '.$e->getMessage();
}
?>
结果
我认为问题是数字 rand 和月份数组没有同时执行,因此创建了 table。有没有办法让他们写在同一栏
删除插入的exec版本:
// remove this line
$db->exec("INSERT INTO company(revenue_value) VALUES ('2000')");
使用单个插入:
try
{
$db = new PDO('sqlite:randvalue.sqlite');
$db->exec("CREATE TABLE company (revenue_value INTEGER PRIMARY KEY, month VARCHAR)");
$start = 2000;
$end = 2000 * 1.2;
$numbers = array($start);
for($i=0; $i<10; $i++) {
$numbers[] = rand($start, $end);
}
$numbers[] = $end;
$months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
$stmt = $db->prepare('INSERT INTO company (revenue_value, month) VALUES (?, ?)');
for($j = 0, $count = count($numbers); $j < $count; $j++) {
$stmt->execute(array($numbers[$j], $months[$j]));
}
// then continue on to output the table
我试图用 pdo 创建 sqlite 数据库并在 table 中回显它有点工作,但它有一些问题,我想你们可能知道它
这是我的 php 代码
<?php
try
{
$db = new PDO('sqlite:randvalue.sqlite');
$db->exec("CREATE TABLE company (revenue_value INTEGER PRIMARY KEY, month VARCHAR)");
$start = 2000;
$end = 2000 * 1.2;
$numbers = array($start);
for($i=0; $i<10; $i++) {
$numbers[] = rand($start, $end);
$db->exec("INSERT INTO company(revenue_value) VALUES ('2000')");
}
$numbers[] = $end;
$stmt = $db->prepare('INSERT INTO company (revenue_value) VALUES (?)');
foreach($numbers as $number) {
$stmt->execute(array($number));
}
$months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
$stmt = $db->prepare('INSERT INTO company (month) VALUES (?)');
foreach ($months as $month) {
$stmt->execute(array($month));
}
print "<table border=1>";
print "<tr><td>value</td>";
print "<td>month</td></tr>";
$result = $db->query('SELECT * FROM company');
foreach($result as $row)
{
print "<tr><td>".$row['revenue_value']."</td>";
print "<td>".$row['month']."</td></tr>";
}
print "</table>";
$db = NULL;
}
catch(PDOException $e)
{
print 'Exception : '.$e->getMessage();
}
?>
结果
我认为问题是数字 rand 和月份数组没有同时执行,因此创建了 table。有没有办法让他们写在同一栏
删除插入的exec版本:
// remove this line
$db->exec("INSERT INTO company(revenue_value) VALUES ('2000')");
使用单个插入:
try
{
$db = new PDO('sqlite:randvalue.sqlite');
$db->exec("CREATE TABLE company (revenue_value INTEGER PRIMARY KEY, month VARCHAR)");
$start = 2000;
$end = 2000 * 1.2;
$numbers = array($start);
for($i=0; $i<10; $i++) {
$numbers[] = rand($start, $end);
}
$numbers[] = $end;
$months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
$stmt = $db->prepare('INSERT INTO company (revenue_value, month) VALUES (?, ?)');
for($j = 0, $count = count($numbers); $j < $count; $j++) {
$stmt->execute(array($numbers[$j], $months[$j]));
}
// then continue on to output the table