开发者

MYSQL LEFT(column, 100) and PHP {$row[(column)]}

I have a mySQL table with a text field named "body" and a title field named "title". I want to choose the first 100 characters of the text field "body" by like this:

$query="SELECT title, LEFT(body, 100) FROM table";
$result = mysql_query($query);

and then output the result in a web page:

while ($row = mysql_fetch_array($result)){
  echo "...html... {$row["title"]} ...html... {$row["body"]} ...html...";
}

but I get an error: "Notice: Unde开发者_StackOverflow社区fined index: body in C:\wamp\www\index.php on line 299" Can somebody help? Thanks.


You need to rename your field with an alias when you apply functions like LEFT() to it, otherwise you have no easy way to reference it by name.

SELECT title, LEFT(body, 100) AS body FROM table
                              ^^^^^^^ give name to field

Now your reference to {$row["body"]} should work correctly.


You have to name your field or access it with LEFT(body, 100) if you apply some function:

$query = "SELECT title, LEFT(body, 100) as body FROM table";


Doing that creates an unnamed column. Just add an alias:

$query="SELECT title, LEFT(body, 100) AS BODY FROM table";
$result = mysql_query($query);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