0

My name is Anna and I'm building a website with a festival calendar! People can add festivals when they're logged in. But now is my question, how can I display what people put in my database. I found the following code on this website, but when I put it in my script, I get the error that there's no database selected. But I do have a database selected (Festivals) ?

            $sql = "SELECT festival_name, festival_organisator, festival_begin FROM Festivals WHERE festival_id=3";
            $result = mysql_query($sql) or die(mysql_error());

            $festival_name = $festival_organisator = $festival_begin = array();

            while($row = mysql_fetch_assoc($result)) {
                $festival_name[] = $row['festival_name'];
                $festival_organisator[] = $row['festival_organisator'];
                $festival_begin[] = $row['festival_begin'];
            }

I hope anybody can help me! Thanks in advance

7
  • 2
    Please, stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO. Commented Jan 20, 2015 at 14:40
  • How are you inserting the festivals? The code for selecting them should not be that different so there is no need to copy things you don't know from other sites. Commented Jan 20, 2015 at 14:43
  • It's a school assignment, so we're not allowed to copy the data, your user's must add the information Commented Jan 20, 2015 at 14:48
  • 1
    yeap, use mysqli or pdo... but you forgot to connect to your database... If you HAVE to use mysql -> 1) SMACK your teacher 2)then use mysql_connect before querying Commented Jan 20, 2015 at 14:49
  • There's a difference between database and table. You're selecting from a table called Festivals, but in your connect statement, you select the database. Can you show us how you connect to the database? And please, please don't user mysql_... Commented Jan 20, 2015 at 14:51

1 Answer 1

1

You have not yet connected to the database itself

Query selecting from the table Festivals does not mean that you've selected the Festivals database, those are two very different things.

  • The database Festivals contains all the tables (table Festivals, table x, etc).
  • The table Festivals contains data which you can query (festival_name, festival_organisator, etc).

Now you must connect

Taking a look at the documentation is always a good choice, let's see how you can connect to and select a database:

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
?>

You should be connected to the database, having access to the selected database through $link.

mysql_query($sql, $link)

But don't do it!

Unless you must, but cute puppies will die :(

You shouldn't be using mysql_* in the first place though since it's no longer supported. Instead you should be using PDO or mysqli to achieve your task. It may sound or appear trickier at first but it's actually rather simple:

Sign up to request clarification or add additional context in comments.

4 Comments

I believe this is the key line: $db_selected = mysql_select_db('foo', $link);
@SimonShirley Yes but you need to use mysql_connect to get $link in order to connect, and $link is used for querying even though the database has been selected.
hehe we won't want the puppies to die! I'm going to try pdo, thank you very much (:
@AnnaTol You're welcome! :) Feel free to upvote my answer if you have the privileges to do so and mark it as the accepted one by ticking the check mark, if you're satisfied :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.