Hi I need some help regarding Basic MySQL/PHP
07-28-2009, 09:32 PM
|
Hi I need some help regarding Basic MySQL/PHP
|
Posts: 41
Name: Crystal
Location: Beverly Hills
|
Hi I need help regarding basic MySQL
I am doing this all by reading a programming book and following the directions but I think the book has some error and I can't figure it out, can someone help me.
So basically, when I open the file table1.php this is what I get as the output.
Unknown column 'movietype' in 'field list'
I'm still a beginner so if someone could help me figure this out It'd be appreciated.
This is my code for the table1.php:
Code:
<?php
//connect to MySQL
$db = mysql_connect('localhost', 'root', '') or
die('Unable to connect. Check your connection parameters.');
// make sure you're using the right database
mysql_select_db('moviesite', $db) or die(mysql_error($db));
// retrieve information
$query = 'SELECT
movie_name, movie_year, movie_director, movie_leadactor, movietype
FROM
movie
ORDER BY
movie_name ASC,
movie_year DESC';
$result = mysql_query($query, $db) or die(mysql_error($db));
// determine number of rows in returned result
$num_movies = mysql_num_rows($result);
?>
<div style="text-align: center;">
<h2>Movie Review Database</h2>
<table border="1" cellpadding="2" cellspacing="2"
style="width: 70%; margin-left: auto; margin-right: auto;">
<tr>
<th>Movie Title</th>
<th>Year of Release</th>
<th>Movie Director</th>
<th>Movie Lead Actor</th>
<th>Movie Type</th>
</tr>
<?php
// loop through the results
while ($row = mysql_fetch_assoc($result)) {
extract($row);
echo '<tr>';
echo '<td>' . $movie_name . '</td>';
echo '<td>' . $movie_year . '</td>';
echo '<td>' . $movie_director . '</td>';
echo '<td>' . $movie_leadactor . '</td>';
echo '<td>' . $movie_type . '</td>';
echo '</tr>';
}
?>
</table>
<p><?php echo $num_movies; ?> Movies</p>
</div>
This is my code for the Database db_ch03-1.php
Code:
<?php
//connect to MySQL
$db = mysql_connect('localhost', 'root', '') or
die ('Unable to connect. Check your connection parameters.');
//create the main database if it doesn't already exist
$query = 'CREATE DATABASE IF NOT EXISTS moviesite';
mysql_query($query, $db) or die(mysql_error($db));
//make sure our recently created database is the active one
mysql_select_db('moviesite', $db) or die(mysql_error($db));
//create the movie table
$query = 'CREATE TABLE movie (
movie_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
movie_name VARCHAR(255) NOT NULL,
movie_type TINYINT NOT NULL DEFAULT 0,
movie_year SMALLINT UNSIGNED NOT NULL DEFAULT 0,
movie_leadactor INTEGER UNSIGNED NOT NULL DEFAULT 0,
movie_director INTEGER UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (movie_id),
KEY movie_type (movie_type, movie_year)
)
ENGINE=MyISAM';
mysql_query($query, $db) or die (mysql_error($db));
//create the movietype table
$query = 'CREATE TABLE movietype (
movietype_id TINYINT NOT NULL AUTO_INCREMENT,
movietype_label VARCHAR(100) NOT NULL,
PRIMARY KEY (movietype_id)
)
ENGINE=MyISAM';
mysql_query($query, $db) or die(mysql_error($db));
//create the people table
$query = 'CREATE TABLE people (
people_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
people_fullname VARCHAR(255) NOT NULL,
people_isactor TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
people_isdirector TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (people_id)
)
ENGINE=MyISAM';
mysql_query($query, $db) or die(mysql_error($db));
echo 'Movie database successfully created!';
?>
|
|
|
|
07-29-2009, 01:08 AM
|
Re: Hi I need some help regarding Basic MySQL/PHP
|
Posts: 132
Name: Leonard
Location: Minnesota, USA
|
I'm certainly no expert, but did you try changing movietype to movie_type in this line?
Code:
movie_name, movie_year, movie_director, movie_leadactor, movietype
To match this
Code:
movie_type TINYINT NOT NULL DEFAULT 0,
|
|
|
|
07-29-2009, 03:23 PM
|
Re: Hi I need some help regarding Basic MySQL/PHP
|
Posts: 41
Name: Crystal
Location: Beverly Hills
|
Wow it worked, thank you very much.
|
|
|
|
07-29-2009, 04:30 PM
|
Re: Hi I need some help regarding Basic MySQL/PHP
|
Posts: 41
Name: Crystal
Location: Beverly Hills
|
Hi Geek I ran into another stump, can you help me out again. I can't figure this one out tried to do it for hours.
This is my table2.php code
Code:
<?php
//take in the id of a director and return his/her full name
function get_director($director_id) {
global $db;
$query = 'SELECT
people_fullname
FROM
people
WHERE
people_id = ' . $director_id;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $people_fullname;
}
//take in the id of a lead actor and return his/her full name
function get_leadactor($leadactor_id) {
global $db;
$query = 'SELECT
people_fullname
FROM
people
WHERE
people_id = ' . $leadactor_id;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $people_fullname;
}
//take in the id of a movie type and return the meaningful textual
//description
function get_movietype($type_id) {
global $db;
$query = 'SELECT
movietype_label
FROM
movietype
WHERE
movietype_id ' . $type_id;
$result = mysql_query($query, $db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $movietype_label;
}
//connect to MySQL
$db = mysql_connect('localhost', 'root', '') or
die('Unable to connect. Check your connection parameters.');
//make sure you're using the right database
mysql_select_db('moviesite', $db) or die(mysql_error($db));
//retrieve information
$query = 'SELECT
movie_name, movie_year, movie_director, movie_leadactor, movie_type
FROM
movie
ORDER BY
movie_name ASC,
movie_year DESC';
$result = mysql_query($query, $db) or die(mysql_error($db));
//determine number of rows in returned result
$num_movies = mysql_num_rows($result);
$table = <<<ENDHTML
<div style="text-align: center;">
<h2>Movie Review Database</h2>
<table border="1" cellpadding="2" cellspacing="2"
style="width: 70%; margin-left: auto; margin-right: auto;">
<tr>
<th>Movie Title</th>
<th>Year of Release</th>
<th>Movie Director</th>
<th>Movie Lead Actor</th>
<th>Movie Type</th>
</tr>
ENDHTML;
//loop through the results
while ($row = mysql_fetch_assoc($result)) {
extract($row);
$director = get_director($movie_director);
$leadactor = get_leadactor($movie_leadactor);
$movietype = get_movietype($movie_type);
$table .= <<<ENDHTML
<tr>
<td>$movie_name</td>
<td>$movie_year</td>
<td>$director</td>
<td>$leadactor</td>
<td>$movietype</td>
</tr>
ENDHTML;
}
$table .= <<<ENDHTML
</table>
<p>$num_movies Movies</p>
</div>
ENDHTML;
echo $table;
?>
This is my code for the Database db_ch03-1.php
Code:
<?php
//connect to MySQL
$db = mysql_connect('localhost', 'root', '') or
die ('Unable to connect. Check your connection parameters.');
//create the main database if it doesn't already exist
$query = 'CREATE DATABASE IF NOT EXISTS moviesite';
mysql_query($query, $db) or die(mysql_error($db));
//make sure our recently created database is the active one
mysql_select_db('moviesite', $db) or die(mysql_error($db));
//create the movie table
$query = 'CREATE TABLE movie (
movie_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
movie_name VARCHAR(255) NOT NULL,
movie_type TINYINT NOT NULL DEFAULT 0,
movie_year SMALLINT UNSIGNED NOT NULL DEFAULT 0,
movie_leadactor INTEGER UNSIGNED NOT NULL DEFAULT 0,
movie_director INTEGER UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (movie_id),
KEY movie_type (movie_type, movie_year)
)
ENGINE=MyISAM';
mysql_query($query, $db) or die (mysql_error($db));
//create the movietype table
$query = 'CREATE TABLE movietype (
movietype_id TINYINT NOT NULL AUTO_INCREMENT,
movietype_label VARCHAR(100) NOT NULL,
PRIMARY KEY (movietype_id)
)
ENGINE=MyISAM';
mysql_query($query, $db) or die(mysql_error($db));
//create the people table
$query = 'CREATE TABLE people (
people_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
people_fullname VARCHAR(255) NOT NULL,
people_isactor TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
people_isdirector TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (people_id)
)
ENGINE=MyISAM';
mysql_query($query, $db) or die(mysql_error($db));
echo 'Movie database successfully created!';
?>
|
|
|
|
07-29-2009, 05:00 PM
|
Re: Hi I need some help regarding Basic MySQL/PHP
|
Posts: 41
Name: Crystal
Location: Beverly Hills
|
Oh I forgot to tell you what error I get. The error I get is:
Table 'moviesite.people' doesn't exist
|
|
|
|
08-06-2009, 08:12 PM
|
Re: Hi I need some help regarding Basic MySQL/PHP
|
Posts: 41
Name: Crystal
Location: Beverly Hills
|
Can anyone still help me. It's been a couple days and I still cannot figure this out.
|
|
|
|
08-07-2009, 07:08 AM
|
Re: Hi I need some help regarding Basic MySQL/PHP
|
Posts: 744
Name: Mattias Nordahl
Location: Sweden
|
It seems the people table doesn't exist. Are you sure it has been successfully created?
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
|
|
|
|
08-10-2009, 08:13 PM
|
Re: Hi I need some help regarding Basic MySQL/PHP
|
Posts: 41
Name: Crystal
Location: Beverly Hills
|
Yes it has been created. But how can I double check?
|
|
|
|
08-11-2009, 06:40 AM
|
Re: Hi I need some help regarding Basic MySQL/PHP
|
Posts: 744
Name: Mattias Nordahl
Location: Sweden
|
Please show a bit of code or the query being run when the error occurs.
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
|
|
|
|
08-30-2009, 12:07 PM
|
Re: Hi I need some help regarding Basic MySQL/PHP
|
Posts: 41
Name: Crystal
Location: Beverly Hills
|
Table 'moviesite.people' doesn't exist
This is the error I get when I try to open it.
Thank you
|
|
|
|
08-30-2009, 06:32 PM
|
Re: Hi I need some help regarding Basic MySQL/PHP
|
Posts: 744
Name: Mattias Nordahl
Location: Sweden
|
I'm sorry but I can't see what's wrong. Since MySQL is saying the table doesn't exist, it probably doesn't. Either it isn't created at all, or you've made some spelling mistake or similar (check for big and small letters) so that MySQL is looking for the wrong table.
I did find another error though, but it will not fix this problem. In the function get_movietype() the line
Code:
movietype_id ' . $type_id;
should be
Code:
movietype_id = ' . $type_id;
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
|
|
|
|
|
« Reply to Hi I need some help regarding Basic MySQL/PHP
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|