1
<?php
  $servername = "localhost";
  $username = "user";
  $password = "pass";
  $dbname = "test";
  $tablename = "mapcoords";

  $conn = new mysqli($servername, $username, $password, $dbname);

  if ($conn->connect_error)
  {
    echo "Failure";
    die("Connection failed: " . $conn->connect_error);
  }

  echo "Connected successfully";
  $sql = "SELECT (lat, lng) FROM mapcoords";
  $result = $conn->query($sql);

    while($row = $result->fetch_assoc())
    {
      echo "ok";
    }



  $conn->close();

?>

Here is the code. So like I said, it can connect successfully, but the code won't successfully query. What's weird is that if I copy and paste the same code, which seems to be EXACTLY the same, it works. It makes no sense. I can't find a single difference between their code and my code besides the way they space things and the way I space things. Here is their code:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT (lat, lng) FROM mapcoords";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo $row["lat"]. " " . $row["lng"] . "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>
Sam McC
  • 301
  • 2
  • 12
  • 2
    `$sql = "SELECT (lat, lng) FROM mapcoords";` why the parenthesis? That's breaking your query. And you're just assuming that the query is successful, if you'd implemented proper error-handling and enabled reporting (`error_reporting(E_ALL);` and [`mysqli_error`](http://php.net/manual/en/mysqli.error.php)) you'd might figure out why too ;-) – Qirel Dec 09 '16 at 15:42
  • `"the code won't successfully query"` - Then what *does* it do? What's the error? http://php.net/manual/en/mysqli.error.php – David Dec 09 '16 at 15:43
  • @Qirel Then why does the w3 schools code work even if I use the same $sql statement, including the parentheses? – Sam McC Dec 09 '16 at 15:48
  • @david I get an error saying that I am trying to get a property of a non-object – Sam McC Dec 09 '16 at 15:49
  • @SamMcC: Examining the error message would be a good first step in diagnosing the error. Particularly the specific line of code, object you're attempting to reference, etc. Googling the error message will likely yield helpful information as well. – David Dec 09 '16 at 15:51
  • @SamMcC I wouldn't really use w3 as the best resource (but that's my opinion anyway), but I'm guessing the parenthesis were used in a different way, or with usage of functions in MySQL. – Qirel Dec 09 '16 at 15:51
  • @SamMcC can you point me to the W3 Schools tutorial you are following. I copied both of the sets of PHP you posted, setup a test DB, ran both and they both output the same exact error: Operand should contain 1 column(s) – Arnolio Dec 09 '16 at 16:22
  • I usually test my failing queries by typing them into mySQL. I did this with yours. `"SELECT (lat, lng) FROM mapcoords;` generates a syntax error, but `"SELECT lat, lng FROM mapcoords;` works fine. – Topological Sort Dec 09 '16 at 16:48
  • 1
    @qirel You were right... It was the parentheses. I didn't realize that you can't group things like that in SQL. Thank you so much for your help! Also, thanks for telling me about the error reporting, that makes troubleshooting way easier. – Sam McC Dec 09 '16 at 17:31

1 Answers1

3

The problem is that this query:

SELECT (lat, lng) FROM mapcoords

returns the folloowing error:

[21000][1241] Operand should contain 1 column(s)

You have to change the query to

SELECT lat, lng FROM mapcoords
Arnolio
  • 502
  • 3
  • 8
  • And the reason you're getting something about a non-object below is that when the query fails, `result` becomes `false`, which is not an object, so you can't look at its members. To get a more sensible err msg, always check the result of a query, and if it's false, print a sensible err msg. – Topological Sort Dec 09 '16 at 16:49
  • Thank you so much! – Sam McC Dec 09 '16 at 17:32
  • Glad I could help. Pick an answer if you feel it satisfied your question. Otherwise, post the answer and accept. – Arnolio Dec 09 '16 at 17:39