Jump to content

if value is 0 do this w/MySql


Lamez

Recommended Posts

Alright, I have redesigned my paid system.

 

Now if they have a 0 in the paid column, then is shows them as not paid, but if they have a 1 then they have paid.

 

However, I do not know how to write a IF statement with a MySQL statement. Could someone post an example thanks!

Link to comment
https://forums.phpfreaks.com/topic/90463-if-value-is-0-do-this-wmysql/
Share on other sites

right, but how do I do this with a loop, and it changes the message with different usernames?

 

here is my old code: (now the paid value is in one table w/ the usernames)

$qr=mysql_query("select * from users order by first");
$pt=mysql_query("select * from paid order by user");
while ($rs=mysql_fetch_assoc($qr)) {
       $pu=mysql_fetch_assoc($pt);
       $table = 'paid';
       $field = 'user';
       $compared = $pu['user'];
       $result = mysql_query("SELECT $field FROM $table WHERE $field = '$compared'");


?>
  <tr>
    <td><? echo $rs['first'];?></td>
    <td><? echo $rs['last'];?></td>
    <td>
<?
       if(mysql_num_rows($result)==0) {
             //"Not Paid ";
?>			 
<input type="checkbox" name="checkbox[]" value="<? echo $rs['username'] ?>" />
<?php
       } else {
echo '<a href="pay_dele.php?cmd=delete&user='.$rs['username'].'">Not Paid</a> '; 

       }
?>
(<? echo $rs['username']; ?>)
</td>

@Wolphie,it is necessary because how is going to show all users who not paid OR want to show username for unpaid.

Your this query is just selecting paid users.

$sql = "SELECT `paid` FROM `users` WHERE `paid` = 1";

 

I write that code if he want to show some other details for all users or transactions.

I get this error:

Error: Query was empty

 

here is my PHP code:

<table width="63%" border="0">
  <tr>
    <td width="194"><strong>First Name </strong></td>
    <td width="261"><strong>Last Name </strong></td>
    <td width="273"><strong>Paid (username) </strong></td>
  </tr>
<?php 
$paid = "select * from users order by last";
$paid = mysql_query($sql) or die('Error: ' . mysql_error());
while ($rs=mysql_fetch_assoc($paid)) {
?>
  <tr>
    <td><? echo $rs['first'];?></td>
    <td><? echo $rs['last'];?></td>
    <td>
<?php
If($paid == 0 ){
echo "Unpaid";
}
elseif($paid == 1 ){
echo "Paid";
}

echo "(".$rs['username'].")";
} 
?>


</td>
  </tr>
</table>

 

lol, what does that mean?

Change it:

 

<table width="63%" border="0">
  <tr>
    <td width="194"><strong>First Name </strong></td>
    <td width="261"><strong>Last Name </strong></td>
    <td width="273"><strong>Paid (username) </strong></td>
  </tr>
<?php 
$paid = "select * from users order by last";
$q = mysql_query($paid) or die('Error: ' . mysql_error());
while ($rs=mysql_fetch_array($q)) {
?>
  <tr>
    <td><? echo $rs['first'];?></td>
    <td><? echo $rs['last'];?></td>
    <td>
<?php
If($rs['paid'] == 0 ){
echo "Unpaid";
}
elseif($rs['paid'] == 1 ){
echo "Paid";
}

echo "(".$rs['username'].")";
} 
?>


</td>
  </tr>
</table>

That did, but now I gotta change it in the dashboard.

 

How would I write this code for one individual user?

 

here is my old code: (the paid table is now a column in the users table)

$q = mysql_query("Select * from `paid` where user='$session->username'");
if(mysql_num_rows($q)=='0'){
echo "<br><h3>You have not paid yet.</h3>";
}else{ 
echo "[You have paid!]";
}

Hmmm, seems like you guys are going to a LOT more trouble than needed. Just use a simple IF statement in the SQL query.

 

For all users:

SELECT *, IF(paid==1,'Paid','Not Paid') as status
FROM users
ORDER BY last

 

For one user:

SELECT *, IF(paid==1,'Paid','Not Paid') as status
FROM users
WHERE username = '{$session->username}'
ORDER BY last

alright I got the code, I wrote it myself :D

 

<?php
$q = mysql_query("Select * from `users` where username='$session->username'");
$rs=mysql_fetch_array($q);
$paid = $rs['paid'];
if ($paid === ('0')){
echo "<br><br><h3>You have not paid yet.</h3>";
}else{ 
echo "[You have paid!]";
}
?>

 

-Topic Solved!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.