LaVOZs

The World’s Largest Online Community for Developers

'; php - Get Key Value on Row from MySQL Database By Using Email Address - LavOzs.Com

I need to get a value from a Key called Name in the Mysql Database by using the Email Address on the same row of the Name key. I need to echo the value of Name in PHP. Any help will be greatly appreciated.

Thanks!

Here's what I have: (Credits and thanks Mark Fajardo for this code, I made small modifications)

$db = mysqli_connect($db_host, $db_user, $db_password, $db_name);
$email = $_SESSION['email'];

$results = $db->query("SELECT * FROM users;");
$row = mysqli_fetch_assoc($results);

if(array_search($email, $row) != false) { // this will return false if not found
    $name = $row["name"];
    echo $name;
}


UPDATE

Right now it only getting the first row in the database.

Thanks for everyone that answered! I feel like I'm getting closer to the getting it working!

Basically, you have a database with table fields like :

  • id
  • email
  • name

So to retrieve these values ​​from the database in PHP you just have to make a request -> https://www.php.net/manual/en/mysqli.quickstart.statements.php

For this example, we could think of something like :

$email = $_POST['email'];
$requete = $myDb -> query('SELECT id, email, name FROM tableName WHERE email=' . $email);

I strongly advise you to use prepared queries to retrieve or display a value entered by the user which is surely the case of 'name'

And for display it you need :

$data = $requete -> fetch();

// ...

echo $data['name'];

That's all ! You now know how to recover data from a database :)

user11586589
  $result = mysql_query($sql); 

  while ($row = mysql_fetch_assoc($result)) { 
         echo $row['name'].'<br>'; 
  } 

You can make your sql query like this

$sql = "SELECT name FROM users WHERE email = '$email'";
$results = mysql_query($sql, $con);
$row = mysql_fetch_assoc($results);
$name = $results['name'];

Or like so

$sql = "SELECT * FROM users";
$results = mysql_query($sql, $con);
$row = mysql_fetch_assoc($results);

if(array_search($email, $row) != false) { // this will return false if not found
    $name = $row["name"];
}

Edit

To get all the rows

while($row = mysql_fetch_assoc($results)) {
    if(array_search($email, $row) != false) { // this will return false if not found
        $name = $row["name"];
    }
}
Related
Can I concatenate multiple MySQL rows into one field?
Which MySQL data type to use for storing boolean values
How do I connect to a MySQL Database in Python?
Finding duplicate values in MySQL
How to get a list of user accounts using the command line in MySQL?
How do I get a YouTube video thumbnail from the YouTube API?
PHP array delete by value (not key)
How to get the sizes of the tables of a MySQL database?