如何搜索数据库列和 return 匹配行
How to seach a database column, and return matching rows
我有以下数据库(名为“Account_info”):
|id (All Unique)|domain_name (All unique)| account_name |account_key|
|---------------|------------------------|----------------|-----------|
| 1 |example.com |account_23657612|889977 |
| 2 |example.net |account_53357945|889977 |
| 3 |example.edu |account_53357945|889977 |
| 4 |example.xyz |account_93713441|998822 |
我希望在该数据库中搜索域“example.net”,并将该域的“帐户密钥”列作为变量返回。
示例:
SEARCH TERM: example.net
RESPONSE: 889977
到目前为止我的代码:
$domainSearch = "example.net";
$sql = mysqli_query($connect,"SELECT `account_name` FROM `Account_info` WHERE `domain_name`='$domainSearch'");
if($sql){
//Some code here that sets the variable "result" to "889977"
}
要获取帐户密钥 return,只需从结果集中获取数据并将其return作为关联数组
另一方面,请使用参数化parpared语句来避免SQL注入攻击。
因此改变
$sql = mysqli_query($connect,"SELECT `account_name` FROM `Account_info` WHERE `domain_name`='$domainSearch'");
至
$sql = "SELECT * FROM Account_info WHERE domain_name=?"; // SQL with parameters
$stmt = $connect->prepare($sql);
$stmt->bind_param("s", $domainSearch);
$stmt->execute();
$result = $stmt->get_result(); // get the mysqli result
$user = $result->fetch_assoc(); // fetch data
echo $user["account_key"];
// to assign as a variable -- $var1=$user["account_key"];
我有以下数据库(名为“Account_info”):
|id (All Unique)|domain_name (All unique)| account_name |account_key|
|---------------|------------------------|----------------|-----------|
| 1 |example.com |account_23657612|889977 |
| 2 |example.net |account_53357945|889977 |
| 3 |example.edu |account_53357945|889977 |
| 4 |example.xyz |account_93713441|998822 |
我希望在该数据库中搜索域“example.net”,并将该域的“帐户密钥”列作为变量返回。
示例:
SEARCH TERM: example.net
RESPONSE: 889977
到目前为止我的代码:
$domainSearch = "example.net";
$sql = mysqli_query($connect,"SELECT `account_name` FROM `Account_info` WHERE `domain_name`='$domainSearch'");
if($sql){
//Some code here that sets the variable "result" to "889977"
}
要获取帐户密钥 return,只需从结果集中获取数据并将其return作为关联数组
另一方面,请使用参数化parpared语句来避免SQL注入攻击。
因此改变
$sql = mysqli_query($connect,"SELECT `account_name` FROM `Account_info` WHERE `domain_name`='$domainSearch'");
至
$sql = "SELECT * FROM Account_info WHERE domain_name=?"; // SQL with parameters
$stmt = $connect->prepare($sql);
$stmt->bind_param("s", $domainSearch);
$stmt->execute();
$result = $stmt->get_result(); // get the mysqli result
$user = $result->fetch_assoc(); // fetch data
echo $user["account_key"];
// to assign as a variable -- $var1=$user["account_key"];