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

connect and retrieve from database

I am running MAMP on a macbook and I can't seem to get any info to display in my website(blank for testing). I don't know what I am doing wrong and I have looked it up numerous on how to do it, but still the info won't echo to the screen.

$con = mysql_connect('localhost', 'root', 'root') or die("Could not connect to the database");

            $db = msql_select_db('test') or die("Could not select the database");

            $result = mysql_query('SELECT * FROM users WHERE last_name = "bush"') or die("could retrieve data from table");

            while($row = mysql_fetch_assoc($result)) {
                echo $row['first_name']."<br>";
                echo $row['last_name']."<br>";
                echo $row['age'];
            }

4 Answers

You should really be using PDO to connect to the database. Randy Hoyt teaches how to use it in the PHP and MySQL project.

Yes, I did try with PDO as well and get the same problem of info not echo'ing

I think you actually have to echo the stuff like this

echo $row[0]['first_name'];

Still tho, don't use this. It's deprecated.

Actually I don't think that will work. check this out http://us3.php.net/manual/en/ref.pdo-mysql.php or just watch the project. I really don't remember how to use mysql_connect().

wow, started looking over PDO code I had and wow do I feel dumb. spelled query with a g. Had an echo "some text here" exit; in one line and forgot even though it was in 1 line the echo needed a ; at the end.

lol funny realizations that make you laugh.....

Lol, we all make those mistakes... All the time.

It's in a while loop so I don't think I need to include the index?

I'll list my PDO statement code when I get home. But I still run into the same issue of it not displaying. I could be missing something that I'm forgetting to do