XML Parsing Error: junk after document element. Google maps database interaction - php

Hi so I'm trying to pull data from a database to be used with Google Maps following the advice from a Google developers article: https://developers.google.com/maps/articles/phpsqlajax_v3. I have copied their exact code in an attempt to just get a start before manipulating it but it keeps throwing the following error: XML Parsing Error:
junk after document element
Location: http://localhost/Google/phpsqlajax_xml3.php
Line Number 2, Column 1:
^
I know the same error has been posted before, but I'm yet to find a solution that works for me. Any advice is appreciated! Here's my code, straight from google:
<?php
//require("phpsqlajax_dbinfo.php");
$username="root";
$password="";
$database="my_db";
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=mysql_connect('localhost', $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'type="' . $row['type'] . '" ';
echo '/>';
}
// End XML file
echo '</markers>';
?>

Just in case anyone else is stuck with a similar issue it was resolved by changing all the mysql statements to their mysqli equivalent.

Related

phpMyAdmin sql query is always returning 500 Internal Error?

I'm new to PHP.I have been trying to get data from Database and convert them to json . However when I open the chrome console. It says 500 : Internal Server Error.
Here is my PHP Code
<?php
function get_data(){
$link = mysqli_connect("localhost:8889","root2","root","EMPLOYEE");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
$sql = "SELECT * FROM people";
$result = mysqli_query($link,$sql);
$json_array = array();
while($row = mysql_fetch_array($result))
{
$json_array[] = array(
'id' => $row["id"],
'name' => $row["name"]
);
}
$json_array['name'] = "sarath";
$json_array['age'] = 19;
#echo $json_array;
return json_encode($json_array);
}
echo '<pre>';
print_r(get_data());
echo '</pre>';
?>
I get the following output at http://localhost:8888/peo.php. The json_array was not getting printed.
<pre>Success: A proper connection to MySQL was made! The my_db database is great.
Host information: localhost:8889 via TCP/IP
THE DATABASE IMAGE FROM PHPMyAdmin has been linked here
1) You open the connection using mysqli_connect but later use mysql_fetch_array. Change it to mysqli_fetch_array: those are different drivers.
2) Are you sure that you have got json addon installed? You can check it using phpinfo(); or just try to var dump: var_dump(json_encode(['something']));

Storing array values in he database using php?

I am trying to create an php script which can read and array list from android and read it's element and store it in data base, now I have been able to store the data in an text file but unable to proceed further, could some body help me out in this matter?
this is the php script which I have used:-
<?php
$i=0;
$password="";
$user="root";
$database="shadowpets";
$host="localhost";
$response=array();
$con=mysqli_connect($host,$user,$password,$database)or die("Unable to connect");
if($_SERVER["REQUEST_METHOD"]=="POST"){
if(isset($_POST['OrderSummary'])){
$data=$_POST['OrderSummary'];
$file='text.txt';
$result=file_put_contents($file,$data);
$response["success"]=1;
$response["message"]="done";
}else{
$response["success"]=0;
$response["message"]="parameters not correctly formatted";
}
echo json_encode($response);
}
?>
what changes I can do here, so I can read the data store it in the database?
First, you need to set up a database and a table in a database server (MySQL).
Then you can create an insert statement like below, bind its parameters and execute it.
if (!($statement = $con->prepare('INSERT INTO `your_table` (`OrderSummary`) VALUES (?)'))) {
die('Could not prepare statement: (' . $con->errno . ') ' . $con->error);
}
if (!$statement->bind_param('s', $_POST['OrderSummary'])) {
die('Could not bind parameter: (' . $statement->errno . ') ' . $statement->error);
}
if (!$statement->execute()) {
die('Could not execute statement: (' . $statement->errno . ') ' . $statement->error);
}

Google Maps documentation uses deprecated PHP - how can I update it?

