Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8656241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:13:49+00:00 2026-06-12T15:13:49+00:00

I have some working code that uses a SQL select statement within it. As

  • 0

I have some working code that uses a SQL select statement within it. As a static bit of code, it works great, however, I want to be able to use variables in it. So, doing my research, everyone says you should use PHP Prepare statements, to keep things safe and secure. So, my old code looks something like this:

    /*** MY OLD WORKING CODE ***/ 
    /*** SQL Query ***/   
    $sql = "SELECT tickets.ref AS `Ticket Number` ......
    .....BLAH................INTERVAL 30 DAY) AND 
    organizations.name = 'NAMEIWANTTOUSE' ";

    /*** For Each Loop to Build Table ***/
    foreach ($dbh->query($sql) as $row){ 
     echo "<tr>"; 
      echo "<td bgcolor=89FF95 align=center>".$row['Ticket Number']."</td>"; 
       ETC..................
    } 
    $sql->closeCursor();

My new code, that Im trying to get working.. seems to be doing nothing. I know Im not too far out with it, but I just cant get to grips with which variable I should be pushing into my FOREACH loop.. or if I have actually got the PHP Prepare statement right at all?

Any suggestions anyone?

    /*** MY NEW NOT WORKING CODE ***/ 
    /*** Variables ***/
    $NAME = 'NAMEIWANTTOUSE';
    $DAYS = '30';

    /*** Prepare the SQL Query ***/
    $stmt = $sql->prepare("SELECT tickets.ref AS `Ticket Number` .......
    ....BLAH................INTERVAL :DAYS DAY) AND
    organizations.name = :NAME ");

    $stmt->bindParam(":DAYS", $DAYS);
    $stmt->bindParam(":NAME", $NAME);
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $stmt->execute();

    /*** Put each return from SQL into a Variable? ***/
    $MySQLQuery = $stmt->fetchAll();

    /*** For Each Loop to Build Table ***/
    foreach ($dbh->query($MySQLQuery) as $row){ 
     echo "<tr>"; 
      echo "<td>".$row['Ticket Number']."</td>"; 
       ETC..................
    } 
    $sql->closeCursor();

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:13:50+00:00Added an answer on June 12, 2026 at 3:13 pm

    You nearly do have it, except that you ought not be calling query() in the loop. The result set has already been fetched into $MySQLQuery. fetchAll() returns a 2D array, unlike the old mysql_query() which returned a result resource that needed to be looped over with mysql_fetch_*().

    // Following fetchAll(), $MySQLQuery is a 2D array containing your rowset.
    foreach ($MySQLQuery as $row){ 
     echo "<tr>"; 
      echo "<td>".$row['Ticket Number']."</td>"; 
       ETC.........
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some working code that uses IImgCtx to load images, but I can't
i have some code examples I am working to implement that uses the type
I have some PHP code in a project I'm working on that uses PHP's
I have some (working) code that uses a multimap<string,string> . I'd like to change
I have been working on some legacy C++ code that uses variable length structures
I am working on a memory matching game. I have some code that checks
I have some code I am working on that worked just fine until I
I'm working on some code which fills a GridView full of images that have
I am working on some code, basically I have a form. On that form
I have to modify some code in a application I am working on that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.