通过url中的特定id从MySQL数据库中获取数据

时间:2015-09-25 08:19:28

标签: php mysql

我是PHP MySQL的新手。我正在开发一个歌集网站。

我尝试从网址site/publicsong.php?id=12中的ID提取数据库中的数据。

    <?php


// Create connection
$conn = new mysqli($servername = "localhost";$username = "dansdlpe_dan";$password = "g+GbMr}DU4E@";$db_name = "dansdlpe_lyrics";);

// Check connection
$db_name = "dansdlpe_lyrics";
mysqli_select_db($conn,$db_name);

$id = $_GET['id'];
$id = mysqli__real_escape_string($conn,$id);
$query = "SELECT * FROM `lyrics_a` WHERE `id`='" . $id . "'";
$result = mysqli__query($conn,$query);

echo $row['id']; while($row = mysqli__fetch_array( $result )) {

echo "<br><br>";
echo $row['eng_title'];
echo $row['eng_lyrics'];
echo $row['alphabet'];
}
?>

我将mysql_更改为mysqli_并添加了$ conn。

我的结果仍然是空白。请帮帮我们提前谢谢。

3 个答案:

答案 0 :(得分:5)

所以我会坚持你已有的一些修复。

<?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'];
}
?>

答案 1 :(得分:-1)

无需将id用作字符串,
您可以使用:id=" . $id;

答案 2 :(得分:-1)

试试这个:

<?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'];
 }
 ?>