Solved

PHP MySQL Assign variable new value if not found in table

Posted on 2008-10-30
5
644 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:fastfind1
  • 3
5 Comments
 
LVL 9

Accepted Solution

by:
MarkusId earned 300 total points
ID: 22838881
Why don't you include the $v in the WHERE-clause of the sql-statement?


$v = 12;

 

$sql = sprintf("SELECT reasons FROM codes WHERE reason=$v" );

$q = mysql_query($sql);

$x = 0

while($r = mysql_fetch_array($q)) 

{ 

  $x = 1; // If you come here there's a reason the equals $v

}

if ($x == 1) {  $v = 12; }

else {$v = 'default';}

Open in new window

0
 
LVL 6

Assisted Solution

by:cr4ck3rj4ck
cr4ck3rj4ck earned 100 total points
ID: 22839012
Hope this helps.
$sql = sprintf("SELECT reasons FROM codes WHERE reasons = 12");

$q = mysql_query($sql);

$rsSize = mysql_num_rows($q);

!$rsSize ? $v = 'default' : $v = 12;

Open in new window

0
 
LVL 19

Assisted Solution

by:NerdsOfTech
NerdsOfTech earned 100 total points
ID: 22839200
I win on code speed
>:)
$v = 12; 

$sql = 'SELECT reasons FROM codes';

$q = mysql_query($sql);

$v = eval($q.count) ? $v : 'default';

Open in new window

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 22839222
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

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 22839226
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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
unset shopping cart session 15 32
datetime in sql 6 31
PHP get array item with custom id 4 24
hosting images 4 6
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now