php - Select from database and store in an array -
i want select data db , store in array. suppose have column "keyword" in db table. want select rows keyword column "nature".
i trying following code:
<? $term= "nature"; $arr = array(); $sql = "select keyword keywords keyword '%$term%'"; $result = mysql_query($sql) or die(mysql_error()); $rows = mysql_fetch_array($result); foreach ($rows $row){ array_push($arr, $row['keyword']); } print_r($arr); //output: array ( [0] => n [1] => n ) ?>
so result db should return 1 keyword 'nature' need store in array.
- why storing same string 2 times? there no other row in db similar term nature.
- why storing first letter in array? shouln't store "nature" instead of "n"?
please me fixing this.
should like
$term = "nature"; $arr = array(); $sql = "select keyword keywords keyword '%$term%'"; $result = mysql_query($sql) or die(mysql_error()); while( $row = mysql_fetch_assoc( $result ) ) { arr[] = $row[ 'keyword' ]; }
in solution fetch first record result-set numeric indexed array.
btw - aware like-query starting wildcard cannot make use of index?
Comments
Post a Comment