I am trying to integrate an image with movie titles in my auto-complete text-box. For this, I have crawled list of movie titles and their poster links (link to the image of movie) in table "film_Posters" of mysql database.
Question:
Why I get null? Sorry if this question is asked many times, but I really couldn't fix it yet...

This is my php file:
<?php
if (isset($_GET['term'])){
$arr = array();
try {
$conn=new PDO('mysql:dbname=imdb;host=localhost', 'user', 'pass');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('SELECT DISTINCT movieName, posterLink FROM film_Posters WHERE movieName LIKE :term limit 0, 10');
$stmt->execute(array('term' => '%'.$_GET['term'].'%'));
while($row = $stmt->fetchAll(PDO::FETCH_ASSOC)) {
$arr[]=array('value'=>$row['movieName'],
'label'=>$row['movieName'],
'icon'=>$row['posterLink']);
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo json_encode($arr);
}
?>
and this is my html/javascript file:
<!DOCTYPE html>
<html>
<head>
<link href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<fieldset id = "q27"> <legend class="Q27"></legend>
<label class="question"> What are your favorite movies?<span>*</span></label>
<div class="fieldset content">
<div id="m_scents">
<label style="margin-bottom:10px;" for="m_scnts">
<input type="text" id="m_scnt" size="20" name="q27[]"
value="" placeholder="Enter text" />
</label>
</div>
</div>
</fieldset>
<script type = text/javascript>
(function($){
var $title = $('#m_scnt');
$(function () {
var arr = <?php echo json_encode($rows); ?>;
});
$title.autocomplete({
minLength: 2,
source: testfilmsauto.php,
focus: function( event, ui ) {
$title.val( ui.item.label );
return false;
}
});
$title.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
var $li = $('<li>'),
$img = $('<img>');
$img.attr({ //I AM NOT SURE ABOUT THIS PART
src: <?php echo $rows['posterLink']?>,
alt: <?php echo $rows['movieName']?>
});
$li.attr('data-value', item.label);
$li.append('<a href="#">');
$li.find('a').append($img).append(item.label);
return $li.appendTo(ul);
};
})(jQuery);
</script>
</body>
</html>
UPDATE:
Here is the updated php file with the answers from @meda and @marc:
<?php
if (isset($_GET['term'])){
try {
$conn=new PDO('mysql:dbname=imdb;host=localhost', 'user', 'pass');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('SELECT DISTINCT movieName, posterLink FROM film_Posters WHERE movieName LIKE :term limit 0, 10');
$stmt->execute(array('term' => '%'.$_GET['term'].'%'));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo json_encode($rows);
}
?>
Now, I don't see null message any more, but auto-complete doesn't work at all.. I think the problem is related to javascript part... I really appreciate any help.
$arr(injson_encode($arr)) come from in your HTML file?