nodirtyrockstar Posted January 8, 2013 Share Posted January 8, 2013 I'm using PHP 5.3.13. I am just using a very simple query to grab one column of results from a table. I would like to fetch them and then iteratively add them to a dropdown menu. I am trying to understand these methods/functions and seem to be missing something. My research on Google didn't give me any indication of what I'm doing wrong. I start with one of the simplest queries possible, which I know will return a data set as I have tested it in mysqladmin. The problem arises when I try to call the method on the mysqli_result object. $query = "SELECT `artist` FROM `bands`;"; $result = $mysqli->query($query); $bandArr = $result->fetch_all(); The error I'm getting from the above code is: Fatal error: "Call to undefined method mysqli_result::fetch_all()..." I researched this error and read somewhere that you need mysqlnd. Is that true? Do I need to look into my PHP configuration to get this to work? Is it worth it for this task? Then I tried fetch_array... $query = "SELECT `artist` FROM `bands`;"; $result = $mysqli->query($query); $bandArr = $result->fetch_array(MYSQLI_NUM); printf("%s\n%s", $bandArr[0], $bandArr[1]); And the above code for some reason returns an array with only one item, and this error: "Notice: Undefined offset: 1 in..." What am I missing here? Again, all I want is a small result set from one column which can be iterated and each value added to a drop down menu. Thoughts? Suggestions? Thanks in advance for any help you can offer... Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 8, 2013 Share Posted January 8, 2013 (edited) Try this: $bandArr = $result->fetch_array(MYSQLI_NUM); echo '<pre>' . print_r($bandArr,true) . '</pre>'; What do you get? EDIT: Actually, the undefined offset error is because your query is only returning a single column. Edited January 8, 2013 by scootstah Quote Link to comment Share on other sites More sharing options...
DavidAM Posted January 9, 2013 Share Posted January 9, 2013 fetch_array() will return only one row. You need to use it in a loop to process all of the rows: $query = "SELECT `artist` FROM `bands`;"; $result = $mysqli->query($query); while ($bandArr = $result->fetch_array(MYSQLI_NUM)) { printf("%s\n", $bandArr[0]); } Of course, since you are only selecting one column, there will only be one element in the $bandArr array (it will be element zero). Quote Link to comment Share on other sites More sharing options...
nodirtyrockstar Posted January 10, 2013 Author Share Posted January 10, 2013 I am sure you're right. I'll see if I can get this working. Can anyone answer this for me: what is the issue with fetch_all()? Quote Link to comment Share on other sites More sharing options...
Solution DavidAM Posted January 11, 2013 Solution Share Posted January 11, 2013 The error I'm getting from the above code is: Fatal error: "Call to undefined method mysqli_result::fetch_all()..." I researched this error and read somewhere that you need mysqlnd. Is that true? Do I need to look into my PHP configuration to get this to work? Is it worth it for this task? Can anyone answer this for me: what is the issue with fetch_all()? mysqli_fetch_all() is only available with the native driver. I've never used it (fetch_all(), that is). I've never really wanted to use it. Even for populating dropdown selections, I just use a loop. I don't think you will see enough benefit to justify the time and effort to reconfigure PHP. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.