Indicate encoding of XML file using objDOM ->load() - php

I am trying to read an XML file and then input the obtained value into a database. Then entire process works great , as long as there are no special characters in the XML. the XML is formatted as :
<link>
<name>Cech</name>
<club>Chelsea</club>
</link>
In case the name tag encloses a name like Suárez, i get the error: Input is not proper UTF-8, indicate encoding ! Bytes: 0xE1 0x72 0x65 0x7A in file:///C:/wamp/www/ADB/links.xml, line: 1857 in C:\wamp\www\ADB\phptry.php on line 14 , where line 1857 has the name Suárez . i tried including the <?xml version="1.0" encoding="UTF-8"?>
at the beginning of the file and using the utf8_encode(file_get_contents('links.xml')) but it doesnt work. Any suggestions? this is my working php code:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$objDOM = new DOMDocument();
//$content = utf8_encode(file_get_contents('links.xml'));
$objDOM->load('links.xml'); //make sure path is correct
$note = $objDOM->getElementsByTagName("link");
// for each note tag, parse the document and get values for
// tasks and details tag.
foreach( $note as $value )
{
$player = $value->getElementsByTagName("name");
$player_name = $player->item(0)->nodeValue;
$playername = addslashes($player_name);
$club = $value->getElementsByTagName("club");
$club_name = $club->item(0)->nodeValue;
// $points = $value->getElementsByTagName("points");
// $point_value = $points->item(0)->nodeValue;
$sql = "INSERT INTO pilayers (name,club) VALUES('$playername','$club_name')";
mysql_select_db('players');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
}
mysql_close($conn);
?>

The error says that the xml file is not encoded in utf-8. You declared the encoding in the PI instruction, but that does not mean that you editor really saved utf-8.
How to change the encoding depends on your editor/ide.
Eclipse: Edit -> Set Encoding
PHPStorm: File -> File Encoding

Related

Output from MysSQL query to xml file

