TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   How to write this (http://www.talkphp.com/general/4992-how-write.html)

benton 10-04-2009 03:00 AM

How to write this
 
Given something like
PHP Code:

$check mysql_query("select a from table");
$item mysql_fetch_array($check);
echo 
$item['a']; 

Is it poosible to combine the last two statements? Something like
PHP Code:

echo (mysql_fetch_array($check)['a']); 


Tanax 10-04-2009 09:28 AM

Why don't you try it? ;-)

adamdecaf 10-04-2009 04:34 PM

Quote:

Originally Posted by benton (Post 28658)
Given something like

PHP Code:

$check mysql_query("select a from table");
$item mysql_fetch_array($check);
echo 
$item['a']; 

Is it poosible to combine the last two statements? Something like

PHP Code:

echo (mysql_fetch_array($check)['a']); 


That condenced line will throw an error.

PHP Code:

$check mysql_fetch_array(mysql_query("select a from table"));;
echo 
$check['a']; 

That will still throw an error. It does for me at least.

benton 10-05-2009 02:33 AM

Quote:

Originally Posted by Tanax (Post 28659)
Why don't you try it? ;-)

I did - it doesn't work. Thus the question. Perhaps if you tried it you would see that too?

Tanax 10-05-2009 11:13 AM

Quote:

Originally Posted by adamdecaf (Post 28668)
That condenced line will throw an error.

PHP Code:

$check mysql_fetch_array(mysql_query("select a from table"));;
echo 
$check['a']; 

That will still throw an error. It does for me at least.

You have 2 ;; after each other on the first row.

Quote:

Originally Posted by benton (Post 28681)
I did - it doesn't work. Thus the question. Perhaps if you tried it you would see that too?

Your question was if it's possible to do so. If you tried to do so and it didn't work, it's logical to assume that it's not possible.

The reason I told you to try it was because I've seen people(including myself before) ask things they can easially try out on their own(trial and error is the best way of learning). If you've tried it, that's great.

For the record, I've tried that too a long time ago(or something similar) and it doesn't work.

benton 10-05-2009 11:37 AM

Quote:

Originally Posted by Tanax (Post 28683)
You have 2 ;; after each other on the first row.

While not correct, extra semi-colons don't cause problems. The php parser just ignores them.
Quote:

Your question was if it's possible to do so. If you tried to do so and it didn't work, it's logical to assume that it's not possible.
For that statement to be true, you would have to assume the syntax I used is correct, which I don't know if it is. My guess is that there needs to be some grouping or additional code, like
PHP Code:

((mysql_fetch_array($check))['a']); 

or
PHP Code:

$(mysql_fetch_array($check)['a']); 

Those don't work either, by the way.

Tanax 10-05-2009 11:46 AM

Hm. Exactly why do you insist on having it on 2 rows instead of 3?

ETbyrne 10-05-2009 03:11 PM

This is where a good database abstraction layer (or class) comes in handy. If you really want to simplify it down to one line create a MySQL class or use someone else's. You could even just create a single function to do this:

PHP Code:

function first_result_col($sql,$col)
{
    
$check mysql_query($sql);
    
$item mysql_fetch_array($check);
    return 
$item[$col];
}

echo 
first_result_col('SELECT * FROM `posts`','a'); 


benton 10-05-2009 11:38 PM

I was setting up up some test code and got tired of typing in two lines. I wouldn't use in the live code I work on. It seemed an obvious thing to combine the two but when I tried it, it didn't work. I've found in the past that learning how to do things like this helps me understand other problems I may encounter later on so I wanted to learn how to do it to further my understanding of php. But since no one knows how to do it, I'm beggining to think it isn't possible.

adamdecaf 10-05-2009 11:51 PM

It's possible, but doing so could/can/will cause errors/troubles down the line.

benton 10-07-2009 12:09 AM

Please show how to do it. What sort of problems?

adamdecaf 10-07-2009 12:20 AM

Quote:

Originally Posted by benton (Post 28738)
Please show how to do it. What sort of problems?

Multiple problems.
  1. You may get a PHP error for syntax.
  2. Shorter code is not always better code.
  3. A lack of complete understanding will be evident and more prominent with shortened bits of code.
  4. Most developers will like to have that extra line or two.

I would suggest something similar to what ETbyrne wrote above, it solves the solution and provides it in a function for reusable purposes.

Village Idiot 10-07-2009 03:05 PM

You should really be playing around with this and figuring these things out yourself. You will learn a lot more that way.

benton 10-09-2009 03:07 AM

I have. I tried every variation I can think of. The solution may be something obvious but it is beyond me at this point, which is why I posted here. Am I missing the point of this forum?


All times are GMT. The time now is 01:38 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0