Json database links [duplicate] - php

This question already has answers here:
json_encode() escaping forward slashes
(4 answers)
PHP, why do you escape my quotes? [duplicate]
(6 answers)
Closed 7 years ago.
Hello guys im with a problem handling PHP and JSON
This is my php code
<?php
mysql_connect("127.0.0.1","root","");
mysql_select_db("spadramatico_db");
$query = mysql_query("SELECT * FROM feed_table ORDER BY id");
$records = array();
while($obj = mysql_fetch_object($query)) {
$records [] = $obj;
}
print (json_encode($records));
?>
And this is the output result:
[{"id":"1","title":"Teste Title","image":"http:\/\/catalinaseaspa.com\/wp-content\/uploads\/2015\/03\/island-girl.jpg","desc":"Desc test","price":"1"}]
My Problem its with the link, the output its like this:
http:\/\/catalinaseaspa.com\/wp-content\/uploads\/2015\/03\/island-girl.jpg
But its to be like this:
http://catalinaseaspa.com/wp-content/uploads/2015/03/island-girl.jpg
How can i fix that?
Thank you :D

try this code. Add the JSON_UNESCAPED_SLASHES to the function.
<?php
mysql_connect("127.0.0.1","root","");
mysql_select_db("spadramatico_db");
$query = mysql_query("SELECT * FROM feed_table ORDER BY id");
$records = array();
while($obj = mysql_fetch_object($query)) {
$records [] = $obj;
}
print (json_encode($records,JSON_UNESCAPED_SLASHES));
?>

Related

How do I get a value outside the loop using PHP [duplicate]

This question already has answers here:
How to get comma separated values from database [duplicate]
(3 answers)
Converting MySQL results into comma separated values
(4 answers)
Can I concatenate multiple MySQL rows into one field?
(16 answers)
Closed 3 years ago.
I'm creating a small website using PHP, which is generally a site for showcasing the hospital, and I modified the code given in this example:
https://www.w3schools.com/php/php_mysql_select.asp
<?php
$query = "SELECT * FROM emp WHERE type = 'woman' ";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$cat = $row["cat"] . ',';
echo $cat;
////<---- echo on while (Loop)
}
}
The expected output would be as follows:
Output: 35,36
But I changed the code with the link above and it is as follows:
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$cat = $row["cat"] . ',';
}
echo $cat;
///// <---- echo Out of While (Loop)
}
Output: 35
My expecting output would be 35, 36 outside of "while" using "echo".
What code do you recommend to output "35,36" the same code above?
You can try the below code to achive your requirement
$data = array();
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result)) {
$data[] = $row["cat"];
}
}
echo implode(",",$data);

SOLVED: PHP json_encode returns BLANK when MySQL data contains accents [duplicate]

This question already has answers here:
json_encode is returning NULL?
(10 answers)
UTF-8 all the way through
(13 answers)
Closed 3 years ago.
Sorry if this has been asked before but I could not find a solution.
My PHP script returns blank when the data contains an accent and I get no error. How to solve this, please?
My collation is ut8_general_ci.
The server uses PHP 7.
Here is the code:
include ("connectDB.php");
$q = "select id, nom from atelier WHERE session = '2'";
$sql = $mysqli->query($q);
$data = array();
while($row = mysqli_fetch_array($sql, true)){
$data[] = $row;
};
header('Content-Type: application/json');
echo json_encode($data);
Here is the structure of the table:
EDIT: here is the working code
<?php
include ("connectDB.php");
mysqli_set_charset($mysqli,'utf8');
$q = "select id, nom from atelier WHERE session = '3' ORDER BY nom";
$sql = $mysqli->query($q);
$data = array();
while($row = mysqli_fetch_array($sql, true)){
$data[] = $row;
//echo json_last_error(); // returns 5 ?
};
header('Content-Type: application/json');
echo json_encode($data,JSON_UNESCAPED_UNICODE);
?>

The array does not show the information echo [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
No I'm doing wrong, I am modifying a system created by someone else but I can not show the information ... This is the code ...
$db = DB::getInstance();
$id = 1;
$query = $db->query("SELECT * FROM users WHERE id = ?", array($id));
$x = $query->results();
echo $x;
The error: Notice: Array to string conversion in...
Try using var_dump (instead of echo) if the return value is an object. For example:
var_dump($x);
use print_r($x) instead of echo $x
echo is used to print string and numbers but could not print array,
you may use var_dump too ... actually var_dump is used to print objects
use FOR :
$db = DB::getInstance();
$id = 1;
$query = $db->query("SELECT * FROM users WHERE id = ?", array($id));
$x = $query->results();
for ($i=0;$i<count($x); $i++){
echo $x[$i]."<br/>";
}

PHP MYSQLi: Array to string conversion [duplicate]

This question already has answers here:
Notice: Array to string conversion in
(6 answers)
Closed 7 years ago.
I want to get the id of a table and make it a variable so I can use it in a link.
But I am getting Notice: Array to string conversion in .
$getpID = "SELECT id from posts";
$res = mysqli_query($conn, $getpID) or die(mysqli_error());
$pid = mysqli_fetch_assoc($res);
<a id='del' href='deletepost.php?del=$pid'>Delete</a>
This is just a part of the code where I have problems.
You need to do like this:-
<?php
$getpID = "SELECT id from posts";
$res = mysqli_query($conn, $getpID) or die(mysqli_error($conn));
while($pid = mysqli_fetch_assoc($res)){
echo "<a id='del' href='deletepost.php?del=$pid['id']'>Delete</a><br/>";
}
Note:- this is because $res is an result-set array object having 1 or more than value. So you need to iterate like this.
$pid is an array, you need to access the ID from the array then you can use it in the link.
Example:
$id = $pid['id'];
<a id="del" href="deletepost.php?del=$id">Delete</a>

Trim a content from database? [duplicate]

This question already has answers here:
Truncate a multibyte String to n chars
(4 answers)
Closed 9 years ago.
I am calling all names from a mysql database using php. Some names are very large. so i want to show first 7 charectors and put "..."
Here is my code:
<?php $result = mysql_query("SELECT * FROM Persons WHERE section='one' ORDER BY FirstName");
while($row = mysql_fetch_array($result))
{
$fst = $row['FirstName']; ?>
Any suggestions please?
Try like
if(strlen($row['FirstName']) > 7)
$fst = substr($row['FirstName'],0,7).'....';
try this:
if (strlen($fst) <7) {
echo $fst;
}
else{
echo substr($fst,0,7); echo "...";
}

Categories