Computing.Net > Forums > Web Development > Return Message on PHP mysql query

Return Message on PHP mysql query

Reply to Message Icon

Original Message
Name: RTAdams89
Date: May 21, 2008 at 18:12:13 Pacific
Subject: Return Message on PHP mysql query
OS: na
CPU/Ram: na
Model/Manufacturer: na
Comment:

I have a simple form which allows a visitor to enter a invoice number. When they submit the form the invoice number is passed to a PHP script which looks it up in a mysql data base and returns other data from the same row as the entered invoice number.

If a visitor enters an invoice number that isn't in the table, I would like to display a message to that effect. I have tried al kinds of php "if" statements using functions ranging from "isset()" to "empty()" but none work. What is the simple way to accomplish my goal? Here is the PHP code I currently have:

<?php

include "config.php";

mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>

<html>
<head>
<title>Customer Invoice</title>
</head>
<body>
<?php
if (!isset($_POST['submit'])) {
?>
<center>
View Invoice

<form action="" method="post">
Please enter your invoice number and then press "View".<br />
<input type="text" name="invoice" size="5">
<input type="submit" name="submit" value="View">
</form>
<br />
<br />
<br />
Admin Login
</center>
<?php
} else {
$invoice = mysql_real_escape_string($_POST['invoice']);
$result = mysql_query("SELECT * FROM invoices WHERE invoice='" . $invoice . "'");

while($row = mysql_fetch_array($result))
{
echo "<center>Invoice # " . $row['invoice'] . "</center>";
echo "<center>" . $row['name'] . "</center>";
echo "<center>" . $row['date'] . "</center><br /><br />";
echo "Hours: " . $row['hours'] . "<br />";
echo "Rate: $" . $row['rate'] . "/hr<br />";
echo "Labor Charge: $" . ($row['hours'] * $row['rate']) . "<br /><br />";
echo "Hardware: " . $row['h1'] . " -$" . $row['c1'] . "<br />";
echo "Hardware: " . $row['h2'] . " -$" . $row['c2'] . "<br />";
echo "Hardware: " . $row['h3'] . " -$" . $row['c3'] . "<br />";
echo "Parts Charge: $" . ($row['c1'] + $row['c2'] + $row['c3']) . "<br /><br />";
echo "Total Due: $" . $row['total'] . " Paid: $" . $row['paid'] . " Balance Due: $" . ($row['total'] - $row['paid']) . "<br /><br />";
echo "Notes: " . $row['notes'];
echo "<br /><br />END OF INVOICE<br />";
echo "Click here to search again.";

}
}
?>

</body>
</html>

-Ryan Adams
http://RyanTAdams.com


Report Offensive Message For Removal


Response Number 1
Name: RTAdams89
Date: May 21, 2008 at 18:13:49 Pacific
Subject: Return Message on PHP mysql query
Reply: (edit)

PS: you can download the whole PHP script here:

http://invoices.ryantadams.com/inde...

-Ryan Adams
http://RyanTAdams.com


Report Offensive Follow Up For Removal

Response Number 2
Name: Michael J (by mjdamato)
Date: May 22, 2008 at 10:06:21 Pacific
Subject: Return Message on PHP mysql query
Reply: (edit)

Although I would do things a lot differently, the function you are looking for is mysql_num_rows(). Added code in blue
====================================

$result = mysql_query("SELECT * FROM invoices WHERE invoice='" . $invoice . "'");

if (!mysql_num_rows($result)) {
echo "No records found";
} else {

while($row = mysql_fetch_array($result))
{
....


=======================================
Need to add a closing bracket for the else.

Michael J


Report Offensive Follow Up For Removal

Response Number 3
Name: RTAdams89
Date: May 22, 2008 at 12:24:25 Pacific
Subject: Return Message on PHP mysql query
Reply: (edit)

I thought about doing that, but won't that put unnecessary strain on the database? Each time someone checks their invoice, it has to count every row in the table (which could be thousands).

I'm I over estimating the problem?

-Ryan Adams
http://RyanTAdams.com


Report Offensive Follow Up For Removal

Response Number 4
Name: Michael J (by mjdamato)
Date: May 22, 2008 at 19:47:58 Pacific
Subject: Return Message on PHP mysql query
Reply: (edit)

I think you have a misinterpretation of what mysql_num_rows() does. It does NOT count the rows in the database. It tells you haw many records were returned in a query.

In your example above you run a query against the database for records matching a particular invoice number. i would assume that invoice numbers are not duplicated. So the results of that query would include 1 record or 0 records. So, mysql_num_rows() will simply return a 1 or 0 on that result.

That is the exact purpose that command is used for. I'm sure you could do a different workaround using only PHP, but I doubt it would be more efficient.

Michael J


Report Offensive Follow Up For Removal

Response Number 5
Name: RTAdams89
Date: May 23, 2008 at 01:27:33 Pacific
Subject: Return Message on PHP mysql query
Reply: (edit)

Ahh, you are right on. I was thinking of a different command. That should do it. Perfect, thanks.

-Ryan Adams
http://RyanTAdams.com


Report Offensive Follow Up For Removal


Response Number 6
Name: Michael J (by mjdamato)
Date: May 23, 2008 at 09:14:38 Pacific
Subject: Return Message on PHP mysql query
Reply: (edit)

If you were thinking of the COUNT() command that you would use directly in the query, then that is not an intensive process either. Remember the data in a database is indexed so much of the information can be accessed very quickly.

Michael J


Report Offensive Follow Up For Removal

Response Number 7
Name: RTAdams89
Date: May 23, 2008 at 14:21:43 Pacific
Subject: Return Message on PHP mysql query
Reply: (edit)

I'm not sure what I was thinking of, obviously something incorrect :). Anyhow, I've got the page working as I would like now. Thanks for the help!

-Ryan Adams
http://RyanTAdams.com


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Return Message on PHP mysql query

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge