Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Jonathan Wakely
Jonathan Wakely
32 Points

Using Mamp (default settings), how do I test my php connection to my MySQL Database?

So as the above title states, I'm just trying to connect to my Mysql DB* via a php script (below), but all I am seeing is this result output in my browser:

" \n"; } if (!$tblCnt) { echo "There are no tables \n"; } else { echo "There are $tblCnt tables \n"; } ?> "

Please help me!

  • J

===============

<?php

Fill our vars and run on cli

$ php -f db-connect-test.php

$dbname = 'learning'; $dbuser = 'root'; $dbpass = ''; $dbhost = 'localhost'; $link = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'"); mysqli_select_db($link, $dbname) or die("Could not open the db '$dbname'"); $test_query = "SHOW TABLES FROM $dbname"; $result = mysqli_query($link, $test_query); $tblCnt = 0; while($tbl = mysqli_fetch_array($result)) { $tblCnt++;

echo $tbl[0]."<br />\n";

} if (!$tblCnt) { echo "There are no tables<br />\n"; } else { echo "There are $tblCnt tables<br />\n"; } ?>

===============