nephish wrote:will this work if the value of $amount really is the number 0 and not a false value ?
i need to print 0.00 if it is 0.00, but if it is a no value (SQL query = no rows) then i need to print
No Value on Record.
You shouldn't be trying to use any variables from a query where there were no rows returned. This tells me that you probably are not checking for MySQL errors. Below are examples of how to retrieve data from MySQL with error checking in place (you can also use "or die()" to terminate if there's an error on a MySQL command).
An example for retrieving zero or more rows:
// Connect to MySQL server first – You can use variables instead of these literals
$db = mysql_connect('localhost', 'username', 'password');
if (!$db) {
echo 'Could not connect to MySQL server. <br />Error # ', mysql_errno(), ' Error msg: ', mysql_error();
exit;
}
// Select the database you want to use – You can use a variable here too instead
if (!mysql_select_db('DBname', $db)) { // Did selection fail?
// Handle error
echo 'DB Selection failed. <br />Error # ', mysql_errno(), ' Error msg: ', mysql_error();
exit;
}
$sql = "SELECT name, email, amount FROM people_table";
$result = mysql_query($sql, $db);
if (!$result) {
// Handle error
echo 'Query failed. SQL: ', $sql, '<br />Error # ', mysql_errno(), ' Error msg: ', mysql_error();
exit;
}
$cnt = (int) 0;
// The while loop stops when there's no data left; it might not even go in loop
// and echo anything when there's no data returned on the first call to
// mysql_fetch_assoc()
while($row = mysql_fetch_assoc($result)) { // Retrieve data until no more
echo 'Name: ', $row['name'], '<br />';
echo 'E-mail: ', $row['email'], '<br />';
printf('Amount is %01.2f <br>', $row['amount']);
$cnt++;
}
if (0 == $cnt)
echo 'No data found <br />';
An example of retrieving zero or one row (the first row with that name):
// Make MySQL connection and select DB if haven't already
$name = mysql_escape_string('John Doe'); // or use addslashes()
$sql = "SELECT amount FROM people_table WHERE name = '$name'";
$result = mysql_query($sql, $db);
if (!$result) {
// Handle error
echo 'Query failed. SQL: ', $sql, '<br />Error # ', mysql_errno(), ' Error msg: ', mysql_error();
exit;
}
$row = mysql_fetch_assoc($result); // Try to retrieve the data
if (!$row) // No data returned?
// Not necessarily an error – Just no data matched search criteria
echo 'No data found <br />';
else // There is valid data returned
printf('Amount is %01.2f <br>', $row['amount']);