0

I want to select all the rows that have $name but it only shows the first row. My table is like this

id 
name
password
email

Code :

$result = mysql_fetch_assoc(mysql_query("SELECT * FROM  data WHERE username ='$name'"));

print_r($result);

This is not working, it only shows the first row.

3
  • 1
    mysql is deprecated, you should look into mysqli or PDO. Commented Apr 27, 2015 at 9:43
  • did u checked that there are more than one entry for that username you have entered Commented Apr 27, 2015 at 9:49
  • yes sir,it fixed now $query= mysql_query("SELECT * FROM data WHERE username ='$name'"); $finalRes = array(); while($result = mysql_fetch_assoc($query)) { $finalRes[] = $result; } print_r($finalRes); Commented Apr 27, 2015 at 9:59

3 Answers 3

1

Better to use in this way

$query= mysql_query("SELECT * FROM data WHERE username ='$name'");
$finalRes = array();
while($result = mysql_fetch_assoc($query))
{
   $finalRes[] = $result;
}
print_r($finalRes);
Sign up to request clarification or add additional context in comments.

Comments

0

You should look errors:

<?php

$result = mysql_query("SELECT * FROM  data WHERE username = '$name'");
if (!$result) {
    var_export(mysql_error());
}
$data = [];
while ($row = mysql_fetch_assoc($result)) {
    $data[] = $row;
}
var_export($data);

and don't forget about sql-injections...

PDO:

<?php

try {
    $dbh = new PDO('mysql:dbname=test;host=127.0.0.1', 'root');
} catch (PDOException $e) {
    echo 'Connection failed: '.$e->getMessage();
}
$name = 'yourName';
$sth = $dbh->prepare('SELECT * FROM  data WHERE username = :name');
$sth->bindParam(':name', $name, PDO::PARAM_STR);
if (!$sth->execute()) {
    throw new Exception($sth->errorInfo());
}
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
var_export($result);

5 Comments

1) "SELECT * FROM data WHERE username = '$name'" - it's a hole. Write this - it's bad practice... but problem not in this... 2) I suppose: if error occurs it should be processed... var_export(mysql_error());
ok,i will look more into how to prevent sql injection,for now will just put var_export(mysql_error()); after each query.
I recommend to you use PDO!) And be sure that your user really exists at database)
alright PDO it is then,is it more secure or something?
yes! secure, performance, and much other... I added pdo example, looks almost the same, but more powerful...
0

Try like this

    $sql = "select * from data where username='$username'";
    $res = mysql_query($sql);
    while ($row = mysql_fetch_array($res))
   {
   print_r($row);
   }

i have changed the code try this loop so that all the data will print,if not let us know

1 Comment

it still show me only first row // Array ( [0] => 9 [id] => 9 [1] => anis [username] => anis [2] => test [email] => test [3] => test [password] => test )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.