Good morning,
I created a script in php to export the data of a table of a db in mysql to xml.
With my script the file is created, but on the screen I always get this error:
Notice: Object of class mysqli_result could not be converted to int in C:\xampp\htdocs\testxml001\exportxml.php on line 9
Below is also the script in php
<?php
$conn = mysqli_connect("localhost", "root", "", "dbtest001");
if(!$conn){
echo "DB not Connected...";
}
else{
$result=mysqli_query($conn, "Select * from table001");
if($result>0){
$xml = new DOMDocument("1.0");
// It will format the output in xml format otherwise
// the output will be in a single row
$xml->formatOutput=true;
$dbtest001=$xml->createElement("table001");
$xml->appendChild($dbtest001);
while($row=mysqli_fetch_array($result)){
$RECORD=$xml->createElement("RECORD");
$dbtest001->appendChild($RECORD);
$ID=$xml->createElement("ID", $row['ID']);
$RECORD->appendChild($ID);
$category=$xml->createElement("category", $row['category']);
$RECORD->appendChild($category);
$ad_zip=$xml->createElement("ad_zip", $row['ad_zip']);
$RECORD->appendChild($ad_zip);
$ad_city=$xml->createElement("ad_city", $row['ad_city']);
$RECORD->appendChild($ad_city);
$ad_phone=$xml->createElement("ad_phone", $row['ad_phone']);
$RECORD->appendChild($ad_phone);
$email=$xml->createElement("email", $row['email']);
$RECORD->appendChild($email);
}
echo "<xmp>".$xml->saveXML()."</xmp>";
$xml->save("report.xml");
}
else{
echo "error";
}
}
?>
Related
I have made an "api", I have converted data from my sql table into JSON and that is echoed onto my file "api.php", I have "get.php" and I would like to get specific bits of data from the JSON on get.php. It's not working and just throwing an error however
This is for a contract, I have already tried cURL but it doesn't work
api.php (not included login variables)
$dblink = new mysqli($servername, $username, $password, $dbname);
if ($dblink->connect_errno) {
printf("Failed to connect to database");
exit();
}
$result = $dblink->query("SELECT * FROM updates LIMIT 3");
$dbdata = array();
while ( $row = $result->fetch_assoc()) {
$dbdata[]=$row;
}
echo json_encode($dbdata);
and this is what I see on api.php, the JSON is corrected echod i just can't access it.
[{"id":"1564343527","title":"title","type":"Server","overview":"overview","added":"a:2:{i:0;s:6:\"added1\";i:1;s:6:\"added2\";}","removed":"a:2:{i:0;s:8:\"removed1\";i:1;s:8:\"removed2\";}","changed":"a:1:{i:0;s:0:\"\";}","date":"2019\/07\/28","time":"09:52:07pm"}]
get.php
<?php
$strJsonFileContents = file_get_contents('api.php');
var_dump($strJsonFileContents); // show contents
?>
error (on get.php)
string(610) "connect_errno) { printf("Failed to connect to database"); exit(); } $result = $dblink->query("SELECT * FROM updates LIMIT 3"); $dbdata = array(); while ( $row = $result->fetch_assoc()) { $dbdata[]=$row; } echo json_encode($dbdata); ?> "
Expected result is for it to echo the page (api.php) so i can pick apart the JSON, actual result is an error.
Not sure about the extent of your code, but I think this can be done simply by including api.php in get.php. There is no need to read the contents of api.php to parse the data.
get.php:
<?php
require 'api.php';
// api.php is included so $dbdata is in scope here
var_dump(json_encode($dbdata));
I have a sqlite db file which is definitely not corrupt since I can open it with SQLiteStudio.
However, when I try to open it dynamically with PHP with the following code I found in some tutorial:
class MyDB extends SQLite3
{
function __construct()
{
$this->open('../testDB');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
$sql ="SELECT * from testTable";
$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC) )
{
echo "ID = ". $row['id'] . "\n";
echo "NAME = ". $row['name'] ."\n\n";
}
echo "Operation done successfully\n";
$db->close();
I get the following result:
Opened database successfully
Warning: SQLite3::query() [sqlite3.query]: Unable to prepare statement: 11, database disk image is malformed in test.php on line 52
Fatal error: Call to a member function fetchArray() on a non-object in test.php on line 53
I found some threads like that one, but none of them had a definite answer.
Can somebody help me out here? Thanks in advance!
I had this same "database disk image is malformed" error and i solved it using SQLiteStudio.
Since you already have it, open the file, right click on the database and try the Vacuum option. After doing this, try the integrity check. If the result is 'OK' then your problem is solved. At least that's how i solved it. Vacuum rebuilds the entire database.
I've already the same problem with small SQLITE db.
Try to use:
$sql = "VACUUM";
$db->query($sql);
I have a My SQL database that has some tables. Now I want to extract the that data from a table and have it in XML format.
<?php
$username = "root";
$password = "";
$hostname = "";
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password,"examples" );
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else
echo "Connected to MySQL<br>";
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$root_element = "car"."s"; //fruits
$xml .= "<$root_element>";
//execute the SQL query and return records
$result = mysqli_query($dbhandle,"SELECT id, name,year FROM cars");
if (!$result) {
printf("Error: %s\n", mysqli_error($dbhandle));
exit();
}
if(mysqli_num_rows($result)>0) {
while($result_array = mysqli_fetch_assoc($result)) {
$xml .= "<car>";
//loop through each key,value pair in row
foreach($result_array as $key => $value) {
//$key holds the table column name
$xml .= "<$key>";
//embed the SQL data in a CDATA element to avoid XML entity issues
$xml .= "<![CDATA[$value]]>";
//and close the element
$xml .= "</$key>";
}
$xml.="</car>";
}
}
$xml .= "</$root_element>";
//send the xml header to the browser header
("Content-Type:text/xml");
//output the XML data
echo $xml;
//fetch tha data from the database
/*while ($row = mysqli_fetch_array($result)) {
echo $row['id']." ".$row['name']." ". $row['year'];
echo"<br>";
}*/
//close the connection
mysqli_close($dbhandle);
?>
however, I got the following error.
The XML page cannot be displayed Cannot view XML input using style
sheet. Please correct the error and then click the Refresh button, or try again later.
Invalid at the top level of the document. Error processing resource 'http://.... my%20portable%20files/mysqlxml.php'. ... Connected to MySQL<br><?xml version="1.0" encoding="UTF-8"?><cars><car><id><![CDATA[1]]>...`
could you help me please.
The
echo "Connected to MySQL";
part might be interfering with the XML output. See, when outputting XML you must output xml and nothing else: no spaces, no text, nothing before the initial XML tag (then your root node, then the rest).
Also, I don't see the headers being correctly output, try:
header("Content-Type:text/xml");
And also add your encoding, if you must.
If all fails, please, post a link so we can see the output and debug the problem.
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();
I have connected to a database for the first time with oop and stright away come up with an issue, below is my code which i'm struggling with:
$q = 'SELECT * FROM test';
$sqli->query($q);
if($sqli->query($q)){
echo "worked";
}
if($sqli->error){
echo $sqli->error;
}
I have checked for errors when connecting to the db and that works fine, but when I run this query I get no output, why? I expected an error or "worked", but have got neither.
Whats happening?
I have put some comments in the source code to help:
$q = 'SELECT * FROM test';
//$sqli is the result of a
//new mysqli("localhost", "user", "password", "database");
$resource = $sqli->query($q); // this returns a resource or false
if(!$resource) {
echo $sqli->error;
die; // do not process further
}
// process the results
$rows = $resource->fetch_all();
if ($rows) { // check if there are rows
echo "worked";
}
else {
echo "query is ok, but there are no rows";
}
You could also use $resource->fetch_object() which returns an object for output. Therefore if you wanted to print specific data from the result set, you would do something like
//table test.Name and test.Country
while ($rowobj = $resource->fetch_object()){
printf ("%s (%s)\n", $rowobj->Name, $rowobj->Country);
}
Good luck,
You could use this method, I hope it's what you are looking for. You will need to define the DB first. Then you can connect in OOP and test the connection is true or exit();
Let me know if this works for you. You can also define the DB in an external file and just do an include(); towards the top of your script for any pages needing connection to the DB.
define("SERVER","IP Address");
define("USER","DB USERNAME");
define("PASSWORD","DB PASSWORD");
define("DATABASE","DB NAME");
// This is for connection
$mysqli = new mysqli(SERVER, USER, PASSWORD, DATABASE);
if ($mysqli->connect_errno) {
echo "Connection to MySQL failed: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
exit();
}