我尝试使用pdo创建sqlite数据库并在表格中回显它的各种作品,但它有一些问题,我认为你们可能知道它
这是我的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和months数组在创建该表的同时没有执行。无论如何都要让他们写在同一列
答案 0 :(得分:1)
删除插入的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