Title: Simple MySQL Query
Last modified: August 20, 2016

---

# Simple MySQL Query

 *  Resolved [bmd91](https://wordpress.org/support/users/bmd91/)
 * (@bmd91)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/)
 *     ```
       $qry = "SELECT *
               FROM <code>wp_companies</code>
               WHERE 1=1
               AND 'company_name' = 'Acme'";
               $data = mysql_query($qry);
               while($r[]=mysql_fetch_array($data));
   
       echo "<pre>".print_r ($r,true)."</pre>";
       ```
   
 * Anybody know why this would be returning an empty array? It seems so straightforward.
   No errors. Nothing in php error log either. When I put the sql statement into
   phpmyadmin it retrieves the correct row. Am I just missing something really simple?
   Sorry ahead of time if I am haha

Viewing 14 replies - 1 through 14 (of 14 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/#post-3004792)
 * I don’t think the code tags should be there. Try the FROM statement like this:
 *     ```
       FROM wp_companies
       ```
   
 *  Thread Starter [bmd91](https://wordpress.org/support/users/bmd91/)
 * (@bmd91)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/#post-3004799)
 * Empty with or without tags
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/#post-3004803)
 * Definitely leave off the tags. Try taking out the AND clause – just retrieve 
   all rows. Also notice that your while statement should be modified.
 *     ```
       $qry = "SELECT *
               FROM wp_companies
               WHERE 1=1";
       $data = mysql_query($qry);
       while($r[]=mysql_fetch_array($data));
       echo "<pre>".print_r ($r,true)."</pre>";
       ```
   
 * EDIT: Wrong about while statement. Ignore that part.
 *  Thread Starter [bmd91](https://wordpress.org/support/users/bmd91/)
 * (@bmd91)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/#post-3004818)
 * Well if I do that it retrieves all the rows. Why would the whole query work in
   phpmyadmin though? Also, I get :
 *     ```
       Array
       (
           [0] => Array
               (
                   [0] => 2
                   [ID] => 2
                   [1] => 1-800 Pet Supplies
                   [company_name] => 1-800 Pet Supplies
                   [2] => 0
                   [inventory] => 0
                   [3] => 0
                   [worth] => 0
                   [4] => http://www.google.com
                   [value_check_url] => http://www.google.com
               )
       ```
   
 * How do I get the 0,1,2,3,4 values out so it’s just id,co name, inventory, etc.?
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/#post-3004827)
 * Change the call to mysql_fetch_array to only return the associative array, and
   modify the while loop to print out the results:
 *     ```
       while($r = mysql_fetch_array($data,MYSQL_ASSOC)) {
          echo "<p>NAME:{$r['company_name']}</p>";
       }
       ```
   
 *  Thread Starter [bmd91](https://wordpress.org/support/users/bmd91/)
 * (@bmd91)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/#post-3004840)
 * Ok so I have all the results by doing:
 *     ```
       function getCompanyData($company){
               $qry = "SELECT *
               FROM wp_companies
               WHERE 1=1";
               $data = mysql_query($qry);
               while($r[]=mysql_fetch_array($data,MYSQL_ASSOC)){
                   for ($i=0; $i < count($r); $i++) {
                       $name=$r[$i]['company_name'];
                   }
               echo 'Name:'.$name.'<br />';
               };
           }
       ```
   
 * But how would I be able to call getCompanyData(‘Acme’) and get only their data?
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/#post-3004845)
 * It should work if coded like this:
 *     ```
       function getCompanyData($company){
               $qry = "SELECT *
               FROM wp_companies
               WHERE 1=1
               AND company_name = '$company' ";
               while($r = mysql_fetch_array($data,MYSQL_ASSOC)) {
                  echo "<p>NAME:{$r['company_name']}</p>";
               }
       }
       ```
   
 *  Thread Starter [bmd91](https://wordpress.org/support/users/bmd91/)
 * (@bmd91)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/#post-3004847)
 * Haha I started this topic because that wasn’t working..remember?
 *  Thread Starter [bmd91](https://wordpress.org/support/users/bmd91/)
 * (@bmd91)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/#post-3004848)
 * I had
 *     ```
       function getCompanyData($company){
               $qry = "SELECT *
               FROM wp_companies
               WHERE 1=1
               AND company_name = '$company' ";
               $data = mysql_query($qry);
               $r[]=mysql_fetch_array($data,MYSQL_ASSOC);
       ```
   
 * to start
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/#post-3004850)
 * That does not match the code you showed in the original post. The original code
   you showed had quotes around company_name in the query:
 *     ```
       AND 'company_name' = 'Acme'";
       ```
   
 *  Thread Starter [bmd91](https://wordpress.org/support/users/bmd91/)
 * (@bmd91)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/#post-3004851)
 * It’s still direct insertion into the sql statement that is not working. Right
   now I have:
 *     ```
       function getCompanyData($company){
               $qry = "SELECT *
               FROM wp_companies
               WHERE 1=1
               AND company_name = '$company' ";
               $data = mysql_query($qry);
               while($r = mysql_fetch_array($data,MYSQL_ASSOC)) {
                  echo "<p>NAME:{$r['company_name']}</p>";
               }
       }
       ```
   
 * It is echoing out an empty array. Sorry for the confusion
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/#post-3004853)
 * Then I suspect that either the value in $company is not what you expect, or that
   there are no companies with that name. Try echoing out the name and the sql:
 *     ```
       function getCompanyData($company){
               $qry = "SELECT *
               FROM wp_companies
               WHERE 1=1
               AND company_name = '$company' ";
       $co = htmlentities($company);
       echo "<p>CO:$co== QRY:$qry== </p>";
               $data = mysql_query($qry);
       ```
   
 *  Thread Starter [bmd91](https://wordpress.org/support/users/bmd91/)
 * (@bmd91)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/#post-3004880)
 * I looked back at where I was calling getCompanyData and saw I was calling a string.
   I set a variable equal to the string and then called the variable. Works now.
   Oops sorry
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/#post-3004882)
 * Glad you found it.

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Simple MySQL Query’ is closed to new replies.

## Tags

 * [MySQL](https://wordpress.org/support/topic-tag/mysql/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 14 replies
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [13 years, 7 months ago](https://wordpress.org/support/topic/simple-mysql-query/#post-3004882)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
