I am following the book “Jump Start PHP” And it has told me to create a table using php in MySQL.
I have installed phpMyAdmin as it said and created a database called “kickstartapp” It has then told me to make sure that PDO is enabled in the MAMP local server, which I believe this says it is?

Then it tells me to put this code into a setup.php file, which is located in the local host folder:
<? php
$db = new PDO ("mysql: host=localhost;dbname=kickstartapp", "Chris", "password");
$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);
} //close try
catch (PDOException $e) {
echo $e->getMessage();
} //close catch
?>
Then it says that this should’ve created a table in the database kickstartapp, but from the screenshot (below) you can see there’s no table created in this database?

If someone could help that would be great, I’m pretty new to this php as you can see. Could it be that I have this saved in the wrong folder, or should that not make a difference? The location of setup.php is as follows:
applications > mamp > htdocs > kickstart . setup.php
Thanks in advance
you opened the http://localhost/kickstart/setup.php file in your web browser ?you got any errors after opening that page?
Ran your script and nothing happened, so modified the script because.
- there is a space between the ? and PHP
- added notification
- added result
<?php
error_reporting(-1);
ini_set('display_errors',1);
echo '<h5>Create PDO Table named users</h5>';
$db = new PDO ("mysql: host=localhost;dbname=dbTest_001", "user_john", "123456");
$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);
echo 'Just created the table: <b>users</b>' ;
} catch (PDOException $e) {
echo $e->getMessage();
} //close catch
Ran the script again:
Create PDO Table named users
SQLSTATE[42S01]: Base table or view already exists: 1050 Table ‘users’ already exists
Checked http://localhost/phpmyadmin and the users table is showing:
– Host: 127.0.0.1
– Generation Time: Apr 04, 2014 at 07:58 AM
– Server version: 5.5.32
– PHP Version: 5.4.19
SET SQL_MODE = “NO_AUTO_VALUE_ON_ZERO”;
SET time_zone = “+00:00”;
–
– Database: dbTest_001
–
– Table structure for table users
DROP TABLE IF EXISTS users
;
CREATE TABLE IF NOT EXISTS users
(
id
int(11) NOT NULL AUTO_INCREMENT,
name
varchar(40) DEFAULT NULL,
password
varchar(100) DEFAULT NULL,
email
varchar(150) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Thanks very much John.
I am new to php and I have done that twice now (space between the ? and php) I really need to be stricter when checking my code. That has fixed it
Thanks for the help!
Many thanks for letting us know the problem is resolved.
When developing, at the top of every file I have the following:
<php echo __FILE__; die; // just tests to ensure filename and path is correct
error_reporting(-1);
ini_set('display_errors',1);
The best online PHP information: Google PHP Manual function()
Give it a whirl, usually there are numerous helpful examples and comments.