PHP/mySQL get_file_contents and update the column - php

I have a mySQL db table. One of the table columns contains URLs which point to different xml files on a remote server.
My goal is to read each URLs info and write the xml content into another column on the same record (line) respectively.
In my PHP code, I am able to get the URL correctly from mySQL database and I am able to get the XML content on remote server into a variable correctly.
But the issue is while I write the content to my table line by line. Some XML columns got update correctly and some XML columns are empty.
I am pretty sure each time the variable got content correctly because I am able to print out each individual content on screen.
Why are some content updating the column and some don't. All the XML strings have the same format. If I copied that content and updated the mysql table manually, it successfully wrote into the table.
At beginning I thought it was time issue so I add enough sleep time for my PHP code. it does't help. then I suspected my db datatype, so I changed the XML
column data type from VCHAR to TEXT and even LONGTEXT. it does't help either. Does any one have a clue?
part of my php code below...
$result = mysqli_query($con,"SELECT url_txt FROM mytable ");
//work with result line by line:
while($row = mysqli_fetch_array($result)) {
echo $url_content = file_get_contents($row['url_txt']);
//debug line below *******************************/
echo $URL=$row[url_txt];
//debug line above********************************/
mysqli_query($con,"UPDATE mytable SET xml_info='$url_content' where url_txt = '$URL' ");
}

Maybe try converting your XML to a native array and working with it that way:
$array = json_decode(json_encode(simplexml_load_string($url_content)),TRUE);

Related

Replacing content of block after generating clones using cloneBlock - PHPWord

Please can somebody be so kind to show me the syntax for using cloneblock in phpword.
So Ive got data in a MySQL DB, and for the single rows that I need to import into my word doc via phpword it works fine....to run my query and search and replace with template processor. BUT, now I want to insert multiple rows into my word document. I've researched and found that the cloneblock method is the answer. However I cannot get it working....currently my code runs but it doesn't seem to get to the second row.
I actually dnt get any error messages. My code executes fine...but the end display word file doesn't display fine....and if you see my code I got an echo statement...which echo's out in my browser exactly what I want "damaged" &"good", (as an example given of one of the row data) but that data doesn't get pulled into my word doc like that...it duplicates "damaged" , "damaged". .
$group_key=1;
do {   
//loop to increase my uuid  - ($repeatgroup')
$repeatgroup = $id."/"."trailer_repeat_group"."[".$group_key."]";
// query string
$trailer_repeat_grouping = mysqli_query($connect, "SELECT * FROM trailer_repeat_group LEFT JOIN main on trailer_repeat_group.PARENT_KEY = main.metainstanceID WHERE trailer_repeat_group.KEY_id = '$repeatgroup'");
$templateProcessor->cloneBlock('CLONEME', $trailer_count);
while ($row1 = mysqli_fetch_array($trailer_repeat_grouping)) {   
//this echo below I am using to test exactly what happends – independent of
//PHPword/templateprocessor
echo $rttc =  $row1['right_trailer_tyre_condition'];
//inserting  / searching / inserting values
$templateProcessor->setValue("right_trailer_tyre_condition", $rttc);
}
// ending of loop / checking loop
$group_key++;
} while ($group_key <= $trailer_count);
I've done investigation and found the solution.
You're cloning same blocks N times:
$templateProcessor->cloneBlock('CLONEME', $trailer_count);
and then by doing fetch You're trying to replace right_trailer_tyre_condition with some value:
$templateProcessor->setValue("right_trailer_tyre_condition", $rttc);
Issue is that You're replacing all placeholders.
But in fact You need to replace them one by one with different values.
Solution is to define 3rd argument that means count of items to replace.
Simply change it to be:
$templateProcessor->setValue("right_trailer_tyre_condition", $rttc, 1);

Character Encoding issue in PHP and HTML

