Giliweed
Giliweed

Reputation: 5177

how do I fetch data from mysql database?

I want to load the data from mysql

database connection: (database_cnn.php)

Edited:

    include 'database_cnn';
    $query= "select * from all_songs";
    $res=  mysql_query($query);
        $result= array();


     while ($row = mysql_fatch_array($res))
        array_push($result , array( 'id' => row[0],
            'title' => row[1], 
            'artist' => row[2] , 
            'album' =>row[3],
            'lyric' =>row[4]
            )
        );
     echo json_encode(array( "result" => $result));
   ?> 

the error I get:

   Parse error: syntax error, unexpected '[', expecting ')' in D:\xampp\htdocs\abc\IPA_3rd\database_out.php on line 13

can anyone tell me why am I getting this error? Is there something wrong with my code?

Upvotes: 0

Views: 108

Answers (5)

Prasanth Bendra
Prasanth Bendra

Reputation: 32820

Change include 'database_cnn'; to include 'database_cnn.php'; // missing extension

It is mysql_fetch_array NOT mysql_fatch_array Also use mysql_fetch_assoc instead of it

You forgot to put '$' for row variable

array_push($result , array( 'id' => row[0],
            'title' => row[1], 
            'artist' => row[2] , 
            'album' =>row[3],
            'lyric' =>row[4]
            )
        );

I suggest you to go through the php tutorials first.

Upvotes: 2

Xavier W.
Xavier W.

Reputation: 1360

If you have time, it would be a good idea to learn to use PDO instead of Mysql who is decrepated.

He is a link to the Doc

Upvotes: 0

Adnan Khan
Adnan Khan

Reputation: 917

use $row instead of row

while ($row = mysql_fetch_array($res))
    array_push($result , array( 'id' => $row[0],
        'title' => $row[1], 
        'artist' => $row[2] , 
        'album' =>$row[3],
        'lyric' =>$row[4]
        )
    );`

Upvotes: 1

Mihai
Mihai

Reputation: 26804

You are mixing up mysqli with mysql.

Upvotes: 1

Goutam Pal
Goutam Pal

Reputation: 1763

 array_push($result , array( 'id' => row[0],
            'title' => row[1], 
            'artist' => row[2] , 
            'album' =>row[3],
            'lyric' =>row[4]
            )

should be

 array_push($result , array( 'id' => $row[0],
            'title' => $row[1], 
            'artist' => $row[2] , 
            'album' => $row[3],
            'lyric' => $row[4]
            )

and mysql_fatch_array should be mysql_fetch_array

and

 $con = mysqli_connect("localhost", "root", "");
  if (mysqli_connect_errno()){
   echo "Failed to connect to MySQL: " . mysqli_connect_error();

should be

$con = mysql_connect("localhost", "root", "");
if (mysql_connect_errno()){
   echo "Failed to connect to MySQL: " . mysql_connect_error();

Upvotes: 1

Related Questions