Jump start PHP - Chapter 2 issues

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.

The error you get from phpMyAdmin is because the syntax of the query is wrong: “SELECT * FROM WHERE 1” doesn’t specify a table to select all columns from. If this is in the main admin page, is it possible your WAMP installation (assuming that’s what you have) has a problem?

For the other issues, when you say the data doesn’t appear as it should, what happens exactly? Anything, or do you see an error message?

Looking in ‘insert.php’ I think you have a syntax error in the query - you have a second “INSERT” keyword, I think that line should read:


$query = "INSERT INTO users(username, password, email) VALUES('admin', MD5('admin'), 'youremail@domain.com')";

I’m not 100% sure this is wrong, you might have a column called INSERTusername, but it looks like a typo.

Hi,

First of all, thanks for answering.

At this moment, when I run the insert.php in the browser, this appears:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'username' in 'field list'

I’m with MAMP, not WAMP because I use it on Mac, do you think that’s the reason why it’s not running the SQL?

Best regards

No, I doubt it would be that. The error message gives you the information, though:

If you have a look in your setup.php, though, you create the table with four coulmns - id, name, password, email. But in your insert.php, you try to use a column called ‘username’, so you get that error message. You need to either call the column ‘username’ when you create the table in setup.php, or refer to it as ‘name’ in insert.php.

I see one issue with your insert.php, you code appears to be incorrect. Line five reads
"$query = “INSERT INTO users(INSERTusername, password, email) VALUES(‘admin’, MD5(‘admin’), ‘youremail@domain.com’ )”;

it should read
$query = “INSERT INTO users(name, password, email) VALUES(‘admin’, MD5(‘admin’), ‘youremail@domain.com’ )”;

Try this. I have run the script and it works for me.