簡體   English   中英

如何從mysql數據庫中獲取數據?

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

我想從mysql加載數據

數據庫連接:(database_cnn.php)

編輯:

    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));
   ?> 

我得到的錯誤:

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

誰能告訴我為什么會收到此錯誤? 我的代碼有問題嗎?

更改include 'database_cnn'; include 'database_cnn.php'; //缺少擴展名

它是mysql_fetch_array不是mysql_fatch_array也請使用mysql_fetch_assoc代替它

您忘記在行變量中輸入“ $”

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

我建議您先閱讀php教程。

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

應該

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

mysql_fatch_array應該是mysql_fetch_array

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

應該

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

您正在將mysqli與mysql混淆。

使用$ 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]
        )
    );`

如果您有時間,最好學習使用PDO而不是使用已棄用的Mysql。

他是文檔的鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM