Get data from MySQL database by specific id in url

前端 未结 3 1704
孤独总比滥情好
孤独总比滥情好 2021-01-20 17:59

Im new to PHP MySQL. Im developing a songbook site.

I\'m trying to pull data in a database from the ID in the URL site/publicsong.php?id=12.

<         


        
相关标签:
3条回答
  • 2021-01-20 18:28

    So I will stick to what you already have with some fixes.

    <?php
    error_reporting(E_ALL); 
    ini_set('display_errors', 1);
    $servername = "localhost";
    $username = "xxxx";
    $password = "xxxxxx";
    $db_name = "xxxxxxxxx";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $db_name);
    // Check connection
    if ($conn->connect_error){
      die("Connection failed: " . $conn->connect_error);
    } 
    $id = $_GET['id'];
    $id = mysqli_real_escape_string($conn,$id);
    $query = "SELECT * FROM `lyrics_a` WHERE `id`='" . $id . "'";
    $result = mysqli_query($conn,$query);
    
    while($row = mysqli_fetch_array($result)) {
    echo "<br><br>";
    echo $row['id'];
    echo $row['eng_title'];
    echo $row['eng_lyrics'];
    echo $row['alphabet'];
    }
    ?>
    
    0 讨论(0)
  • 2021-01-20 18:35

    No need to use the id as a string,
    you can use like : id=" . $id;

    0 讨论(0)
  • 2021-01-20 18:38

    Try this:

    <?php
    $servername = "localhost";
    $username = "";
    $password = "";
    $db_name = "test";
    
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $db_name);
    // Check connection
    
     mysql_select_db($db_name);
    
    $id = $_GET['id'];
    $id = mysql_real_escape_string($id);
    $query = "SELECT * FROM `lyrics` WHERE `id`='" . $id . "'";
    $result = mysql_query($query);
    
    while($row = mysql_fetch_array( $result )) {
    
    echo "<br><br>";
    echo $row['title'];
    echo $row['content'];
     }
     ?>
    
    0 讨论(0)
提交回复
热议问题