通过php从数据库中检索信息

时间:2016-10-28 13:50:23

标签: php

我一直在编写一个网页表格,当你选择一个设施时,它应该带出相关的停车场,即离它最近的停车场。我知道我的$result错了,只是不知道如何解决它:

$servername = 'servername';

$username = 'username';

$password = 'password';

$SQL = mysql_connect($servername, $username, $password);


$queryNumber = $_GET['query'];

switch ($queryNumber) {  
  case "q1":

    echo "<p>Query 1</p>";

$_GET['facility'];

    echo "<p id='sql'>".$SQL."</p>";  
    echo "<table>";
    echo "   <th>Carpark</th>";
    $result = "SELECT nearbyfacilities, campus FROM CarPark"; // names of columns in my database
    while ( $db_field = mysql_fetch_assoc($result) ) {
        echo "<tr><td>" . $db_field['carpark'] . "</tr></td>";
      }
?>

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您无需在数据库的任何位置进行查询。您需要使用db处理程序和查询字符串执行mysqli_query()。您的代码中缺少该行。它应该是这样的:

$query = "SELECT nearbyfacilities, campus FROM CarPark"; // names of columns in my database
$result = mysqli_query($SQL, $query);
while ( $db_field = mysqli_fetch_assoc($result) ) { ... }

同时将所有mysql_*更改为mysqli_*