-7

The following error is coming in my program

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\Users\joshi\Documents\EasyPHP-12.1\www\a.php on line 5

The code is as follows:

$con = mysqli_connect("localhost","root","","search");
$str = "Akshat";
$result = mysqli_query($con,"SELECT * from index WHERE name LIKE '%$str%'");
while($row = mysql_fetch_assoc($result))
{
  echo "Name: " . $row["name"];
  echo "<br>Desc: " . $row["desc"];
}
m59
  • 42,346
  • 14
  • 112
  • 132
Akshat Joshi
  • 132
  • 1
  • 7

2 Answers2

2

index is a reserved word http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

which needs to be wrapped with backticks

ie.: `index`

$result = mysqli_query($con,"SELECT * from `index` WHERE name LIKE '%$str%'");
                                           ^     ^

plus you are mixing mysql_ with mysqli_ functions.

You should have been using mysqli_fetch_assoc() instead of mysql_fetch_assoc() in conjunction with your query mysqli_query

While keeping uniformity with MySQL functions, it's one or the other; not both.

Rewrite:

<?php
$con = mysqli_connect("localhost","root","","search");
$str = "Akshat";
$result = mysqli_query($con,"SELECT * from `index` WHERE name LIKE '%$str%'");
while($row = mysqli_fetch_assoc($result))
{
echo "Name: " . $row["name"];
echo "<br>Desc: " . $row["desc"];
}
Funk Forty Niner
  • 74,372
  • 15
  • 66
  • 132
  • downvoter, explanation plz - I believe this being a valid answer. – Funk Forty Niner Dec 14 '13 at 17:06
  • And what does this (your link) have to do with OP using `SELECT * FROM index`? @Zarazthuztra `index` is a reserved word and should have been wrapped with backticks, which is clearly not shown in OP's code. – Funk Forty Niner Dec 14 '13 at 21:41
  • @Fred-ii- That's actually an excellent question. I have no idea why that's there. There was a reason when I posted it, but that reason is gone and I can't remember. I'll remove it for ya. Sorry for any confusion! – Zarathuztra Dec 15 '13 at 02:41
  • No problemo ;-) @Zarazthuztra – Funk Forty Niner Dec 15 '13 at 03:46
-1

Try

mysqli_fetch_assoc()

instead of

mysql_fetch_assoc()

Ref: http://www.php.net/manual/en/mysqli-result.fetch-assoc.php

Krish R
  • 22,188
  • 7
  • 49
  • 57
  • Why Downvote Mr.Downvoter? – Krish R Dec 14 '13 at 17:07
  • 1
    Because this is the nth duplicate of this Question, and you have enough rep and experience to vote to close instead of promoting poor researched posts... – brasofilo Dec 14 '13 at 17:08
  • Because I reviewed a bad edit: http://stackoverflow.com/review/suggested-edits/3582813, thanks for understanding. – brasofilo Dec 14 '13 at 17:16
  • Honestly, I don't think an answer shouldn't be downvoted if it's a dupe. I think the proper response should be to answer the question, as well as point out that it's already answered. But that's just MHO :) – Zarathuztra Dec 14 '13 at 17:21