I’m getting error “Not unique table/alias” while trying to display data from MySQL database to table. Assume it has something to do with foreign keys.
Here is SQL for creating tables, afterwords inserting some values. SQL went fine, no problems occurred, but I added the code, since it all go hand-in-hand with getting table on screen.
CREATE TABLE city (
id int not null primary key auto_increment,
name varchar(30) not null
) type=innodb;
CREATE TABLE cinema (
id int not null primary key auto_increment,
name varchar(50) not null,
city int not null,
foreign key(city) references city(id)
) type=innodb;
CREATE TABLE movie (
id int not null primary key auto_increment,
name varchar(30) not null
) type=innodb;
CREATE TABLE relationship (
whattime time NOT NULL,
whichdate date NOT NULL,
movieid int not null,
cinemaid int not null,
primary key (cinemaid, movieid),
foreign key(movieid) references movie(id),
foreign key(cinemaid) references cinema(id)
) type=innodb;
INSERT INTO city (id, name) VALUES (1, 'Paris'), (2, 'Copenhagen'), (3, 'London'), (4, 'Lisbon')
INSERT INTO movie (id, name) VALUES (1, 'The Church')
INSERT INTO cinema (id, name, city) VALUES (1, 'Pathé', 1), (2, 'Cinemaxx', 2), (3, 'Cineworld', 3), (4, 'ZON Lusomundo', 4)
INSERT INTO relationship (whattime, whichdate, movieid, cinemaid) VALUES ('21:00:00', '2011-12-27', 1, 1), ('19:30:00', '2011-12-28', 1, 2), ('20:00:00', '2011-12-27', 1, 3), ('21:00:00', '2012-01-02', 1, 4)
Here is php, which gives me Not unique table/alias: ‘cinema’ error. Any ideas why it’s so?
<?php
include "inc/mysql_con.php";
mysql_select_db($db) or die(mysql_error());
$query = "select city.name, cinema.name, movie.name, date, time from city, cinema, relationship, movie";
$query .= "where cinema.city = city.id";
$query .= "and cinemaid = cinema.id";
$query .= "and movieid = movie.name";
$query .= "order by date";
mysql_query($query) or die(mysql_error());
echo "<table id='premiere'>";
echo "<tr> <th>CITY</th> <th>CINEMA</th> <th>DATE</th> <th>TIME</th></tr>";
while($result = mysql_fetch_array( $query )) {
echo "<tr><td>";
echo $result['city.name'];
echo "</td><td>";
echo $result['cinema.name'];
echo "</td><td>";
echo $result['date'];
echo "</td><td>";
echo $result['time'];
echo "</td></tr>";
}
echo "</table>";
?>
UPDATE:
Now I got sql query right, code below
SELECT city.name, cinema.name, whichdate, whattime
FROM city, cinema, relationship, movie
WHERE cinema.city = city.id
AND cinemaid = cinema.id
ORDER BY whichdate
It displays table fine in SQL, but not in PHP:
ERROR: Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in content.php on line 75
line 75: while($query = mysql_fetch_array( $result )) {
Help will be appreciated.
It looks like your string concatenation misses spaces so the first two lines would give … cinema, relationship, moviewhere cinema.city = city.id. In that case sql reads twice cinema … Could it be as simple as this ? I would echo the query before sending it.