title: mySQL Queries in Wordpress
body: <p>I have a mySQL database table called stock which holds a stock record on items for sale.  When querying the database using PHP I have no issue, but when I try to do it through Wordpress it doesn't work.</p>
  
  <p>my PHP is as follows</p>
  
  <pre><code>// Query the stock level
  function QueryStock($cat) {
      $query = "SELECT * FROM stock";
      $result = mysql_query($query);
  
      if (!$result) die ("Database access failed: " . mysql_error());
      $rows = mysql_num_rows($result);
      for ($j = 0 ; $j &lt; $rows ; ++$j)
      {
          $row = mysql_fetch_row($result);
          if ($row[0] == $cat) {
              $catno=$row[0];
              $supplier=$row[1];
              $itemname=$row[2];
              $category=$row[3];
              $price=$row[4];
              $stock=$row[5];
          }
      }
      mysql_close($db_server);
  
      echo "There are " . $stock . " bears";
  }
  </code></pre>
  
  <p><code>$cat</code> in the function attribute is the catalogue number of the item I am querying and the output is "There are 2 bears".</p>
  
  <p>I put this in Wordpress and the webpage hangs.  Then through Google searching, I found out about the $wpdb object being used by Wordpress at <a href="http://designmodo.com/wpdb-object-wordpress/" rel="nofollow">designmodo.com</a>. So with some code condensing too, I changed it to...</p>
  
  <pre><code>function QueryStock($cat) {
      $query = "SELECT * FROM stock WHERE catno = " . $cat;
      global $wpdb;
      $row = $wpdb-&gt;get_row($query, ARRAY_N);
          $catno == $row[0];
          $itemname = $row[2];
          $price = $row[4];
          $stock = $row[5];
  
      echo "There are " . $stock . " bears";
  }
  </code></pre>
  
  <p>Now the output is "There are bears" (missing the <code>$stock</code> value). I have been doing loads of Google searching to get where I am and I am having problems understanding it.  Where am I going wrong?</p>