+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
Coding Thread, PHP: Extracting data from a SQL generated array in Coding and Web Development; Hi, I'm quite new to PHP and experiencing some issues. I understand programming in general as I have been using ...
  1. #1
    BarryWAaMC's Avatar
    Join Date
    Oct 2009
    Posts
    22
    Thank Post
    4
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Question PHP: Extracting data from a SQL generated array

    Hi,

    I'm quite new to PHP and experiencing some issues.

    I understand programming in general as I have been using ASP for quite some time.

    I am trying to get a record from a database using an sql lookup (sql1). This then returns as an array which is fine, but I need to use part of the array for my next stage.

    My question is, does anyone know how to remove a fields value from the array?

    My code is:
    Code:
    $opt=get_records_sql($sql1);
    	
    	//Diags for SQL content
    		print_object($opt);
    	
    	$n = count($opt);
    	if (empty($opt))
    		{
    		echo 'No options selected';
    		}
    	else
    		{
    		
    		$optno = $opt["subjectid"];
    		
    		// Diags of $optno
    			echo '<br>$optno = '.$optno;
    As you can see, I tried to use this: $opt["subjectid"] as subjectid is the fieldname that I am trying to access and I was under the impression that this was correct for accessing an array, but I get the following error:

    Notice: Undefined index: subjectid

    Any thoughts would be much appreciated.

    Thanks,

    Barry.

  2. #2

    Join Date
    Apr 2006
    Location
    UK
    Posts
    939
    Thank Post
    39
    Thanked 70 Times in 54 Posts
    Rep Power
    30
    Im pretty new to PHP myself, so do you want the values contained in the array or only some of them?

  3. #3
    BarryWAaMC's Avatar
    Join Date
    Oct 2009
    Posts
    22
    Thank Post
    4
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Hi,

    The lookup returned an array automatically.

    I need access to several fields and to cross reference against another database for each record that gets returned.

    For this reason I need the array broken into each field so that I can run the cross reference and then post the result before going on to the next record in the results.

    I know how to do it in ASP, but not in PHP.

    Thanks.

  4. #4

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,159
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    125
    Not quite sure what you're doing, but $opt["subjectid"] is looking for the item in the array called subjectid

    eg you can do:
    $opt["chemistry"]=1;
    or
    $opt["physics"]=0;

    if you've got an array $opt[] which has items corresponding to option subjects and values to go with them (perhaps ticked yes/no)

  5. #5

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,405
    Thank Post
    109
    Thanked 267 Times in 198 Posts
    Rep Power
    169
    Try using this...

    Code:
    echo "<pre>";
    print_r($opt);
    echo "</pre>";
    this will show you the arrays contents in detail...

  6. #6
    BarryWAaMC's Avatar
    Join Date
    Oct 2009
    Posts
    22
    Thank Post
    4
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Question

    The array contains a field subjectid which will contain a number that will be cross referenced against a second database to find out which subject name goes against that number.

    When I run the page, it prints the array contents:
    Code:
    Array
    (
        [1] => stdClass Object
            (
                [uname] => JHollands06
                [tutor] => M LSt
                [subjectid] => 1
                [year] => 2010
                [optid] => 1
            )
    
    )
    this shows me that the subjectid field on this record contains a number 1, but I get an error when I try and lookup that specific field in order to find out which number it contains.

  7. #7

    Join Date
    May 2009
    Location
    Sheffield
    Posts
    275
    Thank Post
    27
    Thanked 40 Times in 30 Posts
    Rep Power
    22
    Quote Originally Posted by BarryWAaMC View Post
    When I run the page, it prints the array contents:
    Code:
    Array
    (
        [1] => stdClass Object
            (
                [uname] => JHollands06
                [tutor] => M LSt
                [subjectid] => 1
                [year] => 2010
                [optid] => 1
            )
    
    )
    It's because the array contains all the records returned by the query, not just "a" record. You need
    $opt[1]["subjectid"].

  8. #8

    Join Date
    Apr 2006
    Location
    UK
    Posts
    939
    Thank Post
    39
    Thanked 70 Times in 54 Posts
    Rep Power
    30
    Edited becuase I was on another planet.
    Last edited by danIT; 26th March 2010 at 07:03 PM.

  9. #9

    Join Date
    May 2009
    Location
    Sheffield
    Posts
    275
    Thank Post
    27
    Thanked 40 Times in 30 Posts
    Rep Power
    22
    Quote Originally Posted by danIT View Post
    You could use:

    extract($opt,EXTR_PREFIX_ALL,'field');

    This will create $field_1, $field_2, $field_3 variables. $field_3 would be your subjectid
    Except that $opt is an array containing one object, which is also an array, so the return will be an array called $field_1.

  10. #10

    Join Date
    Apr 2006
    Location
    UK
    Posts
    939
    Thank Post
    39
    Thanked 70 Times in 54 Posts
    Rep Power
    30
    Oh bugger yes, sorry!

  11. #11
    BarryWAaMC's Avatar
    Join Date
    Oct 2009
    Posts
    22
    Thank Post
    4
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Quote Originally Posted by MattMitchell View Post
    It's because the array contains all the records returned by the query, not just "a" record. You need
    $opt[1]["subjectid"].
    Thanks guys for your assistance.

    I did as was suggested above and got teh following error:

    Fatal error: Cannot use object of type stdClass as array

    Any thoughts?

    Thanks.

  12. #12

    Join Date
    Apr 2006
    Location
    UK
    Posts
    939
    Thank Post
    39
    Thanked 70 Times in 54 Posts
    Rep Power
    30
    Try this:

    foreach($opt as $row) {
    echo $row['subjectid'];
    }

  13. #13
    BarryWAaMC's Avatar
    Join Date
    Oct 2009
    Posts
    22
    Thank Post
    4
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Quote Originally Posted by danIT View Post
    Try this:

    foreach($opt as $row) {
    echo $row['subjectid'];
    }
    I changed it as you suggested, but got exactly the same error.

    Could it have something to do with the fact that the stdClass Object is returned within the array? I don't know, just guessing!

    Cheers for your help and ideas.

  14. #14

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,867
    Thank Post
    412
    Thanked 778 Times in 651 Posts
    Rep Power
    182
    Paste the code where you retrieve the data. You're obviously using a library somewhere along the way, and you're getting an object back not just a plain array.

    A PEAR SQL library by any chance?

  15. #15
    BarryWAaMC's Avatar
    Join Date
    Oct 2009
    Posts
    22
    Thank Post
    4
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Well spotted Powdarrmonkey,

    I'm using a library supplied with a module for Moodle VLE.

    Code:
    $opt=get_records_sql($sql1);
    If this is the cause of my problems, then I will take it up with the author of the module.

    Let me know if you want me to print the code of the 'get_records_sql'.

    Thanks.



SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Extracting data automatically from CMIS
    By danIT in forum MIS Systems
    Replies: 39
    Last Post: 26th March 2010, 09:13 PM
  2. Recover data from part of a RAID5 array?
    By OverWorked in forum Hardware
    Replies: 2
    Last Post: 27th October 2008, 05:29 PM
  3. php and sql ?
    By mac_shinobi in forum Web Development
    Replies: 15
    Last Post: 21st October 2008, 10:43 AM
  4. Extracting data from an MIS
    By MikeBostock in forum MIS Systems
    Replies: 76
    Last Post: 27th February 2008, 10:18 PM
  5. Recover data from broken RAID 0 array
    By indie in forum Hardware
    Replies: 23
    Last Post: 8th February 2006, 11:42 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •