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

Fetching data from Mysql

This is my code. It is showing error always. I have checked everything is fine with the details. Please Help. <?php

$db = new PDO("mysql:host=localhost;dbname=moodle","root","");

$sql = mysql_query("SELECT * FROM mdl_user_enrolments");

if (!$sql) { die('Invalid query: ' . mysql_error()); } while($result = mysql_fetch_array($sql)) { echo " <b>id</b> = " .$result["id"] . " <br>"; echo " <b>Name</b> = " .$result["timestart"] . " <br>"; echo " <b>Age</b> = " .$result["timeend"] . " <br><br>----------------<br><br>"; }

?>

8 Answers

I think you forgot to pass the connection to mysql_query function.

$sql = mysql_query("SELECT * FROM mdl_user_enrolments",$db);

I hope it helps, good luck!

Warning: mysql_query() expects parameter 2 to be resource

It refers to the connection to the database, wich is stored in the $db variable.

I have tried what you suggested but it is giving this warning. Can you please give a sample code in which a PDO is created and we are fetching data from the database. On the other hand below given code is working fine: but i want to USE PDO. <?php //connect to server

$conn = mysql_connect("localhost","root",""); if (!$conn) { die('Could not connect to Server: ' . mysql_error()); } $db_selected = mysql_select_db("moodle",$conn); if (!$db_selected) { die ('Can\'t Connect to Database : ' . mysql_error()); } $sql = mysql_query("SELECT * FROM mdl_user_enrolments"); if (!$sql) { die('Invalid query: ' . mysql_error()); } while($result = mysql_fetch_array($sql)) { echo " <b>id</b> = " .$result["id"] . " <br>"; echo " <b>Name</b> = " .$result["timestart"] . " <br>"; echo " <b>Age</b> = " .$result["timeend"] . " <br><br>----------------<br><br>"; }

?>

Oh i see, check the links below, one is straight from PHP.net and the other is a more extended tutorial about using PDO.

Connections and Connection Management

PDO Toturial

It is still not working...:(

Ok, i've review the code again, try using the code below, it's generic but is the way you retreive data with PDO:

<?php

try
{
    $db = new PDO("mysql:host=localhost;dbname=moodle","root","");

    $sql = mysql_query("SELECT * FROM mdl_user_enrolments");
    $result = $db->query($sql);

    //Display all rows
    foreach($result as $row){
        print_r($row);
    }

    //Close the connection
    $db = null;
}catch(PDOException $e){
    echo $e->getMessage();
    die();
}


?>

You might need to modifiy acoording to your needs, i hope it helps.

Here's the manual for PDO usage from PHP.net PDO, remember that PDO has it's own methods and does not use mysql_fecth_array() nor mysql_query() functions to retreive the data, it has other ways to acomplish those tasks.

It shows this error: Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\database.php on line 13

Sorry, the copy paste method always cause me mistakes:

try
{
    $db = new PDO("mysql:host=localhost;dbname=moodle","root","");

    $sql = 'SELECT * FROM mdl_user_enrolments';
    $result = $db->query($sql);

    //Display all rows
    foreach($result as $row){
        print_r($row);
    }

    //Close the connection
    $db = null;
}catch(PDOException $e){
    echo $e->getMessage();
    die();
}


?>

I didn't remove the mysql_query function, try this code, it should work.

Thanks a lot dear it worked...:)

Cool, glad to help!

Can you please help in one more thing. Please tell me how can i do the same if my database is SQL SERVER 2008 R2. Thanks in Advance.

Hi i'm not using SQL Server for any of my projects and according to the documentation it seems there's little support for SQL Server in PHP. I've found a tutorial which i think you could try to follow, i hope it helps you, if you have problems installing something or moving some configuration just drop me a message here or my mail huango333@gmail.com and i'll tell you what to do.

Here is the link Using Microsoft SQL Server 2008 R2 With PHP’s PDO and Apache

The link to the drivers page seems to be not working so use this please http://www.microsoft.com/en-us/download/details.aspx?id=20098

In order to be able to use SQL Server you will need to install a driver and then try it if it works.

Good luck!