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 "...";
}
Related
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);
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);
?>
This question already has answers here:
How to check if a string is one of the known values?
(4 answers)
Closed 3 years ago.
I'm trying to echo entries from a database that have a lend_status of 1 or 2 and have written my while loop to get the entries but I'm not sure if my IF statement is correct or if this is the best way to go about this. I'm very new to PHP and SQL so any help would be really appreciated!
I've tried using an echo to see where it breaks and it seems to be the IF statement. I know the connection to the database works as I can echo information from it and have tested that.
<?php
// WHILE LOOP TO LOOK THROUGH THE DIFFERENT ROWS
$STH = $DBH->prepare("SELECT * FROM laptop_system");
// DOESN'T HAVE TO BE AN ARRAY BUT GOOD PRACTICE TO PUT THIS IN (BELOW)
$STH->execute(array());
// WHILE LOOP TO LOOK THROUGH THE DIFFERENT ROWS
while ($row = $STH->fetch()) {
$lend_status = $row->lend_status;
$lend_id = $row->lend_id;
$requestee = $row->user_id;
$first_name = $row->first_name;
$last_name = $row->last_name;
$active_status = array(1,2);
if($lend_status == $active_status) {
echo 'hello';
?>
<div>
<?php echo $requestee_name . '\n'; ?>
<?php echo 'hi'; ?>
</div>
<?php }} ?>
The problem is that you are comparing a string / integer - one of the fields of your database row - to an array:
$lend_status = $row->lend_status;
...
$active_status = array(1,2);
if($lend_status == $active_status) {
If you want to check if the status is one of the values in the array, you can use for example in_array():
if (in_array($lend_status, $active_status)) {
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/>";
}
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));
?>