So i am fetching rows from my database using AJAX and then turning them into an array with a variable identifier Here is the code PHP:
$query_val = $_GET["val"];
$result = mysql_query("SELECT * FROM eventos_main WHERE nome_evento LIKE '%$query_val%' OR local_evento LIKE '%$query_val%' OR descricao_evento LIKE '%$query_val%'");
for($i=0;$i<mysql_num_rows($result);$i++){
$array = array();
$array[$query_val] = mysql_fetch_row($result); //fetch result
echo json_encode($array);
}
Here is the javascript:
$('#s_query').keyup(function(){
var nome = document.getElementById('s_query').value;
$.ajax({
url: 'search.php',
data: '&val='+nome,
dataType: 'json',
success: function(data)
{
console.log(nome);
var image_loc = data.nome[7];
console.log(image_loc);
If i change the line var image_loc = data.nome[7]; to var image_loc = data.nidst[7]; it works perfectly. Nidst is the term i search for.
This code returns the error:
"Uncaught TypeError: Cannot read property '7' of undefined".
What should i do?
mysql_*functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.