Link to home
Start Free TrialLog in
Avatar of fastfind1
fastfind1

asked on

PHP MySQL Assign variable new value if not found in table

I have a PHP variable with a set value ($v = 12;)
I have a MySQL table named codes with a column named reasons.


I need to write a PHP script that looks to see if 12 is a value in ANY record in the codes table under the reasons column.  If 12 is in the table, it is only in once.  

If the script finds that 12 does exist in the table and column, the value of $v remains 12.  

But if the script finds that 12 does NOT exist in the table and column, then $v is reassigned a value of "default"

Please help me edit my code below


$v = 12;
 
$sql = sprintf("SELECT reasons FROM codes");
$q = mysql_query($sql);
while($r = mysql_fetch_array($q)) 
{ 
//HERE IS WHERE I GET STUCK
 
If true, then $v = 12;
If false, then $v = 'default';

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of MarkusId
MarkusId
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
TOP 4 Lines below:
PEREFECTED FOR reasons AS STRING

BOTTOM 4 Lines below:
PEREFECTED FOR reasons AS NUMBER
$v = '12'; 
$sql = "SELECT reasons FROM codes WHERE reasons = '$v'";
$q = mysql_query($sql);
$v = $q.count ? $v : 'default';
 
$v = '12'; 
$sql = "SELECT reasons FROM codes WHERE reasons = $v";
$q = mysql_query($sql);
$v = $q.count ? $v : 'default';

Open in new window

TOP 4 Lines below:
UBER-PEREFECTED FOR reasons AS STRING

BOTTOM 4 Lines below:
UBER-PEREFECTED FOR reasons AS NUMBER
$v = '12'; 
$sql = "SELECT reasons FROM codes WHERE reasons = '$v'";
$q = mysql_query($sql);
$v = $q.count ? $v : 'default';
 
$v = 12; 
$sql = "SELECT reasons FROM codes WHERE reasons = $v";
$q = mysql_query($sql);
$v = $q.count ? $v : 'default';

Open in new window