简体   繁体   中英

PHP variables not printing to HTML webpage

We have the following code in the HTML of one of our webpages. We are trying to display all of the Wifi speeds and GPS locations in our database using the MySQL call and while loop shown in the below PHP code. However, it doesn't return anything to the page. We put echo statements in various parts of the PHP (ex. before the while loop, before the database stuff) and it doesn't even print those statements to the webpage.

<body>
<h2>WiFi Speeds in the System</h2>
<p>Speeds Recorded in the System</p>
<?php
  $username = "root";
  $password = "";
  $hostname = "localhost";
  $dbc = mysql_connect($hostname, $username, $password)
    or die('Connection Error: ' . mysql_error());
  mysql_select_db('createinsertdb', $dbc) or die('DB Selection Error' .mysql_error());
  $data = "(SELECT Wifi_speed AND GPS_location FROM Instance)";
  $results = mysql_query($data, $dbc);
  while ($row = mysql_fetch_array($results)) {
      echo $row['Wifi_speed'];
      echo $row['GPS_location'];
  }
?>
</body>

This line is incorrect, being the AND :

$data = "(SELECT Wifi_speed AND GPS_location FROM Instance)";
                            ^^^

Change that to and using a comma as column selectors:

$data = "(SELECT Wifi_speed, GPS_location FROM Instance)";

However, you should remove the brackets from the query:

$data = "SELECT Wifi_speed, GPS_location FROM Instance";

Using:

$results = mysql_query($data, $dbc) or die(mysql_error());

would have signaled the syntax error. Yet you should use it during testing to see if there are in fact errors in your query.


Sidenote:

  • AND is used for a WHERE clause in a SELECT .

Ie:

SELECT col FROM table WHERE col_x = 'something' AND col_y = 'something_else'

Or for UPDATE , ie:

UPDATE table SET col_x='$var1' 
WHERE col_y='$var2' 
AND col_z='$var3'

Footnotes:

Consider moving to mysqli with prepared statements , or PDO with prepared statements , as mysql_ functions are deprecated and will be removed from future PHP releases.


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

"Thank you for the suggest but we tried that and it didn't change anything. – sichen"

  • You may find that you may not be able to use those functions after all. If that is the case, then you will need to switch over to either mysqli_ or PDO.

References:

hi mate i see some problem with your DB connection & query

here is example check this out

in SELECT is incorrect, being the AND .using a comma as column selectors:

and make condition for after set query & check data validation that is proper method

<?php 
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "createinsertdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);  } 
$sql = "SELECT `Wifi_speed `, `GPS_location `,  FROM `Instance`";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
 echo $row['Wifi_speed'];
 echo $row['GPS_location'];
   }
} else {
echo "0 results";
}
$conn->close();
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM