tags:

views:

139

answers:

8

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];
+1  A: 

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

Sarfraz
A: 

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.

metrobalderas
A: 
$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).

Brendan Long
No, no, no! You don't pass parameters to the database that way!
DrJokepu
Why was this voted down? It's true. The fact that doing it isn't particularly helpful doesn't change the fact that it's the only real answer to the question.
Brendan Long
@Brendan Long: It wasn't me who downvoted it but probably due to the SQL injection issue.
DrJokepu
Why not? There's no difference between `$x = "blah blah". $blah;` and `$x = "blah blah $blah";`
Brendan Long
And I'm assuming (probably incorrectly) that the variable is already escaped earlier. The part where I said to add single quotes was specifically to address the injection problem.
Brendan Long
@Brendan Long: Exactly. Both are inherently wrong.
DrJokepu
Even if it's escaped, it makes query caching harder as the database can't reuse previous queries that only differ in the value of the parameter.
DrJokepu
What do you mean? If I escape $var, then run `mysql_query("SELECT * FROM x WHERE var='$var'` it should be safe, right?
Brendan Long
If you are going the string interpolation route (meaning that you trust $var is properly escaped), then at least lose the quotes, as they introduce an extra cast from varchar to number.
Thilo
It is safe but performance-wise, it is far from being optimal. In your example, for every different value of $var the database has to execute a whole new query. If you have parametrized queries, the database can reuse the first query as the only thing that changed was the value of a parameter, hence subsequent queries being faster.
DrJokepu
+2  A: 

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

$CategoryName = mysql_fetch_row($resultCategoryName)[0];
Ignacio Vazquez-Abrams
I wouldn't call it a flaw... And it's planned AFAIK.
Alix Axel
+3  A: 

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);
hobodave
@jspcal: hobodave version of binding is more concise, which is what the OP was looking for in the first place. The only way people will use bind variables (short of hitting them for not doing it), is if there is no extra typing involved.
Thilo
@thilo: the second way is more conscise if you have an assoc array
jspcal
@jspcal: Don't think so, you can do `$db->fetchOne('select ecname from electioncategorymaster where ecid = ? OR foo = ?', array($Category, $foo));`
Alix Axel
@jspcal: In that case you're right.
Alix Axel
@jspcal: please keep personal attacks and off-topic "stuff" out of comments. It degrades SO.
hobodave
@hobodave: you are the one downvoting as a means of retaliation because you didn't like my comment. ive contacted the mods.
jspcal
+1  A: 

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.

Adeel
Make sure to read the part saying how slow it is for multiple queries though.
Brendan Long
@Brendan: That does not seem to apply here, though. He only needs a single column.
Thilo
Yeah I know, I just pointed it since it's a good thing to know when learning about this function.
Brendan Long
A: 

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));
Oto Brglez
A: 

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

Lukman