I have this code i am trying to create a sitemap from and i need some help. When i run the php file i get the output of the file on the screen but no sitemap.xml file is created, anyone know why ?
<?
$xmlfile = 'sitemap.xml';
// this variable will contain the XML sitemap that will be saved in $xmlfile
$xmlsitemap = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
// Connection data (server_address, name, password, database_name)
$hostdb = 'localhost';
$userdb = 'user';
$passdb = 'ps';
$namedb = 'db';
try {
// Connect and create the PDO object
$conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$conn->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8
// Define and perform the SQL SELECT query
$sql = "SELECT id, shortUrl FROM shorturl WHERE id BETWEEN 15 AND 45000";
$result = $conn->query($sql);
// If the SQL query is succesfully performed ($result not false)
if($result !== false) {
// Parse the result set, and add the URL in the XML structure
foreach($result as $row) {
$xmlsitemap .= '
<br><br>
<url><br>
<loc>https://website.com/'. $row['shortUrl'] .'<loc><br>
<changefreq>monthly<changefreq>
<priority>1<priority><br>
<url>
';
}
}
You have to write the content of your $xmlfile variable to a file.
Try file_put_contents('sitemap.xml', $xmlfile); after the foreach loop.
But i am wondering whether <br> works in xml

php query not using UTF-8 charset

I am getting my urls and titles from a post's content, but the titles no longer seem to be UTF-8 and include some funky characters such as "Â" when I echo the result. Any idea why the correct charset isn't being used? My headers do use the right metadata.
I tried some of the solutions on here, but none seems to work so I thought I'd add my code below - just in case I'm missing something.
$servername = "localhost";
$database = "xxxx";
$username = "xxxxx";
$password = "xxxx";
$conn = mysqli_connect($servername, $username, $password, $database);
$post_id = 228;
$content_post = get_post($post_id);
$content = $content_post->post_content;
$doc = new DOMDocument();
$doc->loadHTML('<?xml encoding="utf-8" ?>' . $content);
$links = $doc->getElementsByTagName('a');
$counter = 0;
foreach ($links as $link){
$href = $link->getAttribute('href');
$avoid = array('.jpg', '.png', '.gif', '.jpeg');
if ($href == str_replace($avoid, '', $href)) {
$title = $link->nodeValue;
$title = html_entity_decode($title, ENT_NOQUOTES, 'UTF-8');
$sql = "INSERT INTO wp_urls_download (title, url) VALUES ('$title', '$href')";
if (mysqli_query($conn, $sql)) {
$counter++;
echo "Entry" . $counter . ": $title" . "<br>";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
}
Updated Echo string - changed this after I initially uploaded the code. I have already tried the solutions in the other posts and was not successful.
Did you try to set the utf8 charset on the connection?
$conn->set_charset('utf8');
For more information: http://php.net/manual/en/mysqli.set-charset.php
It seems that you have "double-encoding". What you expected was
Transverse Abdominis (TVA)
But what you have for the space before the parenthesis is a special space that probably came from Microsoft Word, then got converted to utf8 twice. In hex: A0 -> c2a0 -> c382c2a0.
Yes, the link to "utf8 all the way through" would ultimately provide the fix, but I think you need more help.
The A0 was converted from latin1 to utf8, then treating those bytes as if they were latin1 and repeating the conversion.
The connection provide the client's encoding via mysqli_obj->set_charset('utf8') (or similar).
Then the column in the table should be CHARACTER SET utf8mb4 (or utf8). Verify with SHOW CREATE TABLE. (It is probably latin1 currently.)
HTML should start with <meta charset=UTF-8>.
Trouble with UTF-8 characters; what I see is not what I stored

HTML document was not declared

This is about retriving the data in form of CSV from Mysql Table : -
Code , I tried :-
<?php
// mysql database connection details
$host = "localhost";
$username = "root";
$password = "hello";
$dbname = "mysql2csv";
// open connection to mysql database
$connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));
// fetch mysql table rows
$sql = "select * from tbl_books";
$result = mysqli_query($connection, $sql) or die("Selection Error " . mysqli_error($connection));
$fp = fopen('books.csv', 'w');
while($row = mysqli_fetch_assoc($result))
{
fputcsv($fp, $row);
}
fclose($fp);
//close the db connection
mysqli_close($connection);
?>
Errors Obtained...
04:12:27.093 The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.1 mysql2csv.php.
your help will be appreciated ...
Add those lines to your html header
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
Edit:
If you are using PHP file:
header('Content-Type: text/html; charset=utf-8');

Remove unwanted lines while Content-Type: text/plain

I will describe my problem with code - it would be the best.
<?
include('configs.php');
require_once 'DBQueries.php';
$con = mysql_connect( $db_host, $db_user, $db_pass );
mysql_query("SET NAMES 'cp1250'") or die('Could not set names');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_dbname);
$oUnexportedOrders = DBQueries::getInstance()->getUnexportedOrders();
header("Content-Type: text/plain");
while ($aOrderExport = mysql_fetch_assoc ($oUnexportedOrders)){
echo $aOrderExport['data'];
}
What is happening:
include some stuff
connection to DB
get data from DB
IMPORTANT: set header as Content-Type: text/plain
IMPORTANT: print text data with echo
Result:
**!!! There are 7 unwanted lines !!!**
line of data
line of data
line of data
line of data
....
Expected result:
line of data
line of data
line of data
line of data
- Expected is lines of data generated by echo inside the for, but without that 7 lines.
QUESTION:
How to do that, what to call when (etc.) to get rid of those unwanted lines?
Thank you.
ob_clean(); will clear out the output buffer, in conjuction with ob_start();
<?
ob_start();
include('configs.php');
require_once 'DBQueries.php';
$con = mysql_connect( $db_host, $db_user, $db_pass );
mysql_query("SET NAMES 'cp1250'") or die('Could not set names');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_dbname);
$oUnexportedOrders = DBQueries::getInstance()->getUnexportedOrders();
ob_clean();
header("Content-Type: text/plain");
while ($aOrderExport = mysql_fetch_assoc ($oUnexportedOrders)){
echo $aOrderExport['data'];
}
That should get rid of any unwanted extra whitespace from included files.
The blank lines don't come from the mysterious unknown. They're in your code somewhere. Check your files (including the files you include/require) for whitespace before your opening PHP tags and after your closing PHP tags. That whitespace will be passed to the browser.

PHP MYSQL XML ERROR

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();

Categories