$a1 = "SELECT PHONE, NAME FROM table WHERE NAME LIKE %$name%"; $a2 = mysql_query ("$a1"); $a3 = @mysql_fetch_array ($a2); if (!$a3) {echo "NOT FOUND";} echo "$a3[0]<br>"; echo "$a3[1]<br>";
This code will not work. Can anyone see a problem with it?
hmm why are you using $a3 = @mysql_fetch_array ($a2);
try with $a3 = mysql_fetch_array($a2);
also you should use single quotes in your query:
$a1 = "SELECT phone, name FROM table WHERE (name LIKE '%$name%')";
The @ hides the ugly mysql error message, if it does not find a match.
I'll give your version with the single quotes a try...
$a1 = "SELECT PHONE, NAME FROM table WHERE (NAME LIKE '%$name%')"; $a2 = mysql_query ("$a1"); $NUMROWS = @mysql_num_rows($a2); if (!$NUMROWS) {$notfound=1;} if ($NUMROWS) { $I = 0; while ($I < $NUMROWS) { $a3 = @mysql_fetch_array ($a2); echo "$a3[0] $a3[1]<br>"; } }
Why doesn't this work????? When you put single quotes in a variable doesn't the $a2 see it as '%$name%' and not with the variable name?
try this one:
$a1 = "SELECT PHONE, NAME FROM table WHERE NAME LIKE '%".mysql_escape_string($name)."%'"; $a2 = mysql_query ("$a1"); if ($a3 = mysql_fetch_array ($a2)) { echo "<p>"; echo "$a3[0]<br>"; echo "$a3[1]<br>"; echo "</p>\n"; } else echo "NOT FOUND";
It only shows the first one found. 🙁 I need it to show all matches.
See if this works:
$a1 = "SELECT `phone`, `name` FROM `table` WHERE `name` LIKE '%" . $name . "%'"; $a2 = mysql_query ($a1); if ($a3 = mysql_fetch_array ($a2)) { echo "<p>"; echo "$a3[0]<br>"; echo "$a3[1]<br>"; echo "</p>\n"; } else { echo "NOT FOUND"; }
It's minor changes like using backquotes (`) for the field names and the table.
oook... then try this one 🙂
$a1 = "SELECT PHONE, NAME FROM table WHERE NAME LIKE '%".mysql_escape_string($name)."%'"; $a2 = mysql_query ("$a1"); if (mysql_num_rows($a2)==0) echo "NOT FOUND"; else while ($a3 = mysql_fetch_array ($a2)) { echo "<p>"; echo "$a3[0]<br>"; echo "$a3[1]<br>"; echo "</p>\n"; }
THANK YOU!!! 😃