Hi frnds....

i have a colmn in database like CONTENT ..it contains 20 lines of of data....but i need to select only 5 lines of data to a single variable......


plz tell me how can i do?

Recommended Answers

All 9 Replies

Check mysql function substring_index

Eg.

SELECT SUBSTRING_INDEX(columnname, "\n", 10 )
FROM table

Check mysql function substring_index

Eg.

SELECT SUBSTRING_INDEX(columnname, "\n", 10 )
FROM table

hi Naveen..

$blog="select sno,title,name,SUBSTRING_INDEX(content, "\n", 5 ) from blogs limit 1";

when i using the above query i got this error..plz resolve this one...


Warning: Unexpected character in input: '\' (ASCII=92) state=1 in ....

and
Parse error: syntax error, unexpected T_STRING in....

Use single quote in your query ' instead of ". The second argument \n is a delimiter. If you are storing the values in a table with <br> try giving that instead of \n. I tested at my localhost and it works without any problem.

<?php
$con = mysql_connect("localhost","root");
mysql_select_db("test");
$blog="select id,SUBSTRING_INDEX(question, '\n', 5 ) from quiz_questions limit 1";
$result = mysql_query($blog) or die (mysql_error());
$row = mysql_fetch_array($result,MYSQL_ASSOC);
print "<pre>";
print_r($row);
print "</pre>";
?>

:) Get a good editor with syntax highlighting.. That way you will know where you are going wrong!

Use single quote in your query ' instead of ". The second argument \n is a delimiter. If you are storing the values in a table with <br> try giving that instead of \n. I tested at my localhost and it works without any problem.

<?php
$con = mysql_connect("localhost","root");
mysql_select_db("test");
$blog="select id,SUBSTRING_INDEX(question, '\n', 5 ) from quiz_questions limit 1";
$result = mysql_query($blog) or die (mysql_error());
$row = mysql_fetch_array($result,MYSQL_ASSOC);
print "<pre>";
print_r($row);
print "</pre>";
?>

:) Get a good editor with syntax highlighting.. That way you will know where you are going wrong!

Hi Naveen...Thank U 4 giving reply...But,my problem not solved...may its my mistake..plz check this one...

<?php 
	$blog="select sno,title,name,SUBSTRING_INDEX(content, '\n', 5 ) from blogs limit 1";
	$brow=mysql_query($blog) or die(mysql_error());
	$bres=mysql_fetch_array($brow);
	$bid=$bres['sno'];
	$btitle=$bres['title'];
	$bname=$bres['name'];
	$bcontent=$bres['content'];
	?>

here i got everything.. but the content is NULL....

and also in ur query execution what is MYSQL_ASSOC ?

As I said in my earlier post, \n is just a delimiter. How do you know it contains 20 lines of of data ? How are you separating the lines ? Using \n or by using <br> ?
Try this simple example.

$query = "select content from blogs limit 1";
$result = mysql_query($query);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
print htmlentities($row['content']);
?>

If the lines are separated by <br> tag, use that as a delimiter.
Btw, I use mysql_assoc to get only the associative indices. I can use mysql_assoc instead, but I like using mysql_fetch_array. :)
Check Return Values in this link for more details.
http://in.php.net/mysql_fetch_array

As I said in my earlier post, \n is just a delimiter. How do you know it contains 20 lines of of data ? How are you separating the lines ? Using \n or by using <br> ?
Try this simple example.

$query = "select content from blogs limit 1";
$result = mysql_query($query);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
print htmlentities($row['content']);
?>

If the lines are separated by <br> tag, use that as a delimiter.
Btw, I use mysql_assoc to get only the associative indices. I can use mysql_assoc instead, but I like using mysql_fetch_array. :)
Check Return Values in this link for more details.
http://in.php.net/mysql_fetch_array

Hi Naveen....

here when i inserting data from a text area as copy and paste..It looks like paragraphs...when i echo the total content in my web page...i saw the source code, then there is no <br>...it looks totally as paragraphs....

Sry 4 distrubing u more times..

Then use <p> instead :-/ It would be very helpful if you show us the output !

Then use <p> instead :-/ It would be very helpful if you show us the output !

Hi Naveen...

Thank U Sooo much....

Finally I got it....by using this way....

$bcontent=nl2br($bres['content']);
	$bc=explode("<br />",$bcontent);
echo $bc['0'];

Once Again Thank U.....

Great :) Congrats!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.