-2

Hello there im new in php, im trying to make a news plugin on my website. But i have following error which i cant get fixed.

            $query = "SELECT `id` , `headline`, `timestamp` FROM `news` ORDER BY `timestamp` DESC";
        $result = @mysql_query($query);
        if(!$result){
           echo('Error selecting news: ' . mysql_error());
           exit();
        }
        if (mysql_num_rows($result) > 0)
        {
            while($row = mysql_fetch_object($result)) 
            {
            ?>
            <font size="-1"><b><? echo $row[headling]; ?></b> <i><? echo formatDate($row[timestamp]); ?></i></font><?
            }
        }
        mysql_close($conn);
        ?>

Im geting same error over and over again.

Error: Error selecting news: No database selected
  • 1
    Please, [don't use `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) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 21 '14 at 20:33
  • 1
    The error is self-explanatory, wouldn't you say? – Funk Forty Niner Oct 21 '14 at 20:33
  • I checked database few times but it all looks OK. Im not realy good in this stuff but i wanna learn :) @Jay thanks i should check out MySQLi. – Matic Kepa Oct 21 '14 at 20:36
  • Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Oct 21 '14 at 20:39

2 Answers2

2

you need to select a database. see mysql_select_db http://php.net/manual/de/function.mysql-select-db.php

<?php

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
  die('Connection failed : ' . mysql_error());
}

$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
   die ('Db does not exist : ' . mysql_error());
}
?>

Also note that mysql_* is deprecated. use new php style like http://php.net/manual/de/mysqli.select-db.php

Database in this example is "test"

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* return name of current default database */
if ($result = $mysqli->query("SELECT DATABASE()")) {
    $row = $result->fetch_row();
    printf("Default database is %s.\n", $row[0]);
    $result->close();
}

/* change db to world db */
$mysqli->select_db("world");

/* return name of current default database */
if ($result = $mysqli->query("SELECT DATABASE()")) {
    $row = $result->fetch_row();
    printf("Default database is %s.\n", $row[0]);
    $result->close();
}

$mysqli->close();
?>
Benjamin Eckstein
  • 884
  • 2
  • 9
  • 19
0

I'm guessing you need to specify which database you're attempting to query in your statement.

select a.id , a.headline, a.timestamp 
from database.news a 
order by a.timestamp desc

Replace "database" above with the name of yours.

jiy
  • 858
  • 2
  • 9
  • 18