-2

I am trying the fetch the data from my database sunypub from the table journal.

Out of many attributes, I am trying to get three atrributes which are of my use on the webpage through PHP, but it is not showing anything on the webpage.

This is the option which is directing to the page display.php, which will show me the attributes value of attribute jname, date and location from the table journal

<div align = "left">
    <form action = "display.php">
        <input type = "submit" value = "Show all the Conference List">
    </form>
</div>

display.php:

<? php

    // Create Local variable
    $taken = "false";
    $database = "sunypub";
    $password = "";
    $username = "root";



    // Connect to database
    $con = mysql_connect('localhost', $username, $password, $database) or die ("Unable to connect");
    @mysql_select_db($database) or die("Database not found");
    echo "Database Connected";


    $query = "select * from journal "; 
    $result = mysql_query($con,$query) or die("Strange Error");
    echo "Database Connected";

    while( $row = mysql_fetch_assoc( $result, MYSQL_ASSOC ) ){
        echo $row['jname'];
        echo $row['date'];
        echo $row['location']; 
        echo "Database Connected";
    }

    mysql_close($con);

?>
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
hawkeye
  • 349
  • 4
  • 21
  • If you can, you should [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](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 10 '15 at 18:25
  • 1
    Please read the manual: http://php.net/manual/en/function.mysql-query.php The order of the arguments does matter! (BTW: Why your code is wrong, just change to PDO or `mysqli_*`, instead of fixing your old code; + `< ? php` <- space, remove it) – Rizier123 Jun 10 '15 at 18:25
  • 1
    Also, quit suppressing errors with `@`, you'll never know what the problems are. – Jay Blanchard Jun 10 '15 at 18:25
  • 1
    `or die("Strange Error")` will never help you. at the very least you want to use `mysql_error()` -> `or die(mysql_error())`. – Sean Jun 10 '15 at 18:27

1 Answers1

1

mysql_* is deprecated and is removed in new PHP version. So I highly recommend you to change to PDO or mysqli_* prepared statements, instead of fixing your old code.

So your code could look something like this:

(Note that you have to remove the space here: <? php)

<?php

    // Create Local variable
    $taken = "false";

    $dbhost = "localhost";
    $dbname = "sunypub";
    $dbpass = "";
    $dbuser = "root";



    try {

        $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql = "SELECT * FROM journal"; 
        foreach($dbh->query($sql) as $row) {
            echo $row['jname'];
            echo $row['date'];
            echo $row['location']; 
            echo "Database Connected";
        }

        $dbh = NULL;

    } catch(PDOException $e) {
        echo $e->getMessage();
    }

?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156