简体   繁体   English

如何从mysql数据库中获取数据?

[英]how do I fetch data from mysql database?

I want to load the data from mysql 我想从mysql加载数据

database connection: (database_cnn.php) 数据库连接:(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? 我的代码有问题吗?

Change include 'database_cnn'; 更改include 'database_cnn'; to include 'database_cnn.php'; include 'database_cnn.php'; // missing extension //缺少扩展名

It is mysql_fetch_array NOT mysql_fatch_array Also use mysql_fetch_assoc instead of it 它是mysql_fetch_array不是mysql_fatch_array也请使用mysql_fetch_assoc代替它

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. 我建议您先阅读php教程。

 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 mysql_fatch_array应该是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();

您正在将mysqli与mysql混淆。

use $row instead of row 使用$ row代替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]
        )
    );`

If you have time, it would be a good idea to learn to use PDO instead of Mysql who is decrepated. 如果您有时间,最好学习使用PDO而不是使用已弃用的Mysql。

He is a link to the Doc 他是文档的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM