Trying to create database in MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Carlo Gambino
Forum Newbie
Posts: 9
Joined: Mon Jul 21, 2008 10:40 am

Trying to create database in MySQL

Post by Carlo Gambino »

Hello again,

I have been trying to grasp the concepts involved in creating databases in MySQL with PHP. I have a written a script to create a database containing 2 tables. I know I'm new, but it looks like I'm doing everything correctly:

Code: Select all

<?php
//Connect to MySQL
$connect = mysql_connect("localhost", "root", "MyPaSsWoRd") or
    die ("Access Denied- http://www.Digital-Syndicate.org")
 
//create main database. 
mysql_create_db("wiley")
    or die(mysql_error());
 
//make sure our recently created database is the active one
mysql_select_db ("wiley");
 
//create "movie" table
$movie = "CREATE TABLE movie (
    movie_id int(11) NOT NULL auto_increment,
    movie_name varchar(255) NOT NULL,
    movie_type tinyint(2) NOT NULL default 0,
    movie_year int(4) NOT NULL default 0,
    movie_leadactor int(11) NOT NULL default 0,
    movie_director int(11) NOT NULL default 0,
    PRIMARY KEY (movie_id),
    KEY movie_type (movie_type,movie_year)
    ) TYPE=MyISAM AUTO_INCREMENT=4 ";
 
$results = mysql_query($movietype)
    or die(mysql_error());
 
//create "people" table
$people = "CREATE TABLE people (
    people_id int(11) NOT NULL auto_increment,
    people_fullname varchar(255) NOT NULL,
    people_isactor tinyint(1) NOT NULL default 0,
    people_isdirector tinyint(1) NOT NULL default 0,
    PRIMARY KEY (people_id)
) TYPE=MyISAM AUTO_INCREMENT=7";
 
$results = mysql_query($people)
    or die(mysql_error());
 
echo "Movie Database Successfully Created!";
 
?>
The problem is that upon calling the page in my browser, I continually get the following error:

Code: Select all

Parse error: syntax error, unexpected T_STRING in /var/www/createmovie.php on line 7
Please help me understand what I'm missing!
Last edited by Carlo Gambino on Thu Sep 11, 2008 4:55 pm, edited 1 time in total.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Trying to create database in MySQL

Post by jayshields »

Missing semi-colon after first die() call. Your IDE should've let you know about this.
Carlo Gambino
Forum Newbie
Posts: 9
Joined: Mon Jul 21, 2008 10:40 am

Re: Trying to create database in MySQL

Post by Carlo Gambino »

Perhaps that is one problem, I continue to find people with conflicting oppinions of using an IDE. I have been using notepad++ but it didn't catch this. perhaps I will reconsider...

Now that I feel like a simpleton, I run the code again and get:

Code: Select all

Parse error: syntax error, unexpected T_LOGICAL_OR in /var/www/createmovie.php on line 8
Any suggestions on an IDE that might help me see these oversights?

Code: Select all

<?php
//Connect to MySQL
$connect = mysql_connect("localhost", "root", "EDU123") or
    die ("Access Denied- http://www.Digital-Syndicate.org");
 
//create main database. 
mysql_create_db("wiley");
    or die(mysql_error());
 
//make sure our recently created database is the active one
mysql_select_db ("wiley");
 
//create "movie" table
$movie = "CREATE TABLE movie (
    movie_id int(11) NOT NULL auto_increment,
    movie_name varchar(255) NOT NULL,
    movie_type tinyint(2) NOT NULL default 0,
    movie_year int(4) NOT NULL default 0,
    movie_leadactor int(11) NOT NULL default 0,
    movie_director int(11) NOT NULL default 0,
    PRIMARY KEY (movie_id),
    KEY movie_type (movie_type,movie_year)
    ) TYPE=MyISAM AUTO_INCREMENT=4 ";
 
$results = mysql_query($movietype)
    or die(mysql_error());
 
//create "people" table
$people = "CREATE TABLE people (
    people_id int(11) NOT NULL auto_increment,
    people_fullname varchar(255) NOT NULL,
    people_isactor tinyint(1) NOT NULL default 0,
    people_isdirector tinyint(1) NOT NULL default 0,
    PRIMARY KEY (people_id)
) TYPE=MyISAM AUTO_INCREMENT=7";
 
$results = mysql_query($people)
    or die(mysql_error());
 
echo "Movie Database Successfully Created!";
 
?>
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Trying to create database in MySQL

Post by jayshields »

If there's an expected anything on line 8, start by looking at line 7. Immediately you can see you ended line 7 with a semi-colon and in line 8 went on to add an OR. You need to remove the semi-colon from line 7. Being honest, my editor doesn't show things like this either, but IDE's that do would be Eclipse's PHP Development IDE and NeatBeans with the PHP plugin - clunky IDE's!
Carlo Gambino
Forum Newbie
Posts: 9
Joined: Mon Jul 21, 2008 10:40 am

Re: Trying to create database in MySQL

Post by Carlo Gambino »

I just don't understand- it is so difficult to learn this stuff without any formal guidance sometimes!

I had originally had the syntax correct, but I added when I ran the code as you suggest:

Code: Select all

<?php
//Connect to MySQL
$connect = mysql_connect("localhost", "root", "EDU123") or
    die ("Access Denied- http://www.Digital-Syndicate.org");
 
//create main database. 
mysql_create_db("wiley")
    or die(mysql_error());
 
//make sure our recently created database is the active one
mysql_select_db ("wiley");
Then I get the error:

Code: Select all

Fatal error: Call to undefined function mysql_create_db() in /var/www/createmovie.php on line 7
I would prefer to get used to coding without the IDE, I know it's harder but I truly believe that programming manually is better practice overall. I just wish the syntax would be the same for each language and program.. I'm so confused between PHP, python, javascript, and the specifics in between that I'm having a hard time keeping them straight.

Thanks for your input, it's really helpful! I'll continue pushing myself until I get it.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Trying to create database in MySQL

Post by Stryks »

As per the manual page for mysql_create_db() ( found HERE ), this is the recommended workaround to using this depreciated function.

Code: Select all

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
 
$sql = 'CREATE DATABASE my_db';
if (mysql_query($sql, $link)) {
    echo "Database my_db created successfully\n";
} else {
    echo 'Error creating database: ' . mysql_error() . "\n";
}
Hope that helps.
Post Reply