Results 1 to 3 of 3

Thread: [RESOLVED] checking for field existence in data results

  1. #1

    Thread Starter
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Resolved [RESOLVED] checking for field existence in data results

    Using PHP (I think it's PHP5) & MySQL (not sure the exact version, but from sometime in the last 6 months when I downloaded it)

    Ok, I've got some data coming back from a database query, no problem. I know that I can get to the results of each filed using
    $myResults['FieldName'] ... no problem. But, what I'm trying to do is create some generic routines that will return specific fields, formatted for their specific use. these routines will be called after any one of several queries, each of which have about 80% fields in common. Those aren't the ones I'm worried about. It's the 20% that I am. Because I might be releasing this as a plugin for an OS framework, which means other people who may or may not be familiar with PHP will be using it. They will probably know jsut enough to be dangerous and can follow simple instructions of "Put this here, and that there." But following things like "Use this ONLY after query XYZ has been run" is probably asking too much.

    So, with that in mind, I'd like to be able to, in those 20% cases, check to see if a particular field is a part of the current dataset, and if it is, get the field value from it and use it, if not, return a default value - this way the plugin won't make someone's website suddenly stop working.

    right now, the standard function look some thing like this:

    Code:
     function songTitle() {
       return $song["title"];
     }
    what I need is something along these lines:
    Code:
     function songTitle() {
       if("title" is part of $song) {
       return $song["title"]; } else { return ""; }
     }
    Hope this makes sense...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: checking for field existence in data results

    do you need to check if the field exists in the table being queried? or can you just look in the array being returned ($song)? if the latter,

    PHP Code:
    if(array_key_exists("title"$song)){
      return 
    $song['title'];
    }else{
      return 
    false;

    if you're looking to do this at the database level, then you might look into using the mysql_fetch_field() function.

    hope I understood what you meant. if not, and you're still having trouble, try to clarify!

  3. #3

    Thread Starter
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: checking for field existence in data results

    I was looking for it at the array level, not the database, so that's exactly what I needed.

    Thanks!

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

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



Click Here to Expand Forum to Full Width