TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-04-2009, 03:00 AM   #1 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default 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']); 
benton is offline  
Reply With Quote
Old 10-04-2009, 09:28 AM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Why don't you try it?
__________________
Tanax is offline  
Reply With Quote
Old 10-04-2009, 04:34 PM   #3 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

Quote:
Originally Posted by benton View Post
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.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 10-05-2009, 02:33 AM   #4 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
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?
benton is offline  
Reply With Quote
Old 10-05-2009, 11:13 AM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by adamdecaf View Post
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 View Post
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.
__________________
Tanax is offline  
Reply With Quote
Old 10-05-2009, 11:37 AM   #6 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
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.
benton is offline  
Reply With Quote
Old 10-05-2009, 11:46 AM   #7 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Hm. Exactly why do you insist on having it on 2 rows instead of 3?
__________________
Tanax is offline  
Reply With Quote
Old 10-05-2009, 03:11 PM   #8 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

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'); 
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 10-05-2009, 11:38 PM   #9 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

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.
benton is offline  
Reply With Quote
Old 10-05-2009, 11:51 PM   #10 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

It's possible, but doing so could/can/will cause errors/troubles down the line.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 10-07-2009, 12:09 AM   #11 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Please show how to do it. What sort of problems?
benton is offline  
Reply With Quote
Old 10-07-2009, 12:20 AM   #12 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

Quote:
Originally Posted by benton View Post
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.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 10-07-2009, 03:05 PM   #13 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

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

Village Idiot is offline  
Reply With Quote
Old 10-09-2009, 03:07 AM   #14 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

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?
benton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial on how to write Web Site Scrapers sunilbhatia79 Advanced PHP Programming 1 11-12-2012 07:42 AM
Forgetting to write something down Enfernikus The Lounge 5 06-16-2009 05:13 PM
How do you write your code? Runar The Lounge 24 06-15-2009 08:33 AM
QUIZ: Write a script outputting a multiplication table. No echo, functions etc. used FractalizeR Tips & Tricks 0 12-18-2008 09:42 AM
can someone write an article regarding passing variables from one page to another sarmenhb General 3 05-18-2008 12:49 AM


All times are GMT. The time now is 03:35 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design