Търсачка

Статус
Not open for further replies.

Hous

Registered
Код:
//if we got something through $_POST
if (isset($_POST['search'])) {
    // here you would normally include some database connection
    include('db_con.php');
    //$db = new db();
    // never trust what user wrote! We must ALWAYS sanitize user input
    $word = mysql_real_escape_string($_POST['search']);
    $word = htmlentities($word);
    // build your search query to the database
	$sql=mysql_query("SELECT pic_name,id FROM pictures WHERE pic_name LIKE '%" . $word . "%' ORDER BY pic_name LIMIT 10");
	 //$sql = "SELECT pic_name,id FROM pictures WHERE pic_name LIKE '%" . $word . "%' ORDER BY pic_name LIMIT 10";
    // get results
    while ($row=mysql_fetch_array($sql))
	//$row = $db->select_list($sql);
    if(count($row)) {
        $end_result = '';
        foreach($row as $r) {
            $result         = $r['pic_name'];
            // we will use this to bold the search word in result
            $bold           = '<span class="found">' . $word . '</span>';    
            $end_result     .= '<li>' . str_ireplace($word, $bold, $result) . '</li>';            
        }
        echo $end_result;
    } else {
        echo '<li>No results found</li>';
    }
}
от един input пращам данните и тук се прави проверка в дб-то ,обаче при опит да напиша дума която съществува примерно Pig и ми изписва
Search results :

Warning: Illegal string offset 'pic_name' in ...\search.php on line 19

която се пада
Код:
            $result         = $r['pic_name'];

някакви идеи защо така ?
 
vardump $r
Search results :
string(20) "pigs.gif"
Warning: Illegal string offset 'pic_name' in search.php on line 21
string(20) "pigs.gif"
Warning: Illegal string offset 'pic_name' in search.php on line 21
string(2) "34"
Warning: Illegal string offset 'pic_name' in search.php on line 21
string(2) "34"
Warning: Illegal string offset 'pic_name' in Csearch.php on line 21
p
p
3
3
Не разбирам защо ми излиза 20 20 и после 2 2 ... резултата трябва да е само pigs.gif

:lol:
 
Значи както сам виждаш $r не е array а е само името на картинката само по себе си. Оправи си го от

PHP:
$result         = $r['pic_name'];

на

PHP:
$result         = $r;
 
Статус
Not open for further replies.

Горе