Re: I'm new at PHP and MySQL and have a problem
| From: | Torsten Roehr | Date: | Tue, 11 May 2004 19:24:29 +0000 |
| Subject: | Re: I'm new at PHP and MySQL and have a problem | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-34369@lists.php.net to get a copy of this message | ||
"News.Php.Net" <Adam.Freden@cpsa-rbha.org> wrote in message
news:20040511191338.97262.qmail@pb1.pair.com...
> To help me learn using these awesome tools together, I'm creating a sample
> application for myself - a movie database. In this database (which is
named
> "movies"), I have five tables. One named "moviemain", one named
> "actors",
> one named "genre", and then two tables that are designed to link these
> tables together "moviegenre" and "movieactor". I've created a MySQL
> query
> to get all records, and it works great if I use it directly with MySQL,
> however every time I try it with PHP it gives me a "Resource id #2"
message.
> It seems to have something to do with creating a user connection twice, or
> something liek that, but I'm not sure. Here's the simple code snippet:
>
> <?php
> $dbcxn = @mysql_connect('localhost','root','');
> if (!$dbcxn) {
> echo('<p>Unable to connect to the database at this time</p>');
> exit();
> }
> mysql_select_db('movies',$dbcxn);
> if (! @mysql_select_db('movies') ) {
> die('<p>Unable to locate the Movie database within the database
> server</p>');
> }
> $result = @mysql_query("SELECT * from
> MovieMain,movieactor,moviegenre,genre,actors where
> MovieMain.movieid=movieactor.movieid and
> MovieMain.movieid=moviegenre.movieid and genre.genreid=moviegenre.genreid
> and actors.actorid=movieactor.actorid");
> if (!$result) {
> die('<p>Error performing query: ' . mysql_error() . '</p>');
> }
> echo $result;
> ?>
$result is a MySQL result resource handle, you cannot just echo it out. You
have to use a loop to iterate through every row of the result. Try this:
while ($row = mysql_fetch_assoc($result)) {
// now you have an array with all field values of the current record, do
a print_r($row) to see how the array is structured
}
See here:
http://de.php.net/mysql_fetch_assoc
Regards, Torsten