• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to word query

Participant ,
Nov 17, 2020 Nov 17, 2020

Copy link to clipboard

Copied

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

TOPICS
Code , Error , How to , Server side applications

Views

289

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Nov 17, 2020 Nov 17, 2020

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

 

'$Cycle'

Votes

Translate

Translate
LEGEND ,
Nov 17, 2020 Nov 17, 2020

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 17, 2020 Nov 17, 2020

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 17, 2020 Nov 17, 2020

Copy link to clipboard

Copied

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

 

'$Cycle'

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 19, 2020 Nov 19, 2020

Copy link to clipboard

Copied

LATEST

That did it!  Thank you!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines