i wrote php script to connect to database ,how can i change script that script will show also names of fields ? /* Connecting, selecting database */ $CON = MYSQLI_CONNECT("localhost","user1","12345","videostir_2"); IF (MYSQLI_CONNECT_ERRNO()) // CHECK CONNECTION { ECHO "FAILED TO CONNECT TO MYSQL: " . MYSQLI_CONNECT_ERROR(); } $query = "SELECT * FROM stats"; /* Performing SQL query */ $result = mysqli_query($CON,$query) or die("Query failed"); print "<table>\n"; // Printing results in HTML while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) { print "\t<tr>\n"; foreach ($line as $col_value) { print "\t\t<td>$col_value</td>\n"; } print "\t</tr>\n"; } print "</table>\n"; mysqli_free_result($result); // Free result set mysqli_close($link); // Closing connection ?> Code (markup):
Here's a link that will tell you how you can fetch these values: http://php.net/manual/en/mysqli-result.fetch-fields.php
I don't quite understand you, hope this helps. http://php.net/manual/en/function.each.php If it doesn't just tell me, I'll try to help
Using mysqli, html tables, login info in script, and using die() to return an error..... I don't know...I think you should start over by writing modern secure php code..
1) Stop using the functional version, it's just a wrapper that adds overhead to it. 2) why are you using print? 3) why are you declaring your \n when you could just format the output? 4) which would mean you could have proper output instead of that double-quote asshattery. 5) methods are not uppercase, there are namespace 'rules' you should probably be using for PHP, going ape-shit with caps isn't it. Lemme guess, used to write Visual Basic for a living? I'm guessing wildly here, but if I'm following what you want, it would go something like this: <?php $mysqli = new mysqli('localhost','user1','12345','videostir_2'); if ($mysqli->connect_errno()) die( 'MySQL Connection Failed: ' . $mysqli->connect_error() ); if ($result = mysqli_query('SELECT * FROM stats')) { if ($row = $result->fetchObject()) { echo ' <table> <caption>Contents of <b>stats</b> Table</caption> <thead> <tr>'; foreach ($row as $name => $data) echo ' <th scope="col">', $name, '</th>'; echo ' </tr> </thead><tbody>'; do { echo ' <tr>'; foreach ($row as $data) echo ' <td>', $data, '</td>'; echo ' </tr>'; } while ($row = $result->fetch_object()); echo ' </tbody> </table>'; } $result->close(); } $mysqli->close(); ?> Code (markup): You may want to add an "else" statement when the row fetch fails to say "nothing to show". The big magic is the do/while construct and running foreach twice, and understanding how to parse the result using => That should give you a properly organized/constructed table with the formatted whitespace, with proper headings and scope.