开发者

How to reduce this code?

Is there any way to shorten this code:

$resultCategoryName = mysql_query("SELECT ecname FROM electioncategorymaster WHERE ecid=".$Category);
$rowCategoryName = mysql_fetch_row($result开发者_Python百科CategoryName);
$CategoryName = $rowCategoryName[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);


No. Due to a flaw in PHP the following is not possible:

$CategoryName = mysql_fetch_row($resultCategoryName)[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 :)


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