0

Here is the warning:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /var/www/angga/fungsi/fungsi-halaman.php on line 133

line 133 is $k = mysql_fetch_array($kata);

function tag($tagi){
$teks = explode(" ",$tagi);
    for($i=0;$i<count($teks);$i++):
    $teks2 = safe($teks[$i]);
    $kata = mysql_query("select * from `tag` where id=$teks2");
    $k = mysql_fetch_array($kata);
    echo "<a href=\"\">".$k['nama']."</a> ";
    endfor;

}

I am calling like this:

tag($post['tag']);

And output is :

test jancuk surem insert null value function ignore varchar 

original value from $post['tag'] is 1 2 3 4 5 6 7 8 9

I want to convert value from $post['tag'] and display it as name from tag table in my database

I was tried edit by add or die(mysql_error()); and warning is still out. here warning when I add or die(mysql_error());

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

What's wrong with my code ?

edit 1 : the problem is solve by add '' so now is like

 $kata = mysql_query("select * from `tag` where id='$teks2'") or die(mysql_error());

the answer is from first comment in my question, but now is gone or edited?

here the safe()

function safe($value){
   return mysql_real_escape_string($value);
} 
3118731
  • 77
  • 1
  • 2
  • 10
  • Your query may have a syntax error. So query does not fire and returns false. And you get that error – zkanoca Dec 19 '13 at 10:32
  • wow thanks. its work. no warning now. – 3118731 Dec 19 '13 at 10:33
  • What does `var_dump($teks2);` output if you add it just before the `mysql_query()` statement? I'm guessing your `safe()` function is manipulating the string in some weird ways. Can you show the function definition, too? – Amal Murali Dec 19 '13 at 10:33
  • Check what is value of $teks2 before you run your query, if is it empty it will cause sql syntax error – Sean Doe Dec 19 '13 at 10:34

3 Answers3

0

Your query may have a syntax error. So query does not fire and returns false. And you get that error: Try to quote the id value:

$kata = mysql_query("select * from `tag` where id='$teks2'");
zkanoca
  • 9,174
  • 9
  • 44
  • 90
  • This is the same as "select * from `'tag'` where id=$teks2" when you print it. – Sean Doe Dec 19 '13 at 10:40
  • if i'm not wrong, you first answer is in comment right ? you tell me to add `''` so the syntax is like this `mysql_query("select * from `tag` where id='$teks2'")` and its work. thanks. – 3118731 Dec 19 '13 at 10:48
0

You need to check the value returned by mysql_query() before trying to use it. If your query failed, it will return boolean false instead of a result object. If that happens, you can check what error occurred using mysql_error().

It's important to point out though that you're using a deprecated API. You should use the newer mysqli functions instead.

Peter Bloomfield
  • 5,400
  • 26
  • 35
0

Try with following query :

mysql_query("select * from tag where id=$teks2");
Nishu Tayal
  • 19,244
  • 8
  • 46
  • 96