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
Related
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']));
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);
}
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.
Okay i`m loading images from a MySql Database server.. but when i click on it, it opens in a new page not in the lightbox popup.
// Make the connect to MySQL or die
// and display an error.
$db = mysql_connect($host, $username, $password);
if (!$db) {
die('Could not connect: ' . mysql_error());
}
// Select your database
mysql_select_db ($database);
$categorie = $_GET['categorie'];
echo "<h3>" . $categorie . "</h3><br><br>";
$query = "SELECT * FROM afbeelding WHERE categorie = '" . $categorie . "'";
$resultaat = mysql_query($query,$db) or die(mysql_error());
echo "<table>";
while ($rij = mysql_fetch_array($resultaat)){
echo "<img src=" . $rij["afbeelding"] . " width=\"150px\" height=\"150px\" class='foto' />";
}
echo "</table>";
?>
Please some help
If you're loading you images after the page is loaded, you might want to call the function that initiates the lightbox after the images arrive.
You need to set correct CSS class for your <A>, otherwise lightbox JS will not take care of it.
I have a PHP file which reads the credentials from
require("phpRequireInfo.php");
And, my problem is that it keeps giving me error whenever i were to compile it. My php file looks like this:
<?php
header("Content-type: text/xml");
require("phpRequireInfo.php");
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Opens a connection to a MySQL server
$connection=mysql_connect ($database, $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}
// Set the active MySQL database
$dbname= 'csuser';
// Set the active MySQL database
$db_selected = mysql_select_db($dbname, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM Bars ";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
// Iterate through the rows, adding XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name",$row['Name'] );
}
echo $dom->saveXML();
?>
And the error i am getting is: "error on line 4 at column 6: XML declaration allowed only at the start of the document"
The XML File looks like this when running the PHP FILE:
http://imgur.com/SWCZ8sE
Your help will greatly be appreciated
Why does it has 3 empty lines, at the begining?
try ob_start() before require, and ob_end_clean() before echo, to avoid them
ob_start();
require("phpRequireInfo.php");
.
.
.
ob_end_clean();
echo $dom->saveXML();