1

Problem: I am learning PHP and I cant figure out how to display all rows in my table. Right now it only shows the info from which the user logged in. I am not sure how where to start or how to do this. I know while loops could be helpful but not sure how to use it in this context. Im pretty new to all this.

What I Tried

I removed the LIMIT and added echo *$row->username, $row->username.

<?php
$verbindung = mysql_connect("localhost", "social" , "123")
or die("cant connect");
mysql_select_db("homepage") or die ("cant connect to database");

$username = $_POST["username"];
$passwort = md5($_POST["password"]);

$abfrage = "SELECT username, password FROM login WHERE username LIKE '$username'";
$ergebnis = mysql_query($abfrage);
$row = mysql_fetch_object($ergebnis);

if($row->password == $passwort)
    {
    $_SESSION["username"] = $username;
    echo $row->username, $row->username;
    }
else
    {
    echo "Something went wrong>";
    }

?>

Conclusion

What am I missing our / doing wrong? I Would appreciate if you could show me how its done with some thorough explanations if possible.

  • 1
    Please try not to use mysql_connect instead use `mysqli_connect` or `PDO_MySQL` read this->http://php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated – Hirdesh Vishwdewa Aug 05 '15 at 08:09
  • [MySQL](http://php.net/manual/en/book.mysql.php) is deprecated. Use [MySQLi](http://php.net/manual/en/book.mysqli.php) and [mysqli_fetch_assoc](http://php.net/manual/en/mysqli-result.fetch-assoc.php). Also, if you will use `mysqli_fetch_assoc` you have multiple rows and can't compare passwords like this. Get user password with second query and compare with that. – Rene Korss Aug 05 '15 at 08:20

4 Answers4

1

MySQLi Procedural style->

    <?php
        $verbindung = mysqli_connect("localhost", "social" , "123","homepage")
        or die("cant connect");

        $username = $_POST["username"];
        $passwort = md5($_POST["password"]);

        $abfrage = "SELECT username, password FROM login WHERE username LIKE '$username'";

        $ergebnis = mysqli_query($verbindung,$abfrage);
        if (mysqli_num_rows($ergebnis) > 0) {
            // output data of each row
            while($row = mysqli_fetch_assoc($ergebnis)) {
                //print your data here
                //print_r($row);
                echo "Username - ".$row['username']." & password = ".$row['password'];
            }
        } else {
            echo "0 results";
        }
        mysqli_close($verbindung);

    ?>
Hirdesh Vishwdewa
  • 2,334
  • 1
  • 19
  • 33
0

Select all from the table and run a while loop to print all the rows?

$q = "SELECT * From tablename";
    $ergebnis = mysql_query($abfrage);
    while($row = mysql_fetch_object($ergebnis))
    {
    echo "Your content here";
    }

Stop using mysql_* please.

Akshay
  • 2,244
  • 3
  • 15
  • 34
  • It is deprecated. Use some handy library (like Nette framework does) or check out php.net, where you can find better substitution for mysql_* – tomascapek Aug 05 '15 at 08:13
  • does that mean the entire code is old or just mysql? I replaced mysql with mysqli it didnt work though – RR-Asperger Aug 05 '15 at 08:14
  • I think, the best solution for you is tu use some better "database framework", than library functions. For example, Dibi: (http://dibiphp.com/). It is better in many way - faster and comfy development, security ... – tomascapek Aug 05 '15 at 08:22
  • Just give it a try, it is awesome. I use Nette framework and it's database layer is simply magnificent! I think it too used Dibi for quite a while ... – tomascapek Aug 05 '15 at 08:29
0

For your code, try like this

while($row = mysql_fetch_object($ergebnis))
{
echo "Username: ".row['username'];
}
Elyor
  • 5,396
  • 8
  • 48
  • 76
0

You should use mysqli instead of the deprecated mysql.

    $verbindung = mysqli_connect("localhost","social","","123");
if (mysqli_connect_errno())

{
    echo"The Connection was not established" . mysqli_connect_error();
    exit();
}

Next, you can use a while loop to get everything from the table and set up a table to place the data.

Example:

<table>

<tr align="center" bgcolor="skyblue">
<th>S.N</th>
<th>Username</th>
<th>Password</th>
</tr>

<?php


 $get_user = "select * from login";

$run = mysqli_query($verbindung ,$get_user);

$i =0 ;

while ($row=mysqli_fetch_array($run))

{
$username = $row["username"];
$passwort = $row["password"];
    $i++;   



?>

<tr align="center">

<td> <?php echo $i;?></td>
<td><?php echo $username;?></td>
<td><?php echo $passwort;?></td>

</tr>
<?php }?>

</table>
fdfdfd
  • 501
  • 1
  • 7
  • 21