How to use query result as global?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
infomamun
Forum Contributor
Posts: 102
Joined: Mon Dec 28, 2009 7:48 pm

How to use query result as global?

Post by infomamun »

Hi there,
I have a critical condition. I want to check a mysql field value whether it is empty or not. If empty then to show 'No Value' else show the value. So I want to execute following query:

Code: Select all

<?php
function checkv($field){
global $row;
  if ($row[$field]=""){
    echo 'No value;
  }else{
   echo $row[$field];
  }
}

$result = mysql_query("SELECT * FROM $table WHERE some_column=some_condition");
while($row=mysql_fetch_assoc($result){
?>

Fullname: <?php checkv(fullname); ?>
Lastname: <?php checkv(lastname); ?>
<?php
}
?>
But nothing is outputting except html part Fullname: and Lastname. No mysql error is coming if I add 'or die(mysql_error())' at the end of mysql_query.

How I can accomplish the above condition and display mysql field value?

Best Regards
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to use query result as global?

Post by Celauran »

Don't use globals. Seriously. Or SELECT *. Or mysql_ functions.

That said, your function is failing because you're using an assignment operator (=) instead of a comparison operator (==)
infomamun
Forum Contributor
Posts: 102
Joined: Mon Dec 28, 2009 7:48 pm

Re: How to use query result as global?

Post by infomamun »

Hi Celauran,
Thanks for your reply and sorry for my silly mistake I made. Now problem is solved. It is showing what I want but with using global.
How can I bring expected result (if mysql field value is empty then show 'No Value') without using global in this case?
Expecting your help.
Best Regards
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to use query result as global?

Post by Celauran »

Pass in the row array as an argument to your function.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to use query result as global?

Post by Christopher »

Code: Select all

function checkv($row, $field){
  if ($row[$field]=""){
    echo 'No value';
  }else{
   echo $row[$field];
  }
}
Fullname: <?php checkv($row, 'fullname'); ?>

** OR SIMPLER **

Lastname: <?php echo ($row['lastname'] ? $row['lastname'] : 'No value'); ?>
Though I might do simpler and more flexible:

Code: Select all

function checkv($value, $default='No value'){
  echo ($value ? $value : $default);
}
First name: <?php checkv($row['firstname'], 'No first name'); ?>
Last name: <?php checkv($row['lastname'], 'No last name'); ?>
(#10850)
infomamun
Forum Contributor
Posts: 102
Joined: Mon Dec 28, 2009 7:48 pm

Re: How to use query result as global?

Post by infomamun »

Thanks Christopher,
For your help and sample code.
Best Regards
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to use query result as global?

Post by Christopher »

There is an OO design for this type of error checking that is usually called a Validator or Validation Rule. If you are interested I can show you that code.
(#10850)
infomamun
Forum Contributor
Posts: 102
Joined: Mon Dec 28, 2009 7:48 pm

Re: How to use query result as global?

Post by infomamun »

It would be my pleasure if you show that code
Post Reply