Jump to content

Obtaining info from database table


SirChick

Recommended Posts

How do you obtain fields from a table and assign each field to a variable to be used as needed.

An example:

 

Say i had:

 

table name : user

 

Field 1 : User name

Field 2: Password

 

then i wanted:

 

$username = field 1

$password = field 2.

 

 

I know how to do the opposite whereby you input to a table from a variable but not the otherway round lol

Link to comment
https://forums.phpfreaks.com/topic/64249-obtaining-info-from-database-table/
Share on other sites

like so..

$conn = mysql_connect("localhost", "mysql_user", "mysql_password");

//error handle
if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}

//error handle
if (!mysql_select_db("mydbname")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}

$sql = "SELECT id as userid, fullname, userstatus
        FROM   sometable
        WHERE  userstatus = 1";

$result = mysql_query($sql);

$row = mysql_fetch_assoc($result));
$uid = $row["userid"];
$name = $row["fullname"];
$status = $row["userstatus"];

 

 

if (!mysql_select_db("mydbname")) {  // <---Tabe Selection

No thats database selection.

 

YOu select the table within your SQL Query:

$sql = "SELECT id as userid, fullname, userstatus
        FROM   sometable
        WHERE  userstatus = 1";

sometable is the table mysql query to fetch the userid, fullname and userstatus where userstatus equals to 1.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.