2

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>
5
  • you did an edit without marking it as an edit, potentially getting my answer downvoted for it. Commented Apr 28, 2015 at 3:01
  • You can check if you have a result in $data using var_dump($results); You can check if the result is giving error as Fred - ii suggested. Obviosly you get error when you get the results from the DB. Did you try same query directly to the DB using PMA or somthing similar? Commented Apr 28, 2015 at 3:05
  • also mysql_fetch_array returns indexed, and not associative array. You may want to use mysql_fetch_assoc instead, but better is to use mysqli or PDO, since mysql_* functions are deprecated Commented Apr 28, 2015 at 3:09
  • I did a rollback to your original post stackoverflow.com/revisions/29909337/1 Commented Apr 28, 2015 at 3:10
  • Please, stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO. Commented Apr 28, 2015 at 11:32

2 Answers 2

5

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.

I.e.:

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

Or for UPDATE, i.e.:

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:

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for the suggest but we tried that and it didn't change anything.
@sichen you did an edit without marking it as an edit, potentially getting my answer downvoted for it.
@sichen reload my answer I've made an edit about error reporting near the bottom. Plus about using $data = "SELECT Wifi_speed, GPS_location FROM Instance"; instead and removing the brackets.
@sichen I have to ask this question; you are using .php as a file extension correct? Plus, your server is properly configured for PHP/MySQL etc.? Do re-read my entire answer over again please and reload it just in case you didn't see the edit.
We changed it to a .php, but now it only returns the first row in the database. The server is properly configured.
|
0

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();
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.