Display first 50 characters only in php,mysql [duplicate] - php

This question already has answers here:
How do you pull first 100 characters of a string in PHP
(6 answers)
Closed 7 years ago.
I have two fields in database table, title and description. I am displaying tha data in php while loop.
My Code :
$sql = "select * from sightseeing";
$i = 1;
$res = mysql_query($sql, $conn);
if( mysql_num_rows($res) > 0) {
while($row = mysql_fetch_array($res))
{
echo "<tr>
<td>".$i++."</td>
<td>".$row["title"]."</td>
<td>".$row["description"]."</td>
</tr>";
}
}
I want to show only first 50 characters from description field. How to do that?

try this
$sql = "select * from sightseeing";
$i = 1;
$res = mysql_query($sql, $conn);
if( mysql_num_rows($res) > 0) {
while($row = mysql_fetch_array($res))
{
echo "<tr>
<td>".$i++."</td>
<td>".$row["title"]."</td>
<td>".substr($row['description'], 0, 50)."</td>
</tr>";
}
}

use MySQL LEFT
select *,LEFT(description , 50) description from sightseeing

Related

sql select from multiple tables - mysql [duplicate]

This question already has answers here:
MySQL JOIN tables with duplicate column names
(1 answer)
Ambiguous field name between an alias and a field in the select list with name
(3 answers)
Closed 12 months ago.
I have two tables currencies and markets, I want to use id, last, volume, bid, trade_status from markets table and only Symbol from currencies table.
when I using the bellow code as I have volume and id in both tables, that is multiply times showing
<?php
$con = mysqli_connect("localhost","Dotland","passwd","Dotland");
$response = array();
if($con){
$sql = "select * from markets, currencies";
$result = mysqli_query($con,$sql);
if ($result){
header("content-Type: JSON");
$i=0;
while($row = mysqli_fetch_assoc($result)){
$response[$i]['id'] = $row ['id'];
$response[$i]['symbol'] = $row ['symbol'];
$response[$i]['last'] = $row ['last'];
$response[$i]['volume'] = $row ['volume'];
$response[$i]['bid'] = $row ['bid'];
$response[$i]['trade_status'] = $row ['trade_status'];
$i++;
}
echo json_encode($response,JSON_PRETTY_PRINT);
}
}
else{
echo "Database Connection Failed";
}
?>

Die if Data Not Exisits in php mysql table [duplicate]

This question already has answers here:
PHP mysql_num_rows die error
(3 answers)
Closed 5 years ago.
I'm getting an error message when the data is not available in data base table. I would like to stop the qry search without getting an error when the data is not available.
It works good if the data is available in table but not works while the data is not available. For an example, when i search this city name in my table the requested city's data's are not available in that table. During this time i'm getting an "Undefined variable" error.
<?php
$city = 'chennai';
$cityQry ="SELECT * FROM area_data WHERE city ='$city' LIMIT 1";
$cityQryResult = mysql_query($cityQry);
while($row = mysql_fetch_array($cityQryResult)) {
$citypop = $row['population'];
}
?>
Check if mysql_num_rows gives you count more than 0, If count is 0 call die()
$city = 'chennai';
$cityQry ="SELECT * FROM area_data WHERE city ='$city' LIMIT 1";
$cityQryResult = mysql_query($cityQry);
if(mysql_num_rows($cityQryResult) == 0)
{
die("No Data Exists");
} else {
while($row = mysql_fetch_array($cityQryResult)) {
$citypop = $row['population'];
}
}
Please try Below Code (Not Tested Yet) :
<?php
$city = 'chennai';
$cityQry ="SELECT * FROM area_data WHERE city ='$city'";
$result = mysqli_query($cityQry);
// Mysql_num_row is counting table row
$count = mysqli_num_rows($result);
if($count > 0)
{
$cityQryResult = mysql_query($cityQry);
while($row = mysql_fetch_array($cityQryResult)) {
$citypop = $row['population'];
}
} else {
echo "No Data Exists";
}
?>

How do I add a No Results Found Message [duplicate]

This question already has answers here:
Check if a row exists using old mysql_* API
(6 answers)
Closed 6 years ago.
Trying to display a message, when there are no results found.
<?php
mysql_connect ("localhost", "root","") or die (mysql_error());
mysql_select_db ("test");
$term = $_POST['term'];
$sql = mysql_query("select * from testable where FName like '%$term%' or LName like '%$term%' or ID like '%$term%' ");
while ($row = mysql_fetch_array($sql)) {
echo '<br/>First Name:'.$row['FName'];
echo '<br/>Last Name:'.$row['LName'];
echo '<br/>Phone:'.$row['Phone'];
echo '<br/><br/>';
}
?>
Try this :
if(mysql_num_rows($sql) == 0)
{
echo 'No results';
}
else
{
while($row = mysql_fetch_array($sql))
{
//..
}
}
You can use mysql_num_rows() function, Which return the number of returned rows.
if( mysql_num_rows($sql) == 0) exit('No records found!');
passing the above condition meaning that there are some result, So you can loop through:
while ($row = mysql_fetch_array($sql)) {
// print whatever you want.
}

List all usernames from database from id [duplicate]

This question already has answers here:
MySQL returns only one row
(5 answers)
Closed 6 years ago.
I want to list all username from database if the id exists. That code works fine in database select * from users where id IN (1,2,3,4,5).
But in php it doesn't work. My php code is
$sql = "select * from users where id IN (1,2,3,4,5)";
$result = mysql_query($sql);
if ( mysql_num_rows($result) > 0 ) {
$row = mysql_fetch_assoc( $result );
echo $row["username"];
}
This php code only list one user name. I want to list all usernames. Sorry for my bad English. Thanks.
<?php
$sql = "select * from users where id IN (1,2,3,4,5)";
$result = mysql_query($sql);
if ( mysql_num_rows($result) > 0 ) {
while($row = mysql_fetch_assoc( $result )) {
echo $row["username"]."\n";
}
}
if you only fetch once, you will only get one row - obviously. so if you want all rows, you have to fetch as long as there are rows to fetch.

Counting each username in a table [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MYSQL COUNT GROUP BY question
$query = "SELECT * FROM product_id_1";
$result = mysql_query($query);
while( $record = mysql_fetch_array($result) ){
$array[] = $record['username'];
$unique_array = array_unique($array);
}
foreach( $unique_array as $list ){
echo $list.'<br/>';
}
Greeting,
I'm stuck trying to figure out how to apply array_count_unique(). Reason is I need to find out the count of each individual user in the table, i.e how many times adam appears and how many times abcd appears.
UPDATE
This is what I've done according to you guys's suggestion:
$query = 'SELECT username, COUNT(*) AS username_count FROM product_id_1 GROUP BY username';
$result = mysql_query($query);
while ($record = mysql_fetch_object($result)) {
echo $record->username;
echo $record->username_count;
}
This is exactly why we have RDBMS and SQL:
SELECT COUNT(1) AS username_count, username
FROM
product_id_1
GROUP BY
username

Categories