Simple requirement: I want to store a flat unchanged XML strings into a MySQL-DB and then fetch the string itself via php with the tags (i.e. <tag>1234</tag>).
Problem: When I fetch the string, I get the values not the entire XML string.
I store <tag>1243</tag> (done via PHPmyAdmin) then I fetch (see code below) and echo the result I get 1234 not <tag>1234</tag>.
$query = "SELECT * FROM " . $my_table_with_flat_xml_string;
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$xml_flat_file_with_the_tags_please = $row[0];
echo " ( " . $tags. ")";
}
Help!
The String fetched from the database is <tag>1234</tag> as you put it into the database. MySQL does not interpret or parse XML input as long as you don't tell it to.
But you only echo it without escaping the < and >-chars, so your browser thinks, this would be a HTML-Element.
You can use something like the var_dump()-function to print out your fetched value or you escape the brackets by using the htmlspecialchars()-function.
Every node has a "name" (e.g. "tag") and a value (e.g. "1234", or perhaps "empty").
You've got the value.
You simply want to store the name, too. Twice.
By creating the string "<" . $name . ">" . $value . ""
There are several different ways to get $name and $value - it all depends on exactly how you're parsing your XML file.
Here's an excellent tutorial that gives you details on using XML from PHP:
http://www.ibm.com/developerworks/xml/library/x-xmlphp1/index.html
http://www.ibm.com/developerworks/xml/library/x-xmlphp2/index.html
http://www.ibm.com/developerworks/xml/library/x-xmlphp3/index.html
Related
i am trying to use echo inside url. i have store data from the form in database and now i am also fetching it on my page and its working well. Now i am trying to print that data i.e. number and date in url.
Is it possible and if possible please help me out
here is my data that i am fetching and it prints the output
echo $number;
echo $yyyymmdd;
and here is my url in which i want to insert ' echo $number; ' and ' echo $yyyymmdd; ' on the place of and .
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/<number>/date/<yyyymmdd>/");
I have also tried something like this but it gives error of syntex error.
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/"echo $number;"/date/"echo $yyyymmdd;"/");
Another way to add changing parameters to a URL (or string) is by using sprintf(). You define your URL and a type specifier like %d as a placeholder for numbers, and %s for strings. See the php doc for the full list of type specifiers.
$urlFormat = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/%d/date/%s/"
^ ^
Then call sprintf with the changing parameters in order of appearance.
$url = sprintf($urlFormat, $number, $yyyymmdd);
$json = file_get_contents($url);
This becomes more convenient especially if you are calling file get contents in a loop.
Create two variables and append those two inside double-quote or single quote, depending upon the quotes which you have opened and close it.
<?php
$number=123;
$yyyymmdd='2018-10-9';
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/".$number."/<number>/date/<".$yyyymmdd.">/");
?>
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/".$number."/date/".$yyyymmdd."/");
When you compose text, you do not need "echo" but just can write variable.
You can directly use variables in double quotes like this
file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/$number/date/$yyyymmdd/");
Sample code below
$number = 344;
$yyyymmdd = "20180301";
$url1 = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/$number/date/$yyyymmdd/";
echo "url1 ".$url1."\n";
$url2 = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/".$number."/date/".$yyyymmdd."/";
echo "url2 ".$url2. "\n";
I am simply trying to echo or print out specific data from a DB into a string (i hope thats the right name), which should be a very simple process as I've done it before. The point is everytime a user inserts information into the database this string echo's or prints out the inserted data.
But for some very odd reason this time around when i try to echo out the data, I literally get this.
Very frustrating. As you can see from the image above i have tried using 2 different ways to do this a variable and a session, but the echo literally just prints it out. I have done this before so i am aware that it is possible. I am just a little lost into how i am meant to achieve this or even better where i went wrong. I know how to do this using a different style of coding, but i am trying to keep everything uniformed (newbie).
$addon_name = $_SESSION['Add_On_OpName'];
mysqli_report(MYSQLI_REPORT_INDEX); //overrid a common php nonsense error
$prod_sel = $dbc->query("SELECT * FROM Add_On WHERE Add_On_OpName = '$addon_name'");
$prod_sel->data_seek(0);
while ($output = $prod_sel->fetch_assoc()) {
$prod_run .= $output['Add_On_OpName'] . $output['Prod_Name'] . $output['Add_On_Price'] . $output['Add_On_Select'] . '<br>';
$addon = $output['Add_On_OpName']; //echo out product name
$_SESSION['Prod_Name'] = $output['Prod_Name']; //echo out product desc
$_SESSION['Add_On_Price'] = $output['Add_On_Price']; //echo out price
echo '
<p>$addon . " " . $_SESSION["Add_On_Price"]; </p>
';
My session is started and the php file is connected to the DB.
I also have error handling which has not given out any error messages.
You must do:
echo "<p>$addon ".$_SESSION["Add_On_Price"]."; </p>";
A string encapsulated into ' is rendered just as it is.
Use " to render a string that contains variables. Example:
$a = 3;
$a++;
echo "the result is $a";
will result in the result is 4.
On the other hand,
echo 'the result is $a';
gives the result is $a.
As the documentation points out:
Single quoted ¶ The simplest way to specify a string is to enclose it
in single quotes (the character ').
Doued ¶
If the string is enclosed in double-quotes ("), PHP will interpret
more escape sequences for special characters
Try not mix it..
And if within double quotes you have an associative array you may concat.
echo "string $variable". $array["index"];
or
echo "string $variable {$array["index"]}";
Then your code should look like
$addon_name = $_SESSION['Add_On_OpName'];
mysqli_report(MYSQLI_REPORT_INDEX); //overrid a common php nonsense error
$prod_sel = $dbc->query("SELECT * FROM Add_On WHERE Add_On_OpName = '$addon_name'");
$prod_sel->data_seek(0);
while ($output = $prod_sel->fetch_assoc()) {
$prod_run .= $output['Add_On_OpName'] . $output['Prod_Name'] . $output['Add_On_Price'] . $output['Add_On_Select'] . '<br>';
$addon = $output['Add_On_OpName']; //echo out product name
$_SESSION['Prod_Name'] = $output['Prod_Name']; //echo out product desc
$_SESSION['Add_On_Price'] = $output['Add_On_Price']; //echo out price
echo "<p>$addon {$_SESSION["Add_On_Price"]}; </p>'";
}
Long time ago
I never use double quotes due to it require parse the whole string for special notations. However it.
Try not mix single quotes with double quotes. pick up a standard for you code you will not notice any difference than is easy to code and read without surprises
You're mixing single quotes and double quotes. Single quotes do not perform interpolation of variables so when you write this:
echo '... whatever including " char and $ sign';
PHP will just literally print everything inside.
You forget some ' or " !
echo '<p>' . $addon . ' ' . $_SESSION["Add_On_Price"] . '</p>';
Use double quotes
echo "<p>$addon $_SESSION['Add_On_Price']; </p>";
I have used foreach loop to extract multiple value of user_phone between tab but it will produce error.I dont what is exact formate.
$result = $Admin->select($Admin->newsletter_subscribers,'',"");
print_r($result['user_phone']);
$data="<message-submit-request>
<username>#######</username>
<password>#######</password>
<sender-id>$$$$$$</sender-id>".
foreach($result as $row)
{
"<to>".$row['user_phone']."</to>"
}."<MType></MType>
<message-text>
<text>hi test message 1</text>
</message-text>
</message-submit-request>";
Try this:
$result = $Admin->select($Admin->newsletter_subscribers,'',"");
print_r($result['user_phone']);
$data = "<message-submit-request>
<username>#######</username>
<password>#######</password>
<sender-id>$$$$$$</sender-id>";
foreach($result as $row)
{
$data .= "<to>".$row['user_phone']."</to>";
}
$data .= "<MType></MType>
<message-text>
<text>hi test message 1</text>
</message-text>
</message-submit-request>";
The exact format for string concatenation is shown in the PHP manual under string operators. However I won't elaborate on these directly becasue cerating XML by concatenating strings is a bad idea. For example having characters like <, > and & in your input, you'll run into many problems.
For your use-case SimpleXMLElement is a handy object that allows you to do the same in a much more consisted manner:
// create XML document based on boilerplate
$xml = new SimpleXMLElement('
<message-submit-request>
<username>#######</username>
<password>#######</password>
<sender-id>$$$$$$</sender-id>
<MType></MType>
<message-text>
<text>hi test message 1</text>
</message-text>
</message-submit-request>
');
// add elements where appropriate
foreach($result as $row)
{
$xml->addChild('to', $row['user_phone']);
}
This will ensure that all values are properly encoded. Additionally this can have a magnitude on benefits when you further process the data, e.g. outputting it.
However you haven't shown in your question what follows up with $data so you need to know how to obtain the XML from the SimpleXMLElement as string to make the example complete. Here it is:
$data = $xml->asXML();
See as well the SimpleXML Basic Usage guide in the manual if you'd like to learn more.
I want to store strings in a TEXT column in MySQL.
The strings mostly consists of which is the main problem, MySQL (and CodeIgniter) converts them to spaces. And they are important because the string only makes sense with the appropriate amount of spacing.
I considered converting to <div class='whitespace'></div> so that I can style with CSS, but it will make the MySQL column bigger.
So how can I efficiently store such a string in MySQL database?
Thanks for any help !
Use htmlentities (http://www.php.net/htmlentities)
echo htmlentities('Hi there');
// outputs
// Hi there
echo 'Hi there';
// outputs
// Hi there
If you want to decode instead (the reverse) you can use html_entity_decode().
http://www.php.net/manual/en/function.html-entity-decode.php
Consider tobase64_encode the data before inserting it in mysql.
$bigstr = base64_encode($bigstr);
/* insert to database */
Remember to base64_decode when fetching the string back.
Create two functions:
function getRawString($str) {
return strtr($str, array(" " => " "));
}
function getDisplayString($str) {
return strtr($str, array(" " => " "));
}
Store the raw string in the database:
$str = file_get_contents('html_file.html');
$query = "INSERT INTO table_name (my_text) VALUES ('" . getRawString($str) . "')";
// Execute query...
Fetch it from the database and display it:
echo getDisplayString($strFromDB);
I am trying to write a php function that goes to my database and pulls a list of URLS and arranges them into an xml structure and creates an xml file.
Problem is, Some of these urls will contain an ampersand that ARE HTML encoded. So, the database is good, but currently, when my function tries to grab these URLS, the script will stop at the ampersands and not finish.
One example link from database:
http://www.mysite.com/myfile.php?select=on&league_id=8&sport=15
function buildXML($con) {
//build xml file
$sql = "SELECT * FROM url_links";
$res = mysql_query($sql,$con);
$gameArray = array ();
while ($row = mysql_fetch_array($res))
{
array_push($row['form_link']);
}
$xml = '<?xml version="1.0" encoding="utf-8"?><channel>';
foreach ($gameArray as $link)
{
$xml .= "<item><link>".$link."</link></item>";
}
$xml .= '</channel>';
file_put_contents('../xml/full_rankings.xml',$xml);
}
mysql_close($con);
session_write_close();
If i need to alter the links in the database, that can be done.
You can use PHP's html_entity_decode() on the $link to convert & back to &.
In your XML, you could also wrap the link in <![CDATA[]]> to allow it to contain the characters.
$xml .= "<item><link><![CDATA[" . html_entity_decode($link) . "]]></link></item>";
UPDATE
Just noticed you're actually not putting anything into the $gameArray:
array_push($row['form_link']);
Try:
$gameArray[] = $row['form_link'];
* #Musa looks to have noticed it first, for due credit.
Look at this line
array_push($row['form_link']);
you never put anything in the $gameArray array, it should be
array_push($gameArray, $row['form_link']);
You need to use htmlspecialchars_decode. It will decode any encoded special characters in string passed to it.
This is most likely what you are looking for:
http://www.php.net/manual/en/function.mysql-real-escape-string.php
Read the documentation, there are examples at the bottom of the page...
'&' in oracleSQL and MySQL are used in queries as a logical operator which is why it is tossing an error.
You may also want to decode the HTML...