0

I have the following database (Named "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     |

I wish to search this database for the domain "example.net" and have the "Account Key" column for that domain be returned as a variable.

Example:

SEARCH TERM: example.net

RESPONSE: 889977

My code so far:

$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"
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Greenreader9
  • 491
  • 4
  • 12

1 Answers1

1

To get the Account Key as return, just fetch the data from the result set and returns it as an associative array

On the other hand, please use parametized parpared statement to avoid SQL injection attack.

Hence change

$sql = mysqli_query($connect,"SELECT `account_name` FROM `Account_info` WHERE `domain_name`='$domainSearch'");

to


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

Ken Lee
  • 6,985
  • 3
  • 10
  • 29