title: Why am I receiving (apparently) random results from my MySQL query?
body: <p>For clarification, I am essentially re-writing this question.</p>
  
  <p>I have a table, as defined below:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `user` (
    `userid` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(255) NOT NULL,
    `email` varchar(255) NOT NULL,
    `status` enum('banned','moderated','user','author','moderator','admin','owner') NOT NULL DEFAULT 'user',
    `password` char(128) NOT NULL,
    `salt` char(128) NOT NULL,
    `description` text,
    PRIMARY KEY (`userid`),
    UNIQUE KEY `email` (`email`),
    UNIQUE KEY `name` (`name`)
  ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
  </code></pre>
  
  <p>The table dump -- with e-mail field redacted -- is:</p>
  
  <pre><code>--
  -- Dumping data for table `user`
  --
  
  INSERT INTO `user` (`userid`, `name`, `email`, `status`, `password`, `salt`, `description`) VALUES
  (1, 'Rilbur Skryler', '', 'owner', '2008dff1d727007691867904af3a96677ec81847ff943470a9596b184ffdb8b0de03e6fa68d9a6cb03170bff78d8d50f703bcec9120727b9eee5dbceeb1bc26e', '5647f4d6165a42a17d1ac6a46f13dc11d4a3d8d4fcd7b16f88f0fecc659bd1a9ddd1cb79002a8b1ab9daf4da549dd2e516806cc4603dd20cbdde175bd5961049', 'Site owner and administrator'),
  (2, 'Zacky', '', 'author', 'e7a10ef6b58734093286bb7ebba65b8dd36c1cc18bc59f16a6e05760230f228fbb0464131b6c1e4768e26792e9194f43d78c277eb7ac86775f9b46bbe4d9dde2', 'f42029a7c657e3998443ca1f1043202f8431fba3ec9471e389752e3e83c790d6193292678a5878dd45ba6d7496521bf8ecd6e469455d4c34d95b36495691941a', 'Wacky!'),
  (3, 'Roland', '', 'author', 'e7a10ef6b58734093286bb7ebba65b8dd36c1cc18bc59f16a6e05760230f228fbb0464131b6c1e4768e26792e9194f43d78c277eb7ac86775f9b46bbe4d9dde2', 'f42029a7c657e3998443ca1f1043202f8431fba3ec9471e389752e3e83c790d6193292678a5878dd45ba6d7496521bf8ecd6e469455d4c34d95b36495691941a', 'Involuntary addition.');
  </code></pre>
  
  <p>The actual PHP code (with added comments) is:</p>
  
  <pre><code>$query="select ".db_prefix."user.salt, ".db_prefix."user.password from ".db_prefix."user where ".db_prefix."user.userid=:id";
  echo $query.$id;//generates: select user.salt, user.password from user where user.userid=:id
  $statement=$db-&gt;prepare($query);
  $statement-&gt;execute(array('id'=&gt;$id));
  if($row=$statement-&gt;fetch())
  {
      $salt=$row[0];//row[0] is filled with a randomized result as shown two lines down
      var_dump($row);
      echo "pre one: $salt\r\n";//I'm outputting the value of the salt, which appears to be randomly generated.
      echo "one";
      $passwordsalt=generateValueFromPasswordAndSalt($password, $salt);//Used to call the routine that generates a value from the combination of a password string and the salt.
      return $passwordsalt==$row[1];//This always returns 'false' because the stored password value never matches the generated value, as a result of the salt being the wrong string.
  }
  </code></pre>
  
  <p>This problem has been driving me batty.  The value retrieved for the salt appears to be randomly generated (I say 'appears' because I know it shouldn't be random, I have to be missing something).  Various changes, such as reversing the salt and password values in the query or replacing them with a select * resolve the problem.  That's great, but I don't understand <em>why</em> they solve it, so I don't trust them.  (Plus, I want to learn why it's happening so I can avoid it in the future)</p>
  
  <p>If it matters, I'm using a PDO database object initialized as follows:</p>
  
  <pre><code>function get_DB()
  {
      static $db;
      if(isset ($db))
      {
          return $db;
      }
      else
      {
          try{
          $dsn = "mysql:host=".db_host.";dbname=".db_name;
          $db = new PDO($dsn, updater_login, updater_password);
          return $db;
          }
          catch(PDOException $e)
          {
              //echo $e-&gt;getMessage();
              return NULL;
          }
      }
  }
  </code></pre>
  
  <p>Randomly returned salt values include:</p>
  
  <p>9ed1358ccb614ee86f17a3cc964caaad3ab8ede7d705960aac2d5f4448c0f85d0acf581e2225d567789ac2f678b6a6662e4e7a8e55efb0d92be903249c44af7f</p>
  
  <p>6feff9b576ba0b0aa5e3d61996e723ddc4bdc3e7777469409b4da095aae6fb9a6df84fdaa96029a27e07714c670d18c2b8707453e515a3632383b6b03925b04b</p>
  
  <p>60fdc33fa77029f4627c39558417b1bb492a02d8cd56571b3091aff2123ee5545c5717a6a7c0553c7c659864e947f28c65627f3d288c51ef6c9b6de3f5175417</p>
  
  <p>PHP output:</p>
  
  <p>Run One:</p>
  
  <pre><code>select user.salt, user.password from user where user.userid=:id1&lt;pre class='xdebug-var-dump' dir='ltr'&gt;
  &lt;b&gt;array&lt;/b&gt;
    'salt' &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'15316153f3206618917cf7319747eda5871a7b8e942b54dba6f42e8bc491197450285504a37eb825e6aff04871fde52c49e029cf7e3a1d8cb9fe8a0422f4cd03'&lt;/font&gt; &lt;i&gt;(length=128)&lt;/i&gt;
    0 &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'15316153f3206618917cf7319747eda5871a7b8e942b54dba6f42e8bc491197450285504a37eb825e6aff04871fde52c49e029cf7e3a1d8cb9fe8a0422f4cd03'&lt;/font&gt; &lt;i&gt;(length=128)&lt;/i&gt;
    'password' &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'a0d2c6b1bf7163f85738627ffbfd5bd343e9bdaf0b98f00f63f020abac398df426d01f1bdcd8a46771af881640a4210b536cc89b7cea91637d7db705e64144cc'&lt;/font&gt; &lt;i&gt;(length=128)&lt;/i&gt;
    1 &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'a0d2c6b1bf7163f85738627ffbfd5bd343e9bdaf0b98f00f63f020abac398df426d01f1bdcd8a46771af881640a4210b536cc89b7cea91637d7db705e64144cc'&lt;/font&gt; &lt;i&gt;(length=128)&lt;/i&gt;
  &lt;/pre&gt;pre one: 15316153f3206618917cf7319747eda5871a7b8e942b54dba6f42e8bc491197450285504a37eb825e6aff04871fde52c49e029cf7e3a1d8cb9fe8a0422f4cd03
  
  one
  </code></pre>
  
  <p>Run Two:</p>
  
  <pre><code>select user.salt, user.password from user where user.userid=:id1&lt;pre class='xdebug-var-dump' dir='ltr'&gt;
  &lt;b&gt;array&lt;/b&gt;
    'salt' &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'5647f4d6165a42a17d1ac6a46f13dc11d4a3d8d4fcd7b16f88f0fecc659bd1a9ddd1cb79002a8b1ab9daf4da549dd2e516806cc4603dd20cbdde175bd5961049'&lt;/font&gt; &lt;i&gt;(length=128)&lt;/i&gt;
    0 &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'5647f4d6165a42a17d1ac6a46f13dc11d4a3d8d4fcd7b16f88f0fecc659bd1a9ddd1cb79002a8b1ab9daf4da549dd2e516806cc4603dd20cbdde175bd5961049'&lt;/font&gt; &lt;i&gt;(length=128)&lt;/i&gt;
    'password' &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'2008dff1d727007691867904af3a96677ec81847ff943470a9596b184ffdb8b0de03e6fa68d9a6cb03170bff78d8d50f703bcec9120727b9eee5dbceeb1bc26e'&lt;/font&gt; &lt;i&gt;(length=128)&lt;/i&gt;
    1 &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'2008dff1d727007691867904af3a96677ec81847ff943470a9596b184ffdb8b0de03e6fa68d9a6cb03170bff78d8d50f703bcec9120727b9eee5dbceeb1bc26e'&lt;/font&gt; &lt;i&gt;(length=128)&lt;/i&gt;
  &lt;/pre&gt;pre one: 5647f4d6165a42a17d1ac6a46f13dc11d4a3d8d4fcd7b16f88f0fecc659bd1a9ddd1cb79002a8b1ab9daf4da549dd2e516806cc4603dd20cbdde175bd5961049
  
  one
  </code></pre>
  
  <p>Run Three:</p>
  
  <pre><code>select user.salt, user.password from user where user.userid=:id1&lt;pre class='xdebug-var-dump' dir='ltr'&gt;
  &lt;b&gt;array&lt;/b&gt;
    'salt' &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'5df34898a394a3476ed96bc16b5b015a776c25912ba5b94427ff09e2331267ad100f7218e636eb50635a459ca5cfa00cee846db889a920c87cb25bddb47888bf'&lt;/font&gt; &lt;i&gt;(length=128)&lt;/i&gt;
    0 &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'5df34898a394a3476ed96bc16b5b015a776c25912ba5b94427ff09e2331267ad100f7218e636eb50635a459ca5cfa00cee846db889a920c87cb25bddb47888bf'&lt;/font&gt; &lt;i&gt;(length=128)&lt;/i&gt;
    'password' &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'3f9e5a9a6d387d411439d51744b3bb402c67cba64a36f47d82fe92d583ead1e26752aeeaf42bca12d8ef18a5423d2cab79c985f4fbfce1a6e53532453787419e'&lt;/font&gt; &lt;i&gt;(length=128)&lt;/i&gt;
    1 &lt;font color='#888a85'&gt;=&amp;gt;&lt;/font&gt; &lt;small&gt;string&lt;/small&gt; &lt;font color='#cc0000'&gt;'3f9e5a9a6d387d411439d51744b3bb402c67cba64a36f47d82fe92d583ead1e26752aeeaf42bca12d8ef18a5423d2cab79c985f4fbfce1a6e53532453787419e'&lt;/font&gt; &lt;i&gt;(length=128)&lt;/i&gt;
  &lt;/pre&gt;pre one: 5df34898a394a3476ed96bc16b5b015a776c25912ba5b94427ff09e2331267ad100f7218e636eb50635a459ca5cfa00cee846db889a920c87cb25bddb47888bf
  
  one
  </code></pre>