Kieran Hanna
Kieran Hanna

Reputation: 175

How can I pull user details if they're logged in username matched a username in another table on Drupal?

I have attemped it with the following code:

<?php 
session_start();

// connects to the database
$mysqli = new mysqli("localhost", "root", "");

$query = "SELECT First_Name, Last_Name, Balance FROM customers WHERE username = '".$_SESSION['name']."'";
if($result = $mysqli->query($query))
{
while($row = $result->fetch_assoc())
{
echo "<div align=\"center\">";
echo "<br />Your <b><i>Profile</i></b> is as follows:<br />";
echo "<b>First name:</b> ". $row['First_Name'];
echo "<br /><b>Last name:</b> ".$row['Last_Name'];
echo "<br /><b>Balance:</b> ".$row['balance'];
echo "</div>";   
}
$result->free();
}
else
{
echo "No results found";
}

I get the following error:

Notice: A session had already been started - ignoring session_start() in   include_once() (line 2 of     C:\wamp\www\drupalNew\sites\all\modules\New_Balance\balance_module.module).  
Notice: Undefined index: name in include_once() (line 7 of   C:\wamp\www\drupalNew\sites\all\modules\New_Balance\balance_module.module).  

I want this to give details about the user who is logged in the drupal 7 site.

Upvotes: 0

Views: 25

Answers (1)

hugronaphor
hugronaphor

Reputation: 985

Just use:

function your_function() {
  global $user;

  $username = $user->name; // Instead of $_SESSION['name']

  // ... your query
}

Upvotes: 1

Related Questions