0

Working with WordPress, running a custom query to pull all records relating to a specific condition. When I run the query in phpmyadmin it returns all records, when I run the query through PHP code, only 2-5 results return, need to find out how to resolve this:

// get variables from form page
$txtReg = $_REQUEST['txtReg'];
$txtMsg = $_REQUEST['txtMsg'];

// connect to database
$mydb = new 
wpdb('***','***','***','***');

// run the query to fetch all cell numbers from the region variable
$query = "SELECT * FROM tblusers WHERE `Region` ='$txtReg'";
$rows = $mydb->get_results($query);

// display all cell numbers from that region
foreach ($rows as $row) {
  $txtCell = $row->Cell;
  //doSendSMS($txtCell,$txtMsg);
  echo $txtCell;  
}

E.g there are 100 cell numbers in Region A, only a few are returned and echoed, not all 100 like it should be, so when I run the sms code (which is the actual function, echo is used just to test results), not all receive the sms.

  • 1
    Maybe your DBis configured to return only a few rows, in the absence of a LIMIT clause in your query (i.e. someone set "default limit = 2"). https://stackoverflow.com/questions/44733179/can-i-set-a-default-limit-for-all-selects-in-the-mysql-client - try putting `LIMIT 100` on your query and see if you get the result counts youre expecting – Caius Jard Oct 15 '18 at 08:41
  • `$query = "SELECT * FROM tblusers WHERE Region ='$txtReg' LIMIT 100";` – Caius Jard Oct 15 '18 at 08:41
  • still returns only 2 results – Andre van Rensburg Oct 15 '18 at 08:45
  • 2
    Your code is open to [SQL injection](https://stackoverflow.com/q/332365/2469308) related attacks. Please learn to use [Prepared Statements](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Madhur Bhaiya Oct 15 '18 at 08:53
  • Try doing size /count and print_r($rows); check how many rows are there and whats its outputting – Mangesh Sathe Oct 15 '18 at 08:58
  • 1
    Please print the sql query and try the same directly in MySQL. – Tristup Oct 15 '18 at 09:00

1 Answers1

0

@Caius Jard was correct about the settings in the database, it was in fact limited even though no limit clause was applied in the sql query itself. The code is functional in my setup but was limited by the default settings.

tested using:
$query = "SELECT * FROM tblusers WHERE Region ='$txtReg' LIMIT 100";