Solved

How to bind variables using mysqli for unknown number of field

Posted on 2010-08-25
6
386 Views
Last Modified: 2013-12-13
My prepare statement: "select * from tblname";
How do I bind the number of unknown field being retrieved?
It could be 2 or 10.

Help!


I know how to bind if I know that it is going to return 2 fields (see code below).
How do I bind it if I don't know how many fields are being retrieved so that I can bind the result and then fetch all the values???




if ($sql = $mysqli->prepare("SELECT * FROM lindatest")) {
$sql->execute();

/* bind variables to prepared statement */
$sql->bind_result($col1, $col2);

/* fetch values */
while ($sql->fetch()) {
printf($col1." and ".$col2."<br />");
}

/* close statement */
$sql->close();
}
0
Comment
Question by:bigjdve
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 4

Assisted Solution

by:darkyin87
darkyin87 earned 250 total points
ID: 33524185
0
 
LVL 3

Author Comment

by:bigjdve
ID: 33524751
That is helpful.  However, how do I bind the variables if let see there are 10 fields that was returned.
Do I have to have 10 bind variables ($sql->bind_result($col1, $col2...$col10)??
0
 
LVL 4

Accepted Solution

by:
darkyin87 earned 250 total points
ID: 33525690
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 3

Author Comment

by:bigjdve
ID: 33525967
darkyin887 -- again, thank you.  I actually figure this out already.  But thank you.  However, I am now stuck on how to retrieve my data.  I used a while loop to go through my resultset to get my data.  This table only have 2 fields (date, value).  To retrieve the date field, I need to specified that exact field (i.e. $result['date']).  Is there a way to retrieve this field without specifying the field name.

See my code:

if ($sql = $mysqli->prepare("SELECT * FROM lindatest")) {
    $sql->execute();


      $sql->store_result();
      
      $meta = $sql->result_metadata();

      $bindVarsArray = array();
      
      while ($column = $meta->fetch_field()) {
         $bindVarsArray[] = &$results[$column->name];
      }      
      call_user_func_array(array($sql, 'bind_result'), $bindVarsArray);
      
      $sql->fetch();

      while ($sql->fetch())
      {            
            echo "here= ".$results['value']." <br>";
            
      }

      $sql->close();
}
0
 
LVL 4

Expert Comment

by:darkyin87
ID: 33526894
I am sorry.. I think that is not possible.
0
 
LVL 3

Author Closing Comment

by:bigjdve
ID: 33664253
solution was resolved with the call_user_func
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question