Jump to content

db query wont return data


esoteric

Recommended Posts

Hi guys, im trying to connect to a database and get the value for the user in the row called 'user_credit', if it equals 1 or more then i want to show the ''You have £ ....'' bit in the script.

 

Problem is nothing shows at all, even without the if statement. I have changed the value for me in the database so in user_credit the value is 100, which is more than 1 so it should appear. I have probably done something wrong. Any ideas?

 

	<? 
include '../admin/database/membership_dbc.php';

$r = mysql_query("SELECT * FROM users WHERE user_name='".safe($_SESSION['user_name'])."'") or die ("Cannot find table");
while( $cred = mysql_fetch_array($r) ) {
if ($cred >= '1' ) 
{ 
?>
  <p>You have £<? echo $cred['user_credit']; ?> available on you account, 
    would you like to use it on this order?<br>
    <label for="credit"></label>
    <select name="credit" id="credit">
      <option value="Y" selected>Yes, use credit</option>
      <option value="N">No, save credit</option>
    </select>
  </p>
  <?
}
}
  ?>

Link to comment
Share on other sites

mysql_fetch_array returns an array of data representing each row selected. So your code:

 

while( $cred = mysql_fetch_array($r) ) {
if ($cred >= '1' ) 

 

is not correct. $cred is an array and the if test will never be true. Looking at the rest of your code, you should probably using:

 

if ($cred['user_credit'] >= 1) 

 

The user_credit column really should be numeric so you don't need to use the quotes around the number 1.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.