SELECT 1 row: PHP/MySQL
This has been on my mind...
I have always used this (for the most part) to select data in a DB...
Now, if I only want to SELECT one row of data (ie. WHERE id='123') is the above still efficient (or 'good coding') to use? If not, what is the better method.
$connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect!");
$query = "SELECT * from $table.......";
$result = mysql_db_query($database, $query, $connection) or die ("Could not execute query: $query. " . mysql_error());
while(list($id, $name, $address, $city,.......) = mysql_fetch_row($result))
// display data
You could do it this way..
I hope this helps. This is only one way of doing this, there is another ten different ways it could be done so have a search around, you never know what you might find under some rock!
var $dbConnection = mysql_connect($hostname, $user, $pass);
mysql_select_db ($dbName, $dbConnection);
else return false;
// The above code could sit in an include file.
// You can set up some routine to check the connection works like
// $db = dbConnect();
// if (!$db) echo 'Lookout the database connection is dead!';
// but assuming it works you can just do this whenever needed
$db = dbConnect();
// You should avoid using the 'All' operator (*) for speed of execution
// instead, call the individual field names. You should also try to avoid using
// variables inside double quotes ("$table"), instead use concatanated
// single quote strings ('. $table .')
$selectStatment = 'SELECT field1, field2, field3 FROM '. $table .' WHERE id=123';
$recordset = mysql_query($selectStatment, $db);
// If you know that it will only return one row then you can go...
$rowArray = mysql_fetch_row($recordset);
echo $rowArray .' | '. $rowArray .' | '. $rowArray;
// otherwise you could do a loop like this...
while ($rowArray == mysql_fetch_row($recordset))
echo $rowArray .' | '. $rowArray .' | '. $rowArray .'<br>';