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 7546193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:08:11+00:00 2026-05-30T09:08:11+00:00

My function looks like that. if (isset($_POST[‘type’])) { switch ($_POST[‘type’]) { case qsubject: $sql

  • 0

My function looks like that.

if (isset($_POST['type'])) {
    switch ($_POST['type']) {
        case "qsubject":
            $sql = "SELECT id, name FROM chapters WHERE subject_id=?";
            break;
        case "qchapters":
            $sql = "SELECT id, name FROM sections WHERE subject_id=? AND chapter_id=?";
            break;
        case "qsections":
            $sql = "SELECT id, name FROM paragraphs WHERE subject_id=? AND chapter_id=? AND section_id=?";
            break;
    }
    $stmt = $db->prepare($sql) or die($db->error());
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $stmt->store_result();
    if ($stmt->num_rows > 0) {
        $stmt->bind_result($chp_id, $name);
        echo '<option value="#"></option>';
        while ($stmt->fetch())
            echo '<option value="' . $chp_id . '">' . $name . '</option>';
    }
    else
        echo 0;
    $stmt->close();
}

For every switch case $params ($stmt->bind_param($params);) must be different.

So what I want to do is to create $params inside switch

$params='"i"'.$id; 

Then use it like this

$stmt->bind_param($params);

Is that possible?

  • 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-05-30T09:08:12+00:00Added an answer on May 30, 2026 at 9:08 am

    I don’t really know if that is what you want, but just put the prepare() and bind_param() calls inside the case-statement:

    if (isset($_POST['type'])) {
        switch ($_POST['type']) {
            case "qsubject":
                $sql = "SELECT id, name FROM chapters WHERE subject_id=?";
                $stmt = $db->prepare($sql) or die($db->error());
                $stmt->bind_param("i", $id);
                break;
            case "qchapters":
                $sql = "SELECT id, name FROM sections WHERE subject_id=? AND chapter_id=?";
                $stmt = $db->prepare($sql) or die($db->error());
                $stmt->bind_param("ii", $id, $chapter_id);
                break;
            case "qsections":
                $sql = "SELECT id, name FROM paragraphs WHERE subject_id=? AND chapter_id=? AND section_id=?";
                $stmt = $db->prepare($sql) or die($db->error());
                $stmt->bind_param("iii", $id, $chapter_id, $section_id);
                break;
        }
        $stmt->execute();
        $stmt->store_result();
        if ($stmt->num_rows > 0) {
            $stmt->bind_result($chp_id, $name);
            echo '<option value="#"></option>';
            while ($stmt->fetch())
                echo '<option value="' . $chp_id . '">' . $name . '</option>';
        }
        else
            echo 0;
        $stmt->close();
    }
    

    you are doing 3 different queries, so it’s totally fine to have 3 different prepare()-calls.

    if you can’t use this approach you could also create a simple array to store your parameters.

    $params = array();
    case "qsubject":
         $params[] = array("type"=>"i", "value"=>$id);
         break;
    
    case "qchapters":
         $params[] = array("type"=>"i", "value"=>$id);
         $params[] = array("type"=>"i", "value"=>$chapter_id);
         break;
    
    // probably won't work, see text below.
    foreach ($params as $param)
         $stmt->bind_param($param["type"], $param["value"]);
    

    it seems that you have to provide all the variables when calling the bind_param statement. you can still call bind_param with a variable count of arguments. see the comments on http://php.net/manual/de/mysqli-stmt.bind-param.php for a solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I have a function that looks something like this: //iteration over scales foreach ($surveyScales
So I have a function that looks something like this: float function(){ float x
I have a function that loads in some XML that looks like this: private
I have a script that looks like this #!/bin/bash function something() { echo hello
Jquery has a great language construct that looks like this: $(document).ready(function() { $(a).click(function() {
Here is the situation. I have some javascript that looks like this: function onSubmit()
I have a simple recursive array function that looks like this: function recursive_array($results) {
I have some jQuery that looks like this: $.ajax({ type: POST, url: /Customer/CancelSubscription/<%= Model.Customer.Id

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.