I'm trying to get string from database which is url "http://www.google.com/"
but data I get changed to this http:\ /\ /www.google.com\ /
while($row = mysql_fetch_array($result)){
// temporary array to create single category
$tmp = array();
$tmp["id"] = $row["id"];
$tmp["name"] = $row["name"];
$tmp["url"]= $row["url"];
array_push($response["database"], $tmp);
}
how can I get the url without changed.
In your example the two pieces of data are identical, did StackOverflow reformat your data? Your code looks fine to me, perhaps it is a problem with how the data is inserted into the database rather than how it is retrieved. Have you looked at the data in an SQL browser like phpMyAdmin or SQLyog Community Edition to confirm the data is stored as you expect?
the stripslashes() builtin function would seem to do the trick.
I solve this problem
actually I want to create .json file, but when we use array and show data like url in php page .php the data will change because it's read as an html code, in json case we have to create another file with in file managment generated after getting data from database
$response = array(); $response["feed"] = array();
foreach($db->query('SELECT * FROM table') as $row) {
$tmp = array();
$tmp['id'] = $row['id'];
$tmp['name'] = $row['name'];
$tmp['url']= $row['url'];
array_push($response['table'], $tmp); }
//here the data posted into php page so the url change
echo json_encode($response);
//here create .json data and write data in data.json
$fp = fopen('data.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
Related
We have a simple php mysql json api to provide data to android app
We have a fix categories.
so we just check if category is this then fetch result from mysql database and convert in json
Everything is working fine. Fetching url from mysql
$stm_row = $stm->fetchAll(PDO::FETCH_ASSOC);
Till here all is working fine. data is coming from every category
Then, we convert data to JSON:
print(json_encode($stm_row));
But the problem is that only 1 catgory data is printing using json, rest category data showing blank
Can you please check what can be issue
If you want to loop through results ...e.g.
$stm_row = $stm->fetchAll(PDO::FETCH_ASSOC);
foreach ($stm_row as $row => $cat) {
echo $cat['category_name'];
}
OR If you have encoded JSON, need to decode first which will return an array...which you can loop through like ...e.g.
$stm_row = json_encode($stm_row);
$arr = json_decode($stm_row, true);
foreach ($arr as $row => $cat) {
echo $cat['category_name'];
}
i am giving you simple demonstration how to work with json.you can try like that
$data=array();
while($row = mysqli_fetch_array($query)) {
$data[] = $row['your field name'];
}
echo json_encode(array("response"=>$data));
i have done this code in php and it fetches every detail correctly apart from the images it shows "product_photo":null while there is an image.
any ideas why is null?
while ($row = mysql_fetch_object($qry_result)){
$result[] = $row;
}
$jsonstring = json_encode($result);
echo $jsonstring;
I am able to export database to csv but my code somehow imports twice the data to my csv file. I.e same column twice side by side.this is my code. I think my problem is with the implode statment. Any help would be appreciated.
<?php
$db = new sqlite3('I:\preethi\webbs.db');
$headers = array
('Id','CompanyId','DateTime','Serial','DeviceId','AgentAId','GpsAddress','Targa','CommonRoadDescription'
,'RoadCivicNumber','VehicleBrandDescription','VehicleModelDescription' ,'VerbaliVehicleTypeDescription','CommonColorVehicleDescription','VerbaliRuleOneCode','VerbaliRuleOneDes
cription','VerbaliRuleOnePoints'
,'VerbaliClosedNoteDescription','Points','VerbaliMissedNotificationDescription
','MissedNotificationNote','StatementNote');
$results = $db->query('select'.implode (',',$headers).'from VerbaliData');
//$results = $db->query( 'select
Id ,CompanyId ,DateTime ,Serial ,DeviceId ,AgentAId
,GpsAddress ,Targa ,CommonRoadDescription ,RoadCivicNumber ,VehicleBrandDescription
,VehicleModelDescription ,VerbaliVehicleTypeDescription ,CommonColorVehicleDescription
,VerbaliRuleOneCode ,VerbaliRuleOneDescription ,VerbaliRuleOnePoints ,VerbaliClosedNoteDescription
,Points ,VerbaliMissedNotificationDescription ,MissedNotificationNote ,StatementNote from
VerbaliData');
$fp = fopen('explores.csv', 'w');
fputcsv($fp,$headers);
while ($row = $results->fetchArray()) {
fputcsv($fp, $row);
}
fclose($fp);
?>
Just try with :
while($row = $results->fetchArray(SQLITE3_NUM)) {
Or
while($row = $results->fetchArray(SQLITE3_ASSOC)) {
More Details: http://php.net/manual/en/sqlite3result.fetcharray.php
You have a slight prob in your code fetchArray() returns two array sets one associative and one is numbered, use fetchArray(SQLITE3_NUM) or fetchArray(SQLITE3_ASSOC).
I am trying to write a Database Query to a file, But am just wondering how I get the SQL Data to be input into the file.
It is currently outputting nothing to the .txt file at all. I suspect its something to do with the while loop and am questioning whether it needs to be there or not.
My code is :
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
$query = "SELECT gamename, username, MAX(thescore)
FROM game_scores
GROUP BY gamename, username
ORDER BY gamename, thescore DESC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$scoredata = $row;
}
//save file
$handle = fopen('scores/games-backup'.'.txt','w+');
fwrite($handle,$scoredate);
fclose($handle);
echo "Success";
}
Any help on writing the SQL Results to this text file would be much appreciated.
$row is an array, you will need to make it a string before you can write it to a file, you can use implode().
$scoredata is also being overwritten in each loop, maybe use $scoredata[] instead
while($row = mysql_fetch_array($result)) {
$scoredata[] = implode("; ", $row);
}
This will make $scoredata an array also, so you will need to convert that to a string too!
$handle = fopen('scores/games-backup'.'.txt','w+');
fwrite($handle,implode("\r\n", $scoredata));
fclose($handle);
This should print each database row on a new line in the file.
EDIT: This will just be a text file, formatted as text, not that great for a backup file. You will need to structure the text into an SQL format to make it useful..
Try something like :
$handle = fopen('scores/games-backup'.'.txt','w+');
while($row = mysql_fetch_array($result)) {
fputs($handle, join(';', $row)."\n");
}
fclose($handle);
$row is an array, you must join it (or access the elements )
$row is not a string, you have to make it a string before you can put it into a file.
And you have to change $scoredata to $scoredata[] because, it is now being overwritten continiously.
You can use print_r function with second parameter set true.
$str = print_r($row, true);
fwrite($handle,implode("\r\n", $str));
Or you can serialize the array
$str = serialize($row);
fwrite($handle,implode("\r\n", $str));
I have done a little research on the following error Warning: [json] (php_json_encode) type is unsupported, encoded as nullbut have not found much in the way of answers?
I am trying to encode the result of a mysql query. Not sure what info I can provide.. but when I echo the results of the mysql data using
$data = json_encode($result);
echo $data;
while($info = mysql_fetch_array($result))
{
$content[] = $info;
}
$count = count($content);
for($i=0;$i<$count;$i++){
echo $content[$i]['Name'];
}
every thing shows as normal. Any help would be great.
What about:
$result=array();
for($i=0;$i<$count;$i++)
{
$result[]=$content[$i]['Name'];
}
echo json_encode($result);
mysql_fetch_array will return an array with both numeric and non-numeric values. That's fine for PHP, but it doesn't really play as well in other formats and it could very well be the cause of the issue. Have you tried mysql_fetch_assoc?
BIG EDIT
Just noticed your code above. You have :
$data = json_encode($result);
echo $data;
while($info = mysql_fetch_array($result))
That won't work. $result is a resource data type. It can't be serialized into something which can be read by json_encode. You have to create an array of its contents first and serialize that. Just making sure you know.