Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make MySQL make a if...else selection for PHP?

Status
Not open for further replies.

Moscovite

Programmer
Sep 4, 2002
1
RU
I have a MySQL (v. 3.23.41) data base and a PHP (v. 4) file that collects information from this data base.
Say, there is a table named TBL in the data base, in which there are fields FIELD1, FIELD2.
I write the following in my PHP request:

-------beginning------
$query = "select FIELD1, FIELD2
from TBL";

$result = mysql_query ($query)
or die ("Query failed");

while ($row = mysql_fetch_array ($result))

printf (&quot;%s %s<br>\n&quot;, $row[&quot;FIELD1&quot;], $row[&quot;FIELD2&quot;);
-------end------

I would like MySQL to do the following (but I do not manage to write the correct code):

if FIELD1 = 1 to export it to PHP like, lets say, &quot;Peter&quot;;
if FIELD1 = 2 to export it to PHP like &quot;Paul&quot;;
if FIELD1 = 3 to export it to PHP like &quot;John&quot;;
if FIELD1 has any other value to export it to PHP like &quot;Jesus&quot;.

How should I write the correct code?

Thank you in advance for your answer.
 
$x=0;
While ($x < mysql_numrows($result)):

If ($row['FIELD1']== 1){
echo &quot;Peter&quot;;
}elseif ($row['FIELD1'] == 2){
echo &quot;PAUL&quot;;
}elseif...{

$x++;
endwhile;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top