开发者

Creating a variable by pulling a value from a MySQL database

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 开发者_如何学Cvariable 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'";


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.


Is this what youa re looking for?

$result = mysql_query($sqlStremail);
$row = mysql_fetch_assoc($result);
$subcheckr = $row['subcheckr'];


$sqlStremail = mysql_query("SELECT subcheckr FROM login WHERE username = '$u'");
$result= mysql_fetch_array($sqlStremail);

$some_variable = $result['subcheckr']; // the value you want


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'];


You can write

$sqlStremail = "SELECT subcheckr FROM login WHERE username = '".mysql_real_escape($u)."'";  

$result = mysql_query($sqlStremail );

if (!$result) {
  die 'Could not run query: ' . mysql_error();
}

$row = mysql_fetch_row($result);

$subcheckr = $row['subcheckr'];


$variable = array_pop(mysql_fetch_row(mysql_query("SELECT subcheckr FROM login WHERE username = '$u'")));

Only if username is unique

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