SELECT Query with $_Sessions

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Havouza15
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Feb 21, 2017 3:44 am

Wed May 17, 2017 10:59 pm

Hi!
I try to use $_Session when Making a Select query in MySql

I have tried different ways but nothing works. What do I do wrong

Below is my code. The out commented queries does not work

Regards

Anders

Code: Select all

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM members WHERE username = ('Anders') AND memberID = ('1')";

//$sql = "SELECT * FROM members WHERE username = $_SESSION[username] AND memberID = $_SESSION[memberID]";

//$sql = "SELECT * FROM members WHERE username = {$_SESSION[username]} AND memberID = {$_SESSION[memberID]}";

$result = $conn->query($sql);

$row = $result->fetch_array(MYSQL_BOTH);
echo $row['username'];
?>
<table>
<tr>
<td><?php echo $row['username'];?></td>
<td><?php echo $row['memberID'];?></td>
<td><?php echo $row['email'];?></td>
</tr>
</table>


<?php
$conn->close();
?>

AdoptiveSolution
New php-forum User
New php-forum User
Posts: 162
Joined: Wed Jun 15, 2016 8:35 am

Thu May 18, 2017 3:02 am

Try this :

Code: Select all

$sql = "SELECT * FROM members WHERE username = '" . $_SESSION[username] . "' AND memberID = '" . $_SESSION[memberID] . "'";

Havouza15
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Feb 21, 2017 3:44 am

Thu May 18, 2017 3:11 am

Worked!
Thanks!

Post Reply
  • Information
  • Who is online

    Users browsing this forum: Bing [Bot] and 5 guests