The program is as follows:
<?php
require("phpsqlajax_dbinfo.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// Opens a connection to a MySQL server
$connection=mysqli_connect ('localhost', $username, $password);
if (!$connection) {
die('Not connected : ' . mysqli_error());
}
// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
die ('Can\'t use db : ' . mysqli_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysqli_query($query);
if (!$result) {
die('Invalid query: ' . mysqli_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo "<?xml version='1.0' ?>";
echo '<markers>';
$ind=0;
// Iterate through the rows, printing XML nodes for each
while ($row = @mysqli_fetch_assoc($result)){
// Add to XML document node
echo '<marker ';
echo 'id="' . $row['id'] . '" ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'type="' . $row['type'] . '" ';
echo '/>';
$ind = $ind + 1;
}
// End XML file
echo '</markers>';
?>
It gives me the following errors:
Warning: mysqli_query() expects at least 2 parameters, 1 given in /storage/ssd2/594/12253594/public_html/mapa.php on line 28
Warning: mysqli_error() expects exactly 1 parameter, 0 given in /storage/ssd2/594/12253594/public_html/mapa.php on line 30
Invalid query:
The warnings are clear:
mysqli_query() expects at least 2 parameters, 1 given
It tells you that it
mysqli_query()
needs two parameters, which are: 1st the connection; 2º the query to execute. You are only passing one.mysqli_error() expects exactly 1 parameter, 0 given
It tells you that it
mysqli_error()
is expecting a parameter, which is the connection, but you are not passing any.To solve both problems:
Wherever you use
mysqli_query
you should put it like this:And wherever you use
mysqli_error
:Warning about bad practices and deprecated practices
In your code there are bad practices that consist of the use of
die()
without compassion and in silencing possible errors with@
. You should avoid it, taking control of your code in hand to emit messages or redirect in case of failures. Silencing errors or delegatingdie()
can lead to unexpected behavior.In the new API
mysqli
you can pass the database you want to connect to as the 4th parameter, so you can skip this part:Creating the connection like so:
mysqli_select_db()
only useful if you are connected to one database and want to switch to another.