0

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

3 Answers 3

4

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);

Sign up to request clarification or add additional context in comments.

1 Comment

How would i connect to a database?
1

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"];
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.