rbush Posted July 14, 2014 Share Posted July 14, 2014 Hello, I know this should be pretty simple to figure out, but everything I try is giving me absolutely no results. I have a mysql query selecting columns from my database and returning results. I have the results printing out right now, so I can see that this part is working. All I want to do is take the results and put them into a table to display on my page. Basically, take what's in the database table and copy it to a table I can put on the web. FYI I am using sourcerer so the "connect" code is taken care of for me in the "JFactory" bit of code. Here is the first part of my code, selecting the information from the database. {source} <?php $db = JFactory::getDbo(); $query = $db -> getQuery(true); $query -> SELECT($db -> quoteName(array('first_dept_name', 'last_name', 'dept', 'position', 'phone_num'))); $query -> FROM ($db -> quoteName('#__custom_contacts')); $query -> ORDER ('first_dept_name DESC'); $query -> WHERE ($db -> quoteName('contact_category')."=".$db -> quote('YTown Employees')); $db -> setQuery($query); $results = $db -> loadObjectList(); print_r($results); Here is where I am trying to print the results into a table. I got this code directly from a PHP book, but I am getting nothing at all returned back to me. I get table headers, but no data. <?php echo "<table><tr><th>Name</th><th>Department</th></tr>"; while ($row = mysqli_fetch_array ($result)){ echo "<tr>"; echo "<td>".$row['last_name']."</td>"; echo "<td>".$row['dept']."</td>"; echo "</tr>"; } echo "</table>"; ?> {/source} Quote Link to comment https://forums.phpfreaks.com/topic/289866-display-query-results-in-table/ Share on other sites More sharing options...
tHud Posted July 14, 2014 Share Posted July 14, 2014 Should this while ($row = mysqli_fetch_array ($result)){ read $results ? Quote Link to comment https://forums.phpfreaks.com/topic/289866-display-query-results-in-table/#findComment-1485013 Share on other sites More sharing options...
Ch0cu3r Posted July 14, 2014 Share Posted July 14, 2014 (edited) Why are you using mysqli_fetch_assoc when you're using Joomla's database library? The only time you'd use mysqli_fetch_*() function would be if you had ran a query using mysqli_query(). Quote from Joomla's documentation on the use of loadObjectList Method to get an array of the result set rows from the database query where each row is an object. The array of objects can optionally be keyed by a field name, but defaults to a sequential numeric array. From that I'd hazard a guess this would be the correct way for looping over the results returned by that method. <?php echo "<table><tr><th>Name</th><th>Department</th></tr>"; // loop over results return from loadObjectList() foreach($results as $row) { echo "<tr>"; echo "<td>".$row->last_name."</td>"; echo "<td>".$row->dept."</td>"; echo "</tr>"; } echo "</table>"; ?> Edited July 14, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/289866-display-query-results-in-table/#findComment-1485016 Share on other sites More sharing options...
Solution rbush Posted July 14, 2014 Author Solution Share Posted July 14, 2014 Why are you using mysqli_fetch_assoc when you're using Joomla's database library? The only time you'd use mysqli_fetch_*() function would be if you had ran a query using mysqli_query(). Quote from Joomla's documentation on the use of loadObjectList From that I'd hazard a guess this would be the correct way for looping over the results returned by that method. <?php echo "<table><tr><th>Name</th><th>Department</th></tr>"; // loop over results return from loadObjectList() foreach($results as $row) { echo "<tr>"; echo "<td>".$row->last_name."</td>"; echo "<td>".$row->dept."</td>"; echo "</tr>"; } echo "</table>"; ?> I just realized that didn't make sense myself. Maybe that is part of the problem. I will give that a try. Quote Link to comment https://forums.phpfreaks.com/topic/289866-display-query-results-in-table/#findComment-1485021 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.