Coder42
Coder42

Reputation: 1

Retrieving information from a database through php

I have been coding a webform for facilities that when you select a facility it should bring up the relevant carpark i.e the one closest to it. I know my $result is wrong just dont know how to fix it:

$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>";
      }
?>

Any ideas?

Upvotes: 0

Views: 37

Answers (1)

Daniel Dudas
Daniel Dudas

Reputation: 3002

You don't query anywhere the DB. You need to execute mysqli_query() with the db handler and the query string. That line is missing from your code. It should be like this:

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

Also change all mysql_* to mysqli_*

Upvotes: 1

Related Questions