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>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
sc1892353
  • 85
  • 1
  • 10
  • you did an edit without marking it as an edit, potentially getting my answer downvoted for it. – Funk Forty Niner Apr 28 '15 at 03: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? – bksi Apr 28 '15 at 03: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 – bksi Apr 28 '15 at 03:09
  • I did a rollback to your original post http://stackoverflow.com/revisions/29909337/1 – Funk Forty Niner Apr 28 '15 at 03:10
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 28 '15 at 11:32

2 Answers2

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:

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
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();
?>
channasmcs
  • 1,104
  • 12
  • 27