0

I'm a newbie dealing with php:

I've a string like $string=admin which i wanna check in my database column named "username" including the values like: guest, admin, abc, def, .... Then wanna display a message if found the string within that column. I was doing something like:

$query=mysql_query("SELECT * FROM 'table_name' where 'username' like '$string'",$link); 
//where $link is connection to my database
$row = mysql_fetch_row($query);
echo $row;

I am getting the following error message when using the code above:

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\abc\def.php on line 22

I was also thinking to do something like:

$query = mysql_query("SELECT username FROM table_name",$link);
if ($query){
    while ($data=mysql_fetch_array($query)){
        echo $data['username'];
    }
}

Many thanks in advance for the guidance!

Funk Forty Niner
  • 74,372
  • 15
  • 66
  • 132
Adil
  • 19,538
  • 7
  • 29
  • 52
  • 1
    Remove the quotes around `FROM 'table_name'` and replace with backticks and do the same for `where 'username'` --- Your `like '$string'` could use `%` signs also. – Funk Forty Niner Jan 06 '14 at 19:28
  • Thanks Fred for quick reply but after following your suggestions, still getting the same error msg! – Adil Jan 06 '14 at 19:32
  • You'd most likely want to use `where username='$string'` only, without the LIKE. – Funk Forty Niner Jan 06 '14 at 19:35
  • When using `LIKE` it usually comprises of the `%` sign. Example: `WHERE field LIKE '%oom'` will find `room` and `boom` and `loom` etc. while using `WHERE field LIKE 'room%'` will find matches like `roomy` and `roomful` and `rooms` and using `%room%` will find just the word `room` – Funk Forty Niner Jan 06 '14 at 19:40
  • @Fred-ii- Thanks for your replies but sorry none of the suggestions exactly worked. – Adil Jan 06 '14 at 19:50
  • Your query would need to resemble something like this: `SELECT username FROM table_name WHERE username='guest' AND username='admin' AND username='abc'` --- You can also use `OR` instead of `AND` if you want to check if one of them is present in your table. – Funk Forty Niner Jan 06 '14 at 19:53
  • M Khalid Junaid: By following your given link, i had made these changes to my code: `$query=mysql_query("SELECT * FROM table_name where username like $muser", $link); if($query === FALSE) { die(mysql_error()); } while($row = mysql_fetch_array($query)) { echo $row['FirstName']; }` & my error has been replaced by a statement: > Unknown column 'admin' in 'where clause' Can you plz help me understanding this statement. I just wanna display the matched value. – Adil Jan 06 '14 at 19:59
  • This `like $muser` needs to be `like '$muser'` and `echo $row['FirstName']` is looking for a column named `FirstName` just to let you know how queries and loops work. @Bhaiya I for one, cannot help you anymore than I already have; good luck. Cheers – Funk Forty Niner Jan 06 '14 at 20:05
  • If you're new to PHP, please **DO NOT** use `mysql_query`. It's an antiquated method for accessing the database that's highly error-prone. At the very least [learn how to use PDO](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) properly because the code you have here is severely dangerous because of [SQL injection bugs](http://bobby-tables.com/). – tadman Jan 06 '14 at 20:48
  • Thanks a lot Fred for your help & Tadman for ur valueable suggestion! – Adil Jan 07 '14 at 05:35

3 Answers3

0

This will work for sure:

$squery="SELECT username FROM tablename WHERE username='$string'";
$sresult=mysql_query($squery);
$qry=mysql_fetch_assoc($sresult); 
echo $qry['username']; //in case if u want to print the result.
Funk Forty Niner
  • 74,372
  • 15
  • 66
  • 132
jsk5755
  • 11
  • 3
  • Do post properly formatted code next time you post an answer. I had to edit your answer. – Funk Forty Niner Jan 06 '14 at 20:52
  • I was not allow to properly comment the code to my own question before 8 hours so i commented it below ur comments in that format! – Adil Jan 07 '14 at 04:41
  • I am getting that warning after using these code lines: > Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in file_location on line 23 – Adil Jan 07 '14 at 05:45
0

Per the php reference: http://us2.php.net/mysql_query this function can possibly return simply FALSE. Check and see if your $query variable is returning false, you might not have permission to that table or are calling something funny.

bobkingof12vs
  • 650
  • 5
  • 22
0

The problem has been resolved by this query which searches for the string in the required database column & prints the matched value! Thanks to all!

$result=mysql_query("SELECT * FROM table_name where username ='".$muser."'", $link);
if($result === FALSE) {
    die(mysql_error());
}
while($row = mysql_fetch_array($result))
{
    echo $row['username'];
}

where 'username' is the column name.

Adil
  • 19,538
  • 7
  • 29
  • 52