Categories
Database MySQL PHP

Pulling a single value from the database

Sometimes you just want to pull a single value from the database without a whole lot of work.  Here's how to do it in a single line;

MySQLi

// Assumes that a mysqli connection ($db) has been made to the database
$value = $db->query("SELECT value FROM table WHERE condition = 'met'")->fetch_row()[0];

MySQL (Deprecated)

// Assumes that a connection exists to the database (mysql_connect()) and a database is selected (mysql_select_db())
$value = mysql_result(mysql_query("SELECT value FROM table WHERE condition = 'met'"), 0);

Leave a Reply