johnber Posted September 22, 2018 Share Posted September 22, 2018 Hi Fairly new to PHP and have the following code which should simply output into 3 columns - I have checked the query which does work - so I dont understand why I get this error mysql_fetch_array() expects parameter 1 to be resource, // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = “SELECT * FROM duration_matched WHERE groupm = '”.$coincidence ."’"; $result = $conn->query($sql); // set up loop counter $col_count = 0; // start table and first tr echo ‘ ’; while ($row=mysql_fetch_array($result)) { // if you have output 3 cols then end tr and start a new one if ($col_count == 3) { echo ‘ ’;// and reset the col count$col_count = 0;} // always output the td echo ‘ ’;// and count the column$col_count++;} // then close off the last row and the table echo ‘ ’ . $row[‘image’] . ‘ ’; $conn->close(); thanks in advance John B Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 22, 2018 Share Posted September 22, 2018 1 - You opened a connection using the mysqlI interface 2 - You trying to use the MySQL interface to do all the rest of your db work. In case you haven't looked at the manual lately - MySQL is deprecated, as in, "DON'T USE". Quote Link to comment Share on other sites More sharing options...
johnber Posted September 22, 2018 Author Share Posted September 22, 2018 err thanks - what should I use instead (sorry, newbie to this) John Quote Link to comment Share on other sites More sharing options...
Barand Posted September 22, 2018 Share Posted September 22, 2018 6 minutes ago, johnber said: what should I use instead How about mysqli_fetch_array($result), or $result->fetch_array() Have you considered using the manual at php.net Quote Link to comment Share on other sites More sharing options...
johnber Posted September 22, 2018 Author Share Posted September 22, 2018 thanks for the info - I will have a read Regards john B Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 22, 2018 Share Posted September 22, 2018 I would check with my service provider to see what interfaces they support for php. If the PDO one is available then go directly to the pdo chapter in the php manual and learn that instead of mysqlI. It is not hard to pick up and will serve you better in the long run IMHO. Quote Link to comment Share on other sites More sharing options...
benanamen Posted September 22, 2018 Share Posted September 22, 2018 (edited) I agree with @ginerjmm, learn PDO instead. This tutorial will get you going. https://phpdelusions.net/pdo You can also download my PDO Bumpstart Database. It will help you get going quickly with the basics. https://github.com/benanamen/pdo_bumpstart_ver1.6 Edited September 22, 2018 by benanamen Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 22, 2018 Share Posted September 22, 2018 (edited) Looking at your loop logic I don't understand what you think you are getting with the fetch (btw- use 'fetch_assoc' instead of array). Why do you need a column counter? Aren't you just taking the query results row by row and outputting them to an html table with one query row in one html table row? See if this makes sense. And of course, learn how to use PDO instead of MySQL_* functions // don't use "select *". Better to use actual field names. $sql = "SELECT field1, field2, field3, field4, field5 FROM duration_matched WHERE groupm = '" . $coincidence . "'"; $result = $conn->query($sql); // start table and first tr echo '<table><tr>'; echo '<th>column 1</th><th>column 2</th><th>column 3</th><th>column 4</th><th>column 5</th>'; echo '</tr>'; // now output the rows of data while ($row=mysql_fetch_array($result)) { echo '<tr>'; echo "<td>{$row['field1']}</td>"; echo "<td>{$row['field2']}</td>"; echo "<td>{$row['field3']}</td>"; echo "<td>{$row['field4']}</td>"; echo "<td>{$row['field5']}</td>"; echo "</tr>"; } // then end the table echo "</table>"; Edited September 22, 2018 by ginerjm 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.