In database I have stored some ID's the values are something like "A001" OR "A-001". The value is perfectly stored in database but when I display them in browser it gets converted into numeric value 2.
How can I display the exact string on browser. I have tried with echo, print and var_dump()
If you can provide more information for replication, you would get good answers.
I doubt I understood your question.
Assuming database is MySQL, this code should work:
<?php
mysql_connect(YOUR_HOST,YOUR_ID,YOUR_PW);
$result = mysql_fetch_array(mysql_query("select ID from YOUR_TABLE where ID='A001'");
echo $result[0]; // A001 would be shown in browser
?>

How to display 'text' data correctly with Microsoft ODBC driver for Linux? Currently displaying as random characters

I am trying to display the contents of a field in a MS SQL database, the data type of which is 'text'. When I connect to the database with MS Excel on a PC the value looks something like:
Shipped on the: 18/10/12 Toll IPEC Connote Number: XXX XXX XXXX
When I connect to the database with PHP using the Microsoft ODBC driver for Linux, the output of the text field will display random characters, which are slightly different each time I run the exact same script. Here is what it output the last four times:
Xisep)!ØwXment.class.php))
5isep)!ment.class.php))
µ}isep)!Ø}ment.class.php))
t)!!Owner_IDt)Ø©Ø8
Not sure where it's getting the 'ment.class.php' bit from. That looks like the end of the name of one of my classes, but not one that is included in this particular script. Is the driver broken or what? All other data types (int, varchar, datetime etc) seem to display correctly, the problem only seems to happen with this one text field.
Here is the code:
<?php
require('ConnectWise.inc.php');
$config = new CW_Config;
// Connect to database
$dbcnx = odbc_connect("ConnectWise", $config->db_username, $config->db_password);
// Query database
$query = "select * from Billing_Log where Invoice_Number = '24011'";
$result = odbc_exec($dbcnx, $query);
while ($row = odbc_fetch_array($result)) {
echo $row['Top_Comment'] . "\n";
}
odbc_close($dbcnx);
Here is the output of my last few attempts:
\3ȶä!!¶äY!
öÈö§!!ö§Y!
&Èö×!!ö×Y!
Looks like you're getting an overflow. For some reason SQL is passing the length of your text field as 0, which to SQL means "long" but to PHP means "I dont know" and so PHP is showing whatever is in its memory at the location its expecting data (or something like that - any PHP experts care to explain?).
I've seen this with varchar(max) before but not text. (Converting to) Text is normally the way to fix it. So maybe I'm wrong.
Hope this is of some help. Im not a PHP developer, I just have to use it occasionally, and this sounds much like a painful experience I've gone through before :)

'fi' displaying incorrectly in database

I have a mySQL database with the following
e.g.
Microeconomics.
The ‘theory of the ï¬rm’
or:
Resource allocation modiï¬cations.
For some reason, the text that has been input (through CKEditor), has been changed so any instances of fi are in the database as 'ï¬'. I believe this is something to do with HTML entities. The text (I believe) was copy-pasted from a word document, which could be part of the problem.
How do I change (in PHP or mySQL) all instances of 'ï¬' into fi? When rendered as a PDF by TCPDF, it shows a ? (e.g. financial = ?nancial, significant = signi?cant)
Thanks in advance.
This statement will correct all the false instances for the column that contains the 'ï¬'
Update table Set
column = replace(column, 'ï¬', 'fi')

mysql query does not work on different files -php

i might be doing some idiot mistake, but i could not figure that out. i have some values coming from html and wanna insert into mysql db. problem is, the very same query does not work in regular php file (that includes other queries), but when i try on an independent php file, it does. here is a sample of the code:
$sql15="insert into body
(Article_ID, Article_Title)
values
('$article_id', '".$_POST['Article_Title']."') ";
mysql_query($sql15);
as i mentioned, the very same code works when i just copy this snippet to a new php file, and it works smoothly.. as you see, there are 20+ insert with the same php, because there are 25+ tables, but data is not much. first 14 query and following 7 queries do work by the way.
do you have any ideas?
There are some things to check and do.
Sanitize user input:
"('$article_id', '".mysql_real_escape_string($_POST['Article_Title'])."')";
You might also want to check if the value is what you expect.
Is your $article_id correct for column Article_ID?
Are your table and column names correct?
Check for errors:
$res = mysql_query($sql15);
if (!$res)
echo mysql_errno($link) . ": " . mysql_error($link);
Show us you complete query:
echo $sql15;
First of all i would suggest you to write your insert query like below
$sql15="insert into body SET Article_ID = '$article_id', Article_Title = '".$_POST['Article_Title']."'";
echo $sql15;
mysql_query($sql15);
so that each time when you add new column to database it would be easy for u to change insert query. echo your query and see it in browser. in it seems to o.k then copy it and paste it in SQL section under your phpmyadmin (see you are choosing proper database) and run it. if one row inserted successfully then your query is alright.
I hope this would help you a little.
$sql15="insert into body
(Article_ID, Article_Title)
values
('$article_id', '".$_POST['Article_Title']."') ";
mysql_query($sql15) or die(mysql_error());
use like this u will be get the error. then u will be find the issue
I think using mysql_real_escape_string may solve your problem.I also recommend you to store your form data in a string.
$article_title= mysql_real_escape_string($_POST['Article_Title']);
$sql15="insert into body
(Article_ID, Article_Title)
values
('$article_id', '$article_title') ";
mysql_query($sql15) or die(mysql_error());

Categories