Is there any way to shorten this code:
$resultCategoryName = mysql_query("SELECT ecname FROM electioncategorymaster WHERE ecid=".$Category);
$rowCategoryName = mysql_fetch_row($resultCategoryName);
$CategoryName = $rowCategoryName[0];
Is there any way to shorten this code:
$resultCategoryName = mysql_query("SELECT ecname FROM electioncategorymaster WHERE ecid=".$Category);
$rowCategoryName = mysql_fetch_row($resultCategoryName);
$CategoryName = $rowCategoryName[0];
I don't think it can be reduced any further. All functions are performing their task at the very minimal level. You can only shorten variable names :)
I'm no PHP guru, but maybe you should either be using a framework or a class for each table.
I don't think that code can be reduced per se.
$rowCategoryName = mysql_fetch_row(mysql_query("SELECT ecname FROM electioncategorymaster WHERE ecid=$Category"));
$CategoryName = $rowCategoryName[0];
That's the shortest I can think of. You really should do SELECT ecname FROM electioncategorymaster WHERE ecid='$Category'"
though (putting single quotes around the variable).
No. Due to a flaw in PHP the following is not possible:
$CategoryName = mysql_fetch_row($resultCategoryName)[0];
Perhaps you should look into using an ORM of some sort. Zend_Db has a method for grabbing a single value from a query.
$ecname = $db->fetchOne("SELECT ecname FROM electioncategorymaster WHERE ecid = ?", $Category);
you can use mysql_result to get categroyName in 2 steps.
$resultCategoryName = mysql_query("SELECT ecname FROM electioncategorymaster WHERE ecid=".$Category);
$categoryName = mysql_result($resultCategoryName, 0); //Extract First column
Here is the reference link.
This is the way I do it. I use sprintf to insure that only numeric values are passed as id. I also append SQL sentance with LIMIT 1 to ensure that only one record is fetched.
$r = mysql_fetch_row(mysql_query(
sprintf("SELECT * FROM <sometable> WHERE id = %d LIMIT 1",intval($id))
,$connection));
If you are just asking to shorten the three lines into one line:
list($CategoryName) = mysql_fetch_row(mysql_query("SELECT ecname FROM electioncategorymaster WHERE ecid=".$Category));
But of course you shouldn't really be heading that way. More readable lines is always better than cryptic one-liners. And more especially if you are charging your client based on number of lines .. :)
p/s: two-liners version:
$resultCategoryName = mysql_query("SELECT ecname FROM electioncategorymaster WHERE ecid=".$Category);
list($CategoryName) = mysql_fetch_row($resultCategoryName);
The trick is to use list().