Page 1 of 1

retrieve information from database

Posted: Sat Aug 31, 2002 2:57 pm
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

Posted: Sat Aug 31, 2002 4:33 pm
by volka

Posted: Sun Sep 01, 2002 1:27 am
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

Posted: Sun Sep 01, 2002 2:06 am
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"];
?>

Posted: Sun Sep 01, 2002 2:31 am
by NJ Termite
Thank you so much! :D

Best Regards,Mark

Posted: Mon Sep 02, 2002 6:02 am
by NJ Termite
instead of using:

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

how could i assing each field to a variable?

Thanks mark

Posted: Mon Sep 02, 2002 6:28 am
by twigletmac
If $row contained field1, field2 and field3 elements then

Code: Select all

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

Mac

Posted: Mon Sep 02, 2002 10:02 am
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'];

Posted: Mon Sep 02, 2002 10:06 am
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

Posted: Mon Sep 02, 2002 10:21 am
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 :)