通过url中的特定字段从MySQL数据库表中获取数据

时间:2015-09-25 09:49:46

标签: php mysql

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

我想在每个字母表的单独页面中按字母顺序创建表格。

当用户点击菜单中的字母时,它会指向此页面site/publicsearch.php?browse=a

这是数据库:

| ID  |  TITLE        | ARTIST | CATEGORY | ALPHABET |
+----------------------------------------------------+
|  1  | Amazing love  | XXXXXX | Love     |     a    |
|  2  | Above all     | XXXXXX | Worship  |     a    |
|  3  | BXXXX         | XXXXXX | Love     |     b    |
|  4  | BXXXX         | XXXXXX | Worship  |     b    |

我像这样拉上面的数据库表:它工作正常。

   <?php
$servername = "localhost";
$username = "xxxxxxx";
$password = "xxxxxxxx";
$db_name = "xxxxxxxxxxx";

// Create connection
$conn = new mysqli($servername, $username, $password, $db_name);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

echo "Connected successfully";
exit();
}
    // Attempt select query execution

    $sql = "SELECT * FROM lyrics_a";

    if($result = mysqli_query($conn, $sql)){

        if(mysqli_num_rows($result) > 0){

            echo "<table class='table'>";

                echo "<tr>";

                    echo "<th>#</th>";

                    echo "<th>Title</th>";

                    echo "<th>artist</th>";

                    echo "<th>cateogry</th>";

                echo "</tr>";

            while($row = mysqli_fetch_array($result)){

                echo "<tr>";

                    echo "<td>" . $row['id'] . "</td>";

            echo "<td> <a href=publicsong.php?id=".$row['id'] . ">" . $row['eng_title'] . "</a> " . $row['tel_title'] . " </td>";

                    echo "<td>" . $row['artist'] . "</td>";

                    echo "<td>" . $row['category'] . "</td>";

                echo "</tr>";

            }

            echo "</table>";

            // Close result set

            mysqli_free_result($result);

        } else{

            echo "No records matching your query were found.";

        }

    } else{

        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

    }



    // Close connection

    mysqli_close($link);

    ?>

我需要这样的单独的字母表:

Alphabet "a" table results `site/publicsearch.php?browse=a` 

| ID  |  TITLE        | ARTIST | CATEGORY | ALPHABET |
+----------------------------------------------------+
|  1  | Amazing love  | XXXXXX | Love     |     a    |
|  2  | Above all     | XXXXXX | Worship  |     a    |

Alphabet "b" table results `site/publicsearch.php?browse=b` 

| ID  |  TITLE        | ARTIST | CATEGORY | ALPHABET |
+----------------------------------------------------+
|  3  | BXXXX         | XXXXXX | Love     |     b    |
|  4  | BXXXX         | XXXXXX | Worship  |     b    |

我尝试了下面的代码不起作用。

<?php
$servername = "localhost";
$username = "xxxxxx";
$password = "xxxxxxx";
$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);
} 
$alphabet = $_GET['alphabet'];
$id = mysqli_real_escape_string($conn,$id);
$query = "SELECT * FROM `lyrics` WHERE `alphabet`='" . $alphabet . "'";
$result = mysqli_query($conn,$query);

while($row = mysqli_fetch_array($result)) {
echo "<br><br>";
echo $row['id'];
echo $row['title'];
echo $row['lyrics'];
echo $row['alphabet'];
}
?>

我是新手。请帮忙。谢谢。

2 个答案:

答案 0 :(得分:1)

您建议使用以下网址:publicsearch.php?browse=a

但是在您的脚本中,您使用的是请求参数$_GET['alphabet']

此外,您的数据库中不需要单独的列alphabet,您可以使用SQL's LIKE

$letter = $_GET['browse'];
$sql = "SELECT * FROM `lyrics` WHERE title LIKE '{$letter}%'"

答案 1 :(得分:1)

$alphabet = $_GET['alphabet'];
$id = mysqli_real_escape_string($conn,$id);
$query = "SELECT * FROM `lyrics` WHERE `alphabet`='" . $alphabet . "'";

替换为:

$alphabet = mysqli_real_escape_string($conn,$_GET['browse']);
$query = "SELECT * FROM `lyrics` WHERE `alphabet`= '" . $alphabet . "' ";