FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


how to get something out the db?





golles
the subject of this toic is not really good and I think this is asked before, but I don't know how to say/search it (yea dutch, still school vacation so crap english Razz )

I have a database table with id, gebruikersnaam, wachtwoord and e-mail (eng: username and password)
it is a login script/page, the user fills in his username and password and the script starts working, now I'm wondering how I can get the id out of the db when I know the gebruikersnaam and wachtwoord???

I tried this:
Code:

$sql_query = "SELECT 'id' FROM login WHERE gebruikersnaam='$gebruikersnaam' AND wachtwoord='$wachtwoord'";
$result = mysql_query($sql_query) or die(mysql_error());
echo ("<br>" . $result);


this came on the page:
Quote:

Resource id #4



the table has on this moment 3 rows, and with the login i filled in it should just echo a 1 instead of Resource id #4.

can some help me?
thank you!


//golles
mathiaus
id should be unique so there should either be 1 or 0 rows, not 3 Eh?
Code:
$sql_query = "SELECT 'id' FROM login WHERE gebruikersnaam='$gebruikersnaam' AND wachtwoord='$wachtwoord'";
$result = mysql_query($sql_query) or die(mysql_error());
$row = mysql_fetch_assoc($result) or die(mysql_error());
echo $row['id'];


Theres other ways of extracting the data but its all up to user preference really.
Aredon
I usually use mysql_result when the result is expected to be limited to one match.
Code:

echo ("<br>" . mysql_result($result,0));

The reason for the 2nd argument of zero is because SELECTs are able to select more than just one row (unless you added a "LIMIT 1" to the query) and if you wanted to get the 'id' of a different row, you could just change the 2nd argument
e.g. if you wanted to get the 'id' of the last match, you could do:
Code:

$num_rows = mysql_num_rows($result);
$last_id=mysql_result($result,$num_rows-1);
echo ("<br>" . $last_id);


Also mysql_result accepts an optional 3rd argument in case your SELECT selected more than one field.
e.g. * or ('id','age') in place of 'id'
Code:

$sql_query = "SELECT * FROM login WHERE gebruikersnaam='$gebruikersnaam' AND wachtwoord='$wachtwoord'";

In such a case, you could send mysql_result a 3rd argument in order to select the field you want.
Code:

$sql = "SELECT ('id','age') FROM login WHERE gebruikersnaam='$gebruikersnaam' AND wachtwoord='$wachtwoord'";
$result=mysql_query($sql);
echo mysql_result($result,0,0);//first row, first field -- 'id'
echo mysql_result($result,0,1);//first row, second field -- 'age'
Related topics
problem in sql and importing db.
Anyone?
Thumbs.db and _vti_cnf
PHP DB Error: syntax error
Advanced MySQL db merging Tutorials
Strange Mysql-database
MySQL DB Backup script - minor problem
phpBB Error, seems to be a MYSQL DB error...
Turn off "THUMBS.db"
How do I make a MySQL dB ?
[RESOLVED] How do I make a MySQL dB ?
Connecting to a mysql DB
[PHP] phpbb registration - add to other db
reading from sql db
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.