title: PHP getting data from mysql database for the Query string variable
body: <p>Here is what I would like to do; I am passing an encoded data as a query variable to my webpage, then in my php page, I am decoding the data and checking the same in my database. The code I am using is shown below:</p>
  
  <pre><code>&lt;?php
  // Get the ID from URL.
  $id = ( isset($_GET["id"]) &amp;&amp; !empty($_GET["id"]) ? $_GET["id"] : "");
  
  // If "id" is not empty, proceed.
  if(!empty($id)) {
      $id = base64_decode($id);
      global $wpdb;
      $res = $wpdb-&gt;query("SELECT * FROM S_redirect WHERE source = ".$id);
      $tot = count($res);
  
      echo $tot . " records found.";
  
      for( $i=0; $i&lt;count($res); $i++) {
          echo $res[$i]-&gt;id;
      }
      exit;
  }
  else {
      echo "No ID";
      exit;
  }
  ?&gt;
  </code></pre>
  
  <p>I have one record in my database. The above code correctly says "1 records found". I am not sure how to get the value of the field in that row. I have 3 columns, they are id, field1 and field2. The code "<code>echo $res[$i]-&gt;id</code>" returns nothing.</p>
  
  <p>Please help</p>
  
  <p><strong>BTW, I am trying this in my Wordpress blog.</strong></p>
  
  <p>Thank you all for your suggestions. I tried your suggestions and here are my results:</p>
  
  <pre><code>$res = $wpdb-&gt;query("SELECT * FROM S_redirect WHERE source = ".$id);
  $tot = count($res);
  echo $tot . " records found."."&lt;/br&gt;";
  </code></pre>
  
  <p>it says 1 records found.</p>
  
  <pre><code>$res = $wpdb-&gt;get_row("SELECT * FROM S_redirect WHERE source = ".$id);
  $tot = count($res);
  echo $tot . " records found."."&lt;/br&gt;";
  </code></pre>
  
  <p>it says 0 records found.</p>
  
  <pre><code>$res = $wpdb-&gt;get_results("SELECT * FROM S_redirect WHERE source = ".$id);
  $tot = count($res);
  echo $tot . " records found."."&lt;/br&gt;";  
  </code></pre>
  
  <p>it says 0 records found.</p>
  
  <pre><code>while ($row = mysql_fetch_array($res)) {
  echo $row[0];  
  }
  </code></pre>
  
  <p>If I use mysql_fetch_array, </p>
  
  <blockquote>
    <p>Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/myfolder/mytheme/index.php on line 38.</p>
  </blockquote>
  
  <p>line 38 is while ($row = mysql_fetch_array($res)) {.</p>
  
  <p><strong>What I am trying to accomplish:</strong></p>
  
  <p>I have a wordpress blog. All the above code goes into my index.php. I will pass a product id to my index.php via query string variable. I have created a new table called S_redirect in my wordpress database. I will retrieve the value of query string variable id and check the same in my database table. If record exists, then retrieve one of the column value (url of the product) from that table row and redirect to that product url. If the record doesn't exists then redirect to home page. hope this helps everyone to understand what I am doing.</p>