i would like to ask for an idea of how to fetch information from my database without getting everything in it. For example: i have an article that is consisted of 10 paragraphs in my database but i would only want to fetch 200 characters from it (so that i could have sustained information for my articles preview). Thanks in advance
currently i am using this code to fetch my data but it gives me everything from the database
$result = mysql_query("SELECT * FROM tblArticles Where id='".$_POST['num']."'");
$count=mysql_num_rows($result);
if($count>0)
{
$row = mysql_fetch_array( $result );
$id=$row[0];
$title=$row[1];
$contents=$row[2];
}
Try (make sure you change the field names in the SQL query to the actual field names):
$result = mysql_query("
SELECT id AS article_id,
article_title,
SUBSTRING(article_content, 1, 200) AS article_content
FROM tblArticles
WHERE id='" . mysql_real_escape_string($_POST['num']) . "'");
$count=mysql_num_rows($result);
if($count>0)
{
$row = mysql_fetch_assoc( $result );
$id=$row['article_id'];
$title=$row['article_title'];
$contents=$row['article_content'];
}
I've added the MySQL function SUBSTRING()
to the SQL query. This will retrieve a partial string from a whole string. For example, substring(field, 1, 100)
, will retrieve all the text between character 1 and character 100 from the field.
You should also avoid using MySQL_*
functions. There not recommended for use in new code. Instead, look into PDO or MySQLi
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.