-1

I have an MySQL table with a field which is filled with a sentence and I'd like to get only the first word of the sentence. After I've the first word I'd like to email out it with a form.

  $result = mysql_query("SELECT SUBSTRING_INDEX(`sentence_field`, ' ', 1) AS `firstWord` FROM `table`");
  $iresult = $_POST[$result];

Of course it doesn't work. I don't know why. How should I use the $_POST in PHP?

Wendy
  • 1
  • 2
  • 1
    RTFM: http://php.net/mysql_query. It returns a statement HANDLE, not the value you're requesting in the query. You need to `fetch` a row first. – Marc B Dec 19 '14 at 22:10
  • Thank you. My knowledge is really limited when about mysql and php. – Wendy Dec 19 '14 at 23:14

1 Answers1

1

First, please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO or MySQLi.

Second, you're not returning anything from the query. It will not be in the $_POST array. You need to get the info in the result, one way is by fetching an array -

$result = mysql_query("SELECT SUBSTRING_INDEX(`sentence_field`, ' ', 1) AS `firstWord` FROM `table`");
while ($row = mysql_fetch_assoc($result)) {
    print_r($row);
}
Community
  • 1
  • 1
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119