|
-
Dec 17th, 2008, 01:25 PM
#1
[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
-
Dec 21st, 2008, 12:21 PM
#2
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!
-
Dec 21st, 2008, 01:20 PM
#3
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|