Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过从MySQL数据库中提取值来创建变量_Php_Mysql - Fatal编程技术网

Php 通过从MySQL数据库中提取值来创建变量

Php 通过从MySQL数据库中提取值来创建变量,php,mysql,Php,Mysql,我正在使用一个名为“login”的MySQL表,其中包括名为“username”和“subcheckr”的字段 我想运行一个PHP查询,在表中创建一个等于“subcheckr”的新变量,其中“username”等于一个名为$u的变量。假设我想调用变量“$variable” 我该怎么做?下面的问题是我到目前为止的问题 提前感谢, 约翰 这就是你要找的吗 $result = mysql_query($sqlStremail); $row = mysql_fetch_assoc($result); $

我正在使用一个名为“login”的MySQL表,其中包括名为“username”和“subcheckr”的字段

我想运行一个PHP查询,在表中创建一个等于“subcheckr”的新变量,其中“username”等于一个名为$u的变量。假设我想调用变量“$variable”

我该怎么做?下面的问题是我到目前为止的问题

提前感谢,

约翰


这就是你要找的吗

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

我不知道我是否理解正确,但如果:

就这样做吧

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

$result = mysql_query($query);

$row = mysql_fetch_assoc($result);

$variable = $row["subcheckr"];
如果您不知道,您的查询很容易受到SQL注入的攻击。使用mysql\u real\u escape()之类的工具来过滤$u变量。

您可以执行以下操作:

// 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'];
只有用户名是唯一的

才能写入

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

可能的重复你不能根据你之前问题的答案来做吗?
// 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'];
$variable = array_pop(mysql_fetch_row(mysql_query("SELECT subcheckr FROM login WHERE username = '$u'")));
$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'];