Reputation: 1866
I am trying to make some sessions from my database.
What I wan't to do is, take a specific row in my database an add the value of each column to a session.
The name of the session should then be the name of the column.
I am running ´session_start();´
Here is the code and my attempt to do it:
function opponent_data(){
try {
$PDO_new = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS);
$PDO_new->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$opponent = $PDO_new->prepare("SELECT * FROM Info WHERE user_name =:username");
$opponent->bindParam(":username", $_SESSION["opponent"]);
$opponent->execute();
//my failed attempt
$row = $opponent->fetch(PDO::FETCH_ASSOC);
$column = $opponent->fetch(PDO::FETCH_COLUMN);
$col_count = $opponent->columnCount();
for ($x = 0; $x <= $col_count; $x++) {
$_SESSION[$column[$x]] = $row[$x];
}
}catch (PDOException $e) {
$error[] = $e->getMessage();
}
}
Thanks for any help.
Sorry for the bad tittle, I didn't knew what i should call this.
Upvotes: 1
Views: 56
Reputation: 9583
When you use PDO::FETCH_COLUMN
a column number is expected ie.:
$stmt->fetch(PDO::FETCH_COLUMN, $number_of_column);
It would be easier to loop through the columns from your PDO::FETCH_ASSOC
$row = $opponent->fetch(PDO::FETCH_ASSOC);
foreach ($row as $columnName => $columnVal){
$_SESSION[$columnName] =$columnVal;
}
Upvotes: 2
Reputation: 3097
$x
is a number... But you are fetching your result into an associative array. $row[$x]
subsequently does not exist.
fetchColumn()
and fetchAssoc()
returns only 1 column... These should be in while-loops...
Upvotes: 1