1

I know the basics of HTML, and nothing about PHP. I've found a ton of tutorials, but I couldn't get a single one to work. I have a mySQL database working and apache running. How can I display an entire table on my page? I do not know the column names ahead of time.

Edit: Added and adjusted the code. My index page shows this: connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } //$sql = "SHOW TABLES"; $sql = "select * from cpu"; //edit your table name here $res = $mysqli->query($sql); while ($row = $res->fetch_assoc()) { print_r($row); } ?>

This is my entire index.html source:

<?php $mysqli = new mysqli("192.168.1.2", "webuser", "*****", "OCN"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } //$sql = "SHOW TABLES"; $sql = "select * from cpu"; //edit your table name here $res = $mysqli->query($sql); while ($row = $res->fetch_assoc()) { print_r($row); } ?> 

Once I changed my file to .php, the error message was more clear. It reminded me that I didn't have webuser at 192.168.1.2. I created the user again and gave the correct permissions. The function appears to work, but I'll have more time in a few hours to look at it.

5
  • w3schools.com/php/php_mysql_select.asp read this it's just need letle adjustment to makebit work for you Commented May 16, 2015 at 11:49
  • I've come across this page before. What is the adjustment? Again, I know next to nothing about PHP. Commented May 16, 2015 at 11:51
  • Just a quick notice, if your file is really called index.html then you should rename it to index.php since it contains php code. Commented May 16, 2015 at 12:17
  • After changing that, it made the error message clear. I was able to figure out it was denying me access, not that it was unable to find the server. Commented May 16, 2015 at 12:33
  • You have a mysql server running on a different machine on your lan? If Apache and MySQL are on the same machine change ip to localhost or 127.0.0.1 Commented May 16, 2015 at 14:38

3 Answers 3

2

The real problem here is that you see connect_errno) { echo "Failed to... on your page, instead of a line beginning with Failed to... only.

Because your browser thinks that the code from <?php to $mysqli-> is an HTML tag (because of the opening and closing <>, I think your file is a .htm(l) file rather than a .php file.

Just change the file extension and if PHP is installed correctly it should work.

Sign up to request clarification or add additional context in comments.

Comments

0
<?php $mysqli = new mysqli("localhost", "root", "", "database"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } //$sql = "SHOW TABLES"; $sql = "select * from tbl_comment"; //edit your table name here $res = $mysqli->query($sql); while ($row = $res->fetch_assoc()) { print_r($row); } ?> 

3 Comments

@TylerMontney looks your code unable to connect with 192.168.1.2 try localhost if you are working with XAMPP or WAMP
Hmm, that shouldn't be. My VB app has no issue connecting. My database is not running on the same PC.
according to error you are getting only means "Failed to connect to MySQL:" you are unable to connect with database. please double check mysql connection.
0

From what I understood. You want to display values but you don't know column names. If that's so, try this.

 $mysqli = new mysqli("192.168.1.2", "webuser", "*****", "OCN"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } //$sql = "SHOW TABLES"; $sql = "select * from cpu"; //edit your table name here $res = $mysqli->query($sql); while ($row = $res->fetch_assoc()) { foreach($row as $ind => $val) { echo "Column name: $ind, Column Value: $val<br />"; } echo "<hr />"; } ?> 

1 Comment

Issue I'm having at the moment is I can't connect. I don't know why because I'm not having trouble connecting in other ways. Once I can fix that, I'll try this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.