PHP: Connect to a MySQL database, run a query and read a field

Quick reference for how to read a single field from a MySQL database in PHP: –

// Connect to the database
$mysqli = new mysqli(my.server.com', 'schemaName', 'password', 'databaseName') or die ('<b>Could not connect: </b>' . mysql_error());

// Query the database for the info we need
$results = $mysqli->query("SELECT * FROM myData WHERE id='$id'") or die($mysqli->error);

$content = $results->fetch_assoc();
$field = $content['fieldName'];

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s