tags:

views:

38

answers:

3

Hello,

Does anyone know how to retrieve a piece of data and display the results in php file? A similar query that I would enter is something like this:

SELECT 'email' FROM 'users' WHERE 'username' = 'bob'

Thus, the result would be just the email.

Thanks,

Kevin

+4  A: 

To connect

$con = mysql_connect("host","userName","passWord");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("dbNameToSelect", $con);

To query

$result = mysql_query("SELECT `email` FROM `users` WHERE `username` = 'bob'");
while($row = mysql_fetch_array($result)){
   // do stuff with $row['colName']
}

To close connection mysql_close($con);

Babiker
How would i connect to a database?
Kevin
A: 

See also: http://www.php.net/manual/en/mysqli.query.php There are a few good examples

Radek Suski
A: 

You need to connect and fetch data from a database, like mysql. Check the code below:

$server = "localhost";
$username = "root";
$password = "";
$database = "mysampledb";

mysql_connect($server, $username, $password);
mysql_select_db($database);

$result = mysql_query("SELECT email FROM users WHERE username = 'bob'") or die(mysql_error());

while($row = mysql_fetch_assoc($result)) {
    echo $row["email"];
}
Ioannis Karadimas