Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

read all mysql table entrys

Codes here !

Moderators: macek, egami, gesf

read all mysql table entrys

Postby SystemX » Sat Jun 02, 2012 3:29 am

Hi, i have a small code that will read from mysql table and check the username and password from the url with the database, this works however if i add another username and password in the database it only reads the first entry only :(

Baiscally i have a php script that gets info from the url and verifys it with the mysql like for example:

http://mydomain.info/login.php?username=test&password=123

Then the php script would grab "username=test" and "password=123" and verify them with the database and if correct it will echo "11" if username is wrong then it echos "0", if password is wrong then it echos "01"

so if the database is:
bob pass1
jill pass2
dave pass3


then the php script works with the first entry "bob pass1" but the other users it just echos "0"

Here is the php script(maybe a bit messy as im not all that good with php)

Code: Select all
<?php
// Include database connection settings
include('config.inc');

$query="SELECT * FROM login";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$f1=mysql_result($result,"username");
$f2=mysql_result($result,"password");

$user = addslashes($_GET['username']);
$pass = addslashes($_GET['password']);

if ($user == $f1)
    {
    echo("1");
    }
else
    {
echo("0");
exit;
}
if ($pass == $f2)
    {
    echo("1");
    }
else
    {
echo("0");
exit;
}
?>


How would i get it to work with all entrys in the database and not just the first 1 ?

Thanks for any help
SystemX
New php-forum User
New php-forum User
 
Posts: 8
Joined: Sat Jun 02, 2012 3:15 am

Re: read all mysql table entrys

Postby NigelRen » Fri Jun 15, 2012 11:51 pm

If you want to process all of the rows produced by mysql_query - you need to use a loop - so from the example on the manual page - use something like...
Code: Select all
while ($row = mysql_fetch_assoc($result)) {
    echo $row['firstname'];
    echo $row['lastname'];
    echo $row['address'];
    echo $row['age'];
}

BUT - why don't you use the username and password in the select so that you can retrieve just the one row.

Also if this code is going anywhere near a real web site then please encrypt the password so that we don't have repeats of leaked passwords being published on the web.
NigelRen
php-forum Active User
php-forum Active User
 
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron