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

Anna Tol
  • 145
  • 1
  • 1
  • 9
  • 2
    **Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).** They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). **Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement)** instead, and **use [PDO](http://us1.php.net/pdo).** – Jay Blanchard Jan 20 '15 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. – jeroen Jan 20 '15 at 14:43
  • It's a school assignment, so we're not allowed to copy the data, your user's must add the information – Anna Tol Jan 20 '15 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 – Alexis Peters Jan 20 '15 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_... – Neville Kuyt Jan 20 '15 at 14:51
  • Naville K: Like this: $servername = "localhost"; $username = ""; $password = ""; $dbname = "mysql"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } – Anna Tol Jan 20 '15 at 15:42
  • But thank you all, I'm going to try it with pdo! – Anna Tol Jan 20 '15 at 15:44

1 Answers1

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:

Community
  • 1
  • 1
Jonast92
  • 4,964
  • 1
  • 18
  • 32
  • I believe this is the key line: `$db_selected = mysql_select_db('foo', $link);` – Simon Shirley Jan 20 '15 at 14:55
  • 1
    @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. – Jonast92 Jan 20 '15 at 14:57
  • hehe we won't want the puppies to die! I'm going to try pdo, thank you very much (: – Anna Tol Jan 20 '15 at 15:44
  • @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 :) – Jonast92 Jan 20 '15 at 15:46