samedi 21 février 2015

PDO+PHP+SQLITE+PRINT+'ME' = WEIRD LOOKING TABLE (something is wrong)

I trying to create sqlite database with pdo and echo it out in table it sorts of works but it has some issues and i thought you guys may know it


This is my php code



<?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();
}

?>


and the result


What it displays


I think the issue is that the number rand and the months array are not executing at the same time there fore creating that table. Is there anyway to make them write in the same columns


Aucun commentaire:

Enregistrer un commentaire