retrieve information from database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
NJ Termite
Forum Newbie
Posts: 6
Joined: Fri Aug 30, 2002 11:59 pm

retrieve information from database

Post by NJ Termite »

i need to retrive information from my database, table user,

i need it to get the fields for that user, like name, address and print it to a page. Any Sugesstions? im fairly new to php

Thanks,
Mark
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

NJ Termite
Forum Newbie
Posts: 6
Joined: Fri Aug 30, 2002 11:59 pm

Post by NJ Termite »

thanks for the link, one question.

how could i retrive information from just one row, say if i have 4 fields with :

---------------------------------------------------------

field1 field2 field3 field4
row1 termite password Mark Mattie

row2 PDog mypass John Doe

row3 Mary108 marypass Mary Jane

----------------------------------------------------------

if the user name(field1) and password(field2) match then it wll print out First name(field3) and Last name(field3)?

example:
someone types in

njtermite(field1) password(field2) then i have the user name and passoword in variables, since they match it will print out Mark(field3 and Mattie(field4)

thanks,
mark
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Here it is:

Code: Select all

SELECT field3,field4 FROM table_name WHERE field1 = 'termite' AND field2 = 'password'
Here's the PHP code that will print out the field 3 and field 4.

Code: Select all

<?php
mysql_connect("host","user","password") or die("Fail to establish connection with MySQL");
mysql_select_db("db") or die("Fail to select a database");

$sql = "SELECT field3,field4 FROM table_name WHERE field1 = 'termite' AND field2 = 'password'";
$result = mysql_query($sql);
if(mysql_num_rows($result) == 0) { echo "Invalid password"; }
$row = mysql_fetch_array($result);

echo $rowї"field3"];
echo $rowї"field4"];
?>
NJ Termite
Forum Newbie
Posts: 6
Joined: Fri Aug 30, 2002 11:59 pm

Post by NJ Termite »

Thank you so much! :D

Best Regards,Mark
NJ Termite
Forum Newbie
Posts: 6
Joined: Fri Aug 30, 2002 11:59 pm

Post by NJ Termite »

instead of using:

echo $row["field3"];
echo $row["field4"];

how could i assing each field to a variable?

Thanks mark
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If $row contained field1, field2 and field3 elements then

Code: Select all

extract($row);
would create the variables $field1, $field2, $field3.

Mac
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Or if you want to be explicit about it, can aid the reading of the code in 6 months time :wink:

$MyFirstField = $row['field1'];
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Code: Select all

$MyFirstField = $rowї'field1'];
This can be a lot of typing if you have loads of fields coming from the database.

Mac
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

True,

Just really giving another alternative, may help them in the future with other stuff not directly related to this topic.

I also find it easier to read code when it is explicit, saves having to read the database schema in conjunction with the code, just personal preference nothing else :)
Post Reply