Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.