Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Brian Bush
Brian Bush
4,398 Points

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

Matthew Reed
PLUS
Matthew Reed
Courses Plus Student 17,986 Points

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

Brian Bush
Brian Bush
4,398 Points

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

Matthew Reed
PLUS
Matthew Reed
Courses Plus Student 17,986 Points

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.

Matthew Reed
Matthew Reed
Courses Plus Student 17,986 Points

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().

Brian Bush
Brian Bush
4,398 Points

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.....

Brian Bush
Brian Bush
4,398 Points

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