https://developers.google.com/maps/documentation/javascript/mysql-to-maps
Features deprecated PHP for connecting to a MySQL database, i.e. using mysql instead of mysqli or pdo.
I tried following the tutorial to display markers with set names/lat/lng from a mySQL database, but found that the PHP was deprecated and simply replacing 'mysql' with 'mysqli' gave me a nearly empty XML document.
Here is my PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "pins_db";
$database = "root-pins_db";
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);
// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $dbname);
// Select all the rows in the markers table
$query = "SELECT * FROM pins_table WHERE 1";
$result = mysqli_query($query);
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = #mysqli_fetch_assoc($result)){
// Add to XML document node
echo '<marker ';
echo 'name="' . $row['name'] . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo '/>';
}
// End XML file
echo '</markers>';
?>
The resulting XML document only contains:
<markers/>
I'm running PHP 7.0.15 and MySQL 5.6.35 on MAMP.
The database info:
user/pw: root/root
database: pins_db
table: pins_table
The table:
1 id int(11)
2 datetime datetime
3 lat float(10,6)
4 lng float(10,6)
5 name text
Am I missing something here? I'm fairly new to PHP and very new to mySQL; I can't find an answer to this anywhere else online.
Turn on php error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
http://php.net/manual/en/function.error-reporting.php
http://php.net/manual/en/function.ini-set.php
Test for successful execution of the query execution.
$result = mysqli_query($connection,$query);
if(!$result) {
// error returned
die('error#002: '.mysqli_error($connection));
}
http://php.net/manual/en/mysqli.error.php
Remove the # (at sign) before the function call, because we do not want to silence php errors:
while ( $row = #mysqli_fetch_assoc($result) ) {
^
Also check for a successful connection
$connection = mysqli_connect('localhost', $username, $password);
if(!$connection) {
die('error connecting: ' . mysqli_connect_error());
}
http://php.net/manual/en/mysqli.connect-error.php
It's also possible that mysqli_select_db isn't working. For example if the database name is incorrect or doesn't exist.
http://php.net/manual/en/mysqli.select-db.php
Note that a database name could be supplied as a fourth argument on mysqli_connect.
https://ericlippert.com/2014/03/05/how-to-debug-small-programs/

how to change mysql database code into postgresql code using core php?

I am using function based database connection and select query also plz help me.
FIRST CODE:
function connectDb(){
//my postgresql db connection
$link=pg_pconnect('host=' . __DB_HOST__ . ' port=' . __DB_PORT__ . ' dbname=' . __DB_NAME__ . ' user=' . __DB_USER__ . ' password=' . __DB_PWD__) or die('connection failed');
AllDelete();// Audit Logs Auto Deletion
}
SECOND CODE:
function AllDelete(){
$MysqlForLogs=pg_query("select audit.*,users.username from audit,users where audit.user_id=users.id and DATE_PART('day',now()-audit.date) > 7") or die(pg_last_error());
$CountRows=pg_num_rows($MysqlForLogs);
}
I am executing the browser it will show on error like 'No database selected' but really i am giving the database name.
What is the problem?

Encoding Error in PHP script to generate XML

I'm having problems with my PHP file that generates XML from MySQL database. The code is below
<?php
require("decibelone_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=mysql_connect (localhost, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM location WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker>';
echo '<name>' . parseToXML($row['name']) . '</name>';
echo '<address>' . parseToXML($row['address']) . '</address>';
echo '<latitude>' . $row['latitude'] . '</latitude>';
echo '<longitude>' . $row['longitude'] . '</longitude>';
echo '<description>' . $row['description'] . '</description>';
echo '<time>' . $row['time'] . '</time>';
echo '</marker>';
}
// End XML file
echo '</markers>';
?>
I'm not sure what the problem is, but I have changed all my character encodings (in mysql and under htaccess) to UTF8. The problem surfaced recently after a server maintenance by my host, and I did not change my file before and after it, so I doubt that it could be a problem with my code itself. Can anyone point me in the right direction? Thanks in advance!
You can use DomDocument class. I think what you are using is an old way. Please loo at following link.
http://php.net/manual/en/class.domdocument.php

Categories