fetch specific data from database using php

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];

            }

Answers


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


Need Your Help

Handling Push Notifications when App is Terminated

ios objective-c iphone push-notification

When my App is not running and receives a Push Notification, if I click on that notification, the App is launched - but then it doesn't prompt the user with the Alert-View I set up, asking them whe...

How do I make a batch variable = to a command answer?

batch-file set

I have looked in quite a few places. Maybe I have not asked the right question, but I am now here. I am making a batch file to sense what the Operating system is.