John John - 1 year ago 82
PHP Question

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 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,


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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download