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
https://forums.phpfreaks.com/topic/247734-db-query-wont-return-data/
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.

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.