tags:

views:

36

answers:

5

Hello,

I am using a MySQL table called "login" that includes fields called "username" and "subcheckr."

I would like to run a PHP query to create a new variable equal to "subcheckr" in the table where "username" equals a variable called $u. Let's say I want to call the variable "$variable."

How can I do this? The query below is what I have so far.

Thanks in advance,

John

  $sqlStremail = "SELECT subcheckr
               FROM login 
              WHERE username = '$u'";
+1  A: 

Is this what youa re looking for?

$result = mysql_query($sqlStremail);
$row = mysql_fetch_assoc($result);
$subcheckr = $row['subcheckr'];
Joyce Babu
+1  A: 
$sqlStremail = mysql_query("SELECT subcheckr FROM login WHERE username = '$u'");
$result= mysql_fetch_array($sqlStremail);

$some_variable = $result['subcheckr']; // the value you want
Ross
+1  A: 

I don't know if I understood correctly but if:

Just do something like this.

$sqlStremail = "SELECT subcheckr
                FROM login 
                WHERE username = '$u'";

$result = mysql_query($query);

$row = mysql_fetch_assoc($result);

$variable = $row["subcheckr"];

In case you don't know, your query is vulnerable for SQL injections. Use something like mysql_real_escape() to filter your $u variable.

Stegeman
A: 

You can do:

// make sure you use mysql_real_escape to escape your username.
$sqlStremail = "SELECT subcheckr FROM login WHERE username = '".mysql_real_escape($u)."'";  

// run the query.
$result = mysql_query($sqlStremail );

// See if the query ran. If not print the cause of err and exit.
if (!$result) {
    die 'Could not run query: ' . mysql_error();
}
// if query ran fine..fetch the result row. 
$row = mysql_fetch_row($result);

// extract the field you want.
$subcheckr = $row['subcheckr'];
codaddict
A: 
$variable = array_pop(mysql_fetch_row(mysql_query("SELECT subcheckr FROM login WHERE username = '$u'")));

Only if username is unique

BenWells