Reputation: 1550
This is driving me crazy...
I'm simply trying to pull an INT value from my database (MySQL), but for some reason i get this error:
Notice: Array to string conversion in C:\xampp\htdocs\ajax\myjobs.php on line 33
this points my html where I try to echo $deptnum:
<h1><?php echo $deptnum; ?></h1>
Here's some of my PHP code:
<?php
require ("config.php");
include ("menu.php");
/* config.php CONTAINS CODE TO CONNECT TO DATABASE AND IT WORKS FINE */
/* Check if a value for user_name exists */
if (isset($_SESSION['user_name'])) {
$the_user_name = $_SESSION['user_name'];
/* FYI, THIS DOES PULL THE CORRECT USERNAME, IN THIS CASE: "ussatjodo" */
/* pull value from database */
$result = $db->prepare("SELECT `dept` from `users` WHERE `username` = :uname");
$result->execute(array(':uname' => $the_user_name));
$deptnum=$result->fetch();
} else {
$the_user_name = '';
}
?>
<h1><?php echo $deptnum; ?></h1>
And here is the table "users"
TO SUM IT UP:
Upvotes: 1
Views: 5068
Reputation: 4037
Comment. Not relevant to the question.
From what it looks like, you are storing passwords as plain text.
Rule of thumb, NEVER STORE PLAIN TEXT PASSWORDS
Always store encrypted passwords. Use default encryption techniques ie http://www.php.net/manual/en/function.password-hash.php , available in PHP > 5.5
If you are using PHP < 5.5, https://github.com/ircmaxell/password_compat provides a compatibility library for using password_hash
Upvotes: 2
Reputation: 78994
fetch()
returns an array indexed by the table column names. Try:
echo $deptnum['dept'];
PHP >= 5.4.0:
$deptnum = $result->fetch()['dept'];
Upvotes: 4