Hello Sitepoint forum,
I’m new here! I bought the book Jump start PHP and start learning php/mysql from it. At this moment, I’m having trouble on passing the chapter2 on making setup.php, getdata.php, insert.php. I write the code and after I put on browser, the data doesn’t appear like it should (says the book). I even download the files from the Sitepoint website and they are diferent from the book.
Also, when I go to Phpmyadmin and appear this error
Error
SQL query: Documentation
SELECT * FROM WHERE 1
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE 1' at line 1
Can you check if I’m doing anything wrong, I’m really stuck at this moment.
setup.php
<?php
$db = new PDO("mysql:host=localhost;dbname=kickstartapp", "teste", "teste");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$queryStr = "CREATE TABLE users (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(40), password VARCHAR(100), email VARCHAR(150))";
$db->query($queryStr);
} catch (PDOException $e) {
echo $e->getMessage();
}
getdata.php
<?php
$db = new PDO("mysql:host=localhost;dbname=kickstartapp", "teste", "teste");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$query = $db->prepare("SELECT * FROM users");
$query->execute();
for($i=0; $row = $query->fetch(); $i++){
echo $row['id'].' - '.$row['name'].' - '.$row['email'].' - '.$row['password'];
echo "<br/>";
}
}catch (PDOException $e) {
echo $e->getMessage();
}
$query->closeCursor();
$db = null;
insert.php
<?php
$db = new PDO("mysql:host=localhost;dbname=kickstartapp", "teste", "teste");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$query = "INSERT INTO users(INSERTusername, password, email) VALUES('admin', MD5('admin'), 'youremail@domain.com' )";
$result = $db->query($query);
}catch (PDOException $e) {
echo $e->getMessage();
}
I would appreciate any help on this topic.