display query result in a table using PHP
-
here's my code... this is intended to display a query result in a table but i am getting unwanted characters and no table was displayed... what could be wrong with my codes? here it is... thanks in advance.. <html> <body> <?php if (isset($_POST['btnSearch'])) { //connection to the database $connection = mysql_connect('localhost', 'root', ''); if (!$connection) die(mysql_error()); //select database if (!mysql_select_db('bbs', $connection)) die(mysql_error); $SearchOpt = $_POST['SearchOption']; if (empty($SearchOpt)) die("You did not select field."); $SearchKey = $_POST['txtKey']; if (empty($SearchKey)) die("You did not enter a keyword."); $sqlQuery = mysql_query("SELECT * FROM t_board WHERE $SearchOpt LIKE '%$SearchKey%' ORDER BY registerTime DESC;"); if(!$sqlQuery) die(mysql_error()); if (mysql_num_rows($sqlQuery)) { echo "<tbody>"; $recs = mysql_fetch_array($sqlQuery) or die(mysql_error()); foreach($recs as $rec) { echo "<tr>"; echo "<th>" . $rec['index'] . "</th>"; echo "<th>".$rec['subject']."</th>"; echo "<th>".$rec['writer']."</th>"; echo "<th>".$rec['registerTime']."</th>"; echo "</tr>"; } echo "</tbody>"; } } ?> </body> </html>
-
here's my code... this is intended to display a query result in a table but i am getting unwanted characters and no table was displayed... what could be wrong with my codes? here it is... thanks in advance.. <html> <body> <?php if (isset($_POST['btnSearch'])) { //connection to the database $connection = mysql_connect('localhost', 'root', ''); if (!$connection) die(mysql_error()); //select database if (!mysql_select_db('bbs', $connection)) die(mysql_error); $SearchOpt = $_POST['SearchOption']; if (empty($SearchOpt)) die("You did not select field."); $SearchKey = $_POST['txtKey']; if (empty($SearchKey)) die("You did not enter a keyword."); $sqlQuery = mysql_query("SELECT * FROM t_board WHERE $SearchOpt LIKE '%$SearchKey%' ORDER BY registerTime DESC;"); if(!$sqlQuery) die(mysql_error()); if (mysql_num_rows($sqlQuery)) { echo "<tbody>"; $recs = mysql_fetch_array($sqlQuery) or die(mysql_error()); foreach($recs as $rec) { echo "<tr>"; echo "<th>" . $rec['index'] . "</th>"; echo "<th>".$rec['subject']."</th>"; echo "<th>".$rec['writer']."</th>"; echo "<th>".$rec['registerTime']."</th>"; echo "</tr>"; } echo "</tbody>"; } } ?> </body> </html>
You might want to try changing this:
$sqlQuery = mysql_query("SELECT * FROM t_board WHERE $SearchOpt LIKE '%$SearchKey%' ORDER BY registerTime DESC;");
to this:$sqlQuery = mysql_query("SELECT * FROM t_board WHERE $SearchOpt LIKE '%" . $SearchKey . "%' ORDER BY registerTime DESC;");
Also, make sure your request is POSTing the correct parameters: How many records exist in $recs after you run the query? Tryprint count($recs);
-
here's my code... this is intended to display a query result in a table but i am getting unwanted characters and no table was displayed... what could be wrong with my codes? here it is... thanks in advance.. <html> <body> <?php if (isset($_POST['btnSearch'])) { //connection to the database $connection = mysql_connect('localhost', 'root', ''); if (!$connection) die(mysql_error()); //select database if (!mysql_select_db('bbs', $connection)) die(mysql_error); $SearchOpt = $_POST['SearchOption']; if (empty($SearchOpt)) die("You did not select field."); $SearchKey = $_POST['txtKey']; if (empty($SearchKey)) die("You did not enter a keyword."); $sqlQuery = mysql_query("SELECT * FROM t_board WHERE $SearchOpt LIKE '%$SearchKey%' ORDER BY registerTime DESC;"); if(!$sqlQuery) die(mysql_error()); if (mysql_num_rows($sqlQuery)) { echo "<tbody>"; $recs = mysql_fetch_array($sqlQuery) or die(mysql_error()); foreach($recs as $rec) { echo "<tr>"; echo "<th>" . $rec['index'] . "</th>"; echo "<th>".$rec['subject']."</th>"; echo "<th>".$rec['writer']."</th>"; echo "<th>".$rec['registerTime']."</th>"; echo "</tr>"; } echo "</tbody>"; } } ?> </body> </html>