2

I am using this tutorial from the google maps api to generate xml from mysql

This is the error I'm getting:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource

From what I can tell in my code, $result should have the resource via the query.

Does anyone know why it is giving this specific error?

The code:

<?php

// Start XML file, create parent node

    $dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node); 
?>
<?php   
    // connect
    $host = "localhost";
    $username = 'root';
    $psswrd = 'root';
    $db = 'sql_maps';
    $dbc = mysqli_connect($host, $username, $psswrd, $db);

    if(!mysqli_connect_errno() ) {
        echo 'sueccess'; } else { 
        die("Database failed: " . 
        mysqli_connect_error() .
        " ( " . mysqli_connect_errno() . " )"
        );

    }
?>
<?php
// Using PHP's domxml Functions to Output XML
// Select all the rows in the markers table

    // get data
    $query = "SELECT * FROM markers WHERE 1";

    // catch resource(collection of database rows)
    $result = mysqli_query($dbc, $query);
    // check
    if($result) {
        echo 'success'; 
    } else {
        die("connection failed");
    }



header("Content-type:  application/xml"); 

while ($row = mysql_fetch_assoc($result)){  
  // ADD TO XML DOCUMENT NODE  
  $node = $dom->createElement("marker");  
  $newnode = $parnode->appendChild($node);   
  $newnode->setAttribute("name",$row['name']);
  $newnode->setAttribute("address", $row['address']);  
  $newnode->setAttribute("lat", $row['lat']);  
  $newnode->setAttribute("lng", $row['lng']);  
  $newnode->setAttribute("type", $row['type']);
} 

echo $dom->saveXML();

?>

Also, what does WHERE 1 describe in the SELECT query? I'm assuming that is referring to the id and then it would continue the while loop from there?

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Paul Yorde
  • 75
  • 1
  • 2
  • 12
  • `WHERE 1` is redundant. The value `1` is always true, so the `WHERE` selects every row of the table. Leave off `WHERE` and you have the same result. – Kevin Panko Oct 25 '13 at 20:31

1 Answers1

1

$result is a ressource(otherwise the script would exit with "connection failed"), but not of the expected type.

You are mixing mysqli and mysql-functions.

change the faulty line to

while ($row = mysqli_fetch_assoc($result)){ 
//-----------------^

what does 'WHERE 1' describe in the SELECT query?
It does nothing, it's a where-clause which always evaluates to true, all rows will be selected. You may remove this. See: Importance of WHERE 1 in MySQL queries

Community
  • 1
  • 1
Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
  • Funny, I had read another thread telling the author that they were mixing mysql with mysqli, but then didn't think to check my own. What do you mean by expected type? Appreciate the info concerning the resource behavior and the where clause. I read the thread you referenced for the where clause, and it seems to suggest convenience to use it? – Paul Yorde Oct 25 '13 at 19:33
  • The expected type of ressource is a **`mysql`**-ressource, but the ressource returned by mysqli_query is a **`mysqli`**-ressource. – Dr.Molle Oct 25 '13 at 19:46
  • The convenience of the where-clause is interesting when you use dynamic where-clauses. But you don't use them, so you may ommit the where-clause completely. – Dr.Molle Oct 25 '13 at 19:49