Jump to content

Only get some columns from prepared mysqli statement


flex_php

Recommended Posts

I've just started using prepared statements and my php has been limited to get what I need out of the database. All has gone well until I'm changing one of my joined statements that I'm looping through to create 2 datasets from the result (perhaps the way I'm doing this is from very old database habits?).

 

Anyway, a query such as:

 

    $query = "SELECT table1.field1, table1.field2, table1.field3, table2.field11, table2.field22 FROM table1 INNER JOIN table2 ON table1.this = table2.that WHERE table1.field1 = ?"

 

Preparing the statement such as:

   

 

    $stmt = mysqli_prepare($this->connection, $query);

        mysqli_bind_param($stmt, 'i', $var1);

        mysqli_stmt_execute($stmt);

        $rows = Array();

        $row = new MyObject();

        mysqli_bind_result($stmt, $row->field1, $row->field2...all the fields listed in the query....);

        while (mysqli_stmt_fetch($stmt)) {

        $rows[] = $row;

        $row = new MyObject();

        mysqli_bind_result($stmt, $row->field1, $row->field2...all the fields listed in the query....);

        }

 

Now what I'd like to do is grab selective fields from the result.

Something like

 

    if (condition){

   

    $row = new MyObject1();

    $row->field1 = $stmt->field1;

   

    }else{

   

    $row = new MyObject2();

    $row->field11 = $stmt->field11;

   

    }

 

Does this make sense and how would I go about getting only the fields I want from the result?

Link to comment
Share on other sites

Thanks for the response. I don't have a problem with duplicate column names, what I'm looking to do is take selective fields from the result (The result comes back just fine). I'm ordering the result by a field ID -- I want to watch for this ID to change, and grab selective fields when this happens.

Link to comment
Share on other sites

Yes, I understand the "ID changing" isn't directly related to mysql. It's a quiz, questions & choices. Before I was using prepared statements I would use my query and be able to sort out the questions from the choices with the result object. Here I suppose I can go through the result, again, after the bind_result using all the fields in the parameters. It seems to do it 'my old way' with the prepared statement functions isn't possible, or ideal even before for all I know to be honest. I'm trying to figure out any 'best practices' as the last time I worked with a database with knowledge was with cobol.

Link to comment
Share on other sites

Thanks for the answers. Then am I better off breaking them off into my 2 different objects server or client side? Sorry if what I'm trying to do isn't clear. I could get all the records (questions & choices) and break them apart -- Or I could execute a query for each question to get the choices.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.