Skip to main content
ElizabethGailLittle
Inspiring
November 17, 2020
Answered

How to word query

  • November 17, 2020
  • 1 reply
  • 504 views

I want to display records from a table to show only those with one of 3 values in one field.  I'm enclosing the offending code.

 

$Cycle=$_GET['CycleYear'];

$SQL_rs1 = "SELECT * FROM RCL_selections WHERE RCL_selections.r_Cycle_Year = $Cycle ORDER BY RCL_selections.r_date ASC, RCL_selections.r_seq ASC";

$rs1 = $sainttim->query($SQL_rs1);
$error = $rs1->errorInfo();
if (isset($error[2])) die($error[2]);
$totalRows_rs1 = $rs1->rowCount();

 

$Cycle contains the desired value.  But I get an error at line $error = because $rs1 is not an object, so obviously the query failed.  I've tried substituting ? for $Cycle in the. SQL, but then cannot work out how to phrase the query action.  I've tried every combination I can think of.  Did a prepare, bound variables, used execute, etc.  But I need to a return an array of records.

 

Any help will be appreciated.!

Thanks,

Gail

This topic has been closed for replies.
Correct answer osgood_

Have you tried wrapping the $Cycle variable in the SELECT query in quotes:

 

'$Cycle'

1 reply

Legend
November 17, 2020

Why do you need these 2 lines of code anyway?

$error = $rs1->errorInfo();
if (isset($error[2])) die($error[2]);

 

Surely the below will do the job assuming $sainttim is your connection variable:

 

$Cycle=$_GET['CycleYear'];

$SQL_rs1 = "SELECT * FROM RCL_selections WHERE RCL_selections.r_Cycle_Year = $Cycle ORDER BY RCL_selections.r_date ASC, RCL_selections.r_seq ASC";

$rs1 = $sainttim->query($SQL_rs1);

$totalRows_rs1 = $rs1->rowCount();

 

But I would use some kind of sanitization as you're getting $Cycle from a url parameter?

 

Try that to see if at least you can get some information returned and then go back to the error handing if you really require it.

ElizabethGailLittle
Inspiring
November 17, 2020

But my problem is that the query aas defined is not working!  I removed the 2 lines of code as you suggested, and then get the following:

Fatal error: Uncaught Error: Call to a member function rowCount() on bool in /home1/sainttim/public_html/DisplayRCL.php:37 Stack trace: #0 {main} thrown in /home1/sainttim/public_html/DisplayRCL.php on line 37

 

Before removing the 2 lines, the error was on the call to $error - $rs1->errorInfo().  The problem is in the query.  Apparently using the variable $Cycle is not working.  I know the value and it is correct.

 

Thanks for the response.

osgood_Correct answer
Legend
November 17, 2020

Have you tried wrapping the $Cycle variable in the SELECT query in quotes:

 

'$Cycle'