![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
output from database
Need some help with outputting from database.
The code im using is; query = "SELECT * FROM product WHERE productID = '$ID' OR prodName = '$prodName' OR prodDesc = '$prodDesc' OR supplier = '$supplier' OR cost = '$cost'"; The output is ; <?
$i=0;
while ($i < $num) {
$prodID=mysql_result($result,$i,"productID");
$prodName=mysql_result($result,$i,"prodName");
$prodDesc=mysql_result($result,$i,"prodDesc");
$supplier=mysql_result($result,$i,"supplier");
$cost=mysql_result($result,$i,"cost");
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$prodID"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $prodName; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $prodDesc; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $supplier; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $cost; ?></font></td>
</tr>
<?
$i++;
}However, the output is not the data being searched. Everything from the database seems to output from after the data being searched. Hope this makes sense. Any help Thanks in advance |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
output from database
Need some help with outputting from database.
The code im using is; query = "SELECT * FROM product WHERE productID = '$ID' OR prodName = '$prodName' OR prodDesc = '$prodDesc' OR supplier = '$supplier' OR cost = '$cost'"; The output is ; <?
$i=0;
while ($i < $num) {
$prodID=mysql_result($result,$i,"productID");
$prodName=mysql_result($result,$i,"prodName");
$prodDesc=mysql_result($result,$i,"prodDesc");
$supplier=mysql_result($result,$i,"supplier");
$cost=mysql_result($result,$i,"cost");
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$prodID"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $prodName; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $prodDesc; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $supplier; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $cost; ?></font></td>
</tr>
<?
$i++;
}However, the output is not the data being searched. Everything from the database seems to output from after the data being searched. I tried $trimmedProdName = trim($prodName); because i thought maybe it was the entries with empty data fields...but it dosn't help. Hope this makes sense. Any help Thanks in advance |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You're really a little short of information. Without knowing what your database contents are like and exactly what you are expecting, one can't even tell if your query should be ORs or ANDs. There's no indication of where $num comes from. Stuff like that. Personally, I'd probably fetch an entire row into an array and build the table with that, but I guess that's just me. If you aren't sure what's coming back as the result, you might sprinkle a few echoes or print_rs around. You don't show the actual query, so we can't tell if you're prepared to deal with failure or if you're prepared to display any error messages or warnings. You might try running the query by hand directly with MySQL and seeing what it spits back at you.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
How are you getting results when you don't have any connections to the database or specific table defined? Besides that, I don't even see where you are executing your query.
Look into mysql_connect, mysql_select_db, and mysql_query.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Indeed. I presumed those were missing from the presentation, not the code. Lol.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Aug 2005
Location: Leeds - UK
Posts: 69
Rep Power: 4
![]() |
Ok, sorry, i assumed that it was obvious, my fault.
Rest of code looks something like this; session_start();
header("Cache-control: private"); //IE 6 Fix
error_reporting(E_ALL);
if(isset($_SESSION['access']))
{
include '../connect.php';
include ('../template.php');$ID=$_POST['prodID']; $prodName=$_POST['prodName']; $prodDesc=$_POST['prodDesc']; $supplier=$_POST['supplier']; $cost=$_POST['cost']; $trimmedCost = trim($cost); $trimmedProdName = trim($prodName); ?>
<?include ('inventoryMenu.php');?>
<P>
you want to search; <br>
<?echo "id: $ID"; ?>
<br>
<?echo "name: $prodName"; ?>
<br>
<?echo "description: $prodDesc"; ?>
<br>
<?echo "supplier: $supplier"; ?>
<br>
<?echo "cost: $cost"; ?>
<br><? $query = "SELECT * FROM product WHERE productID = '$ID' OR prodName = '$trimmedProdName' OR prodDesc = '$prodDesc' OR supplier = '$supplier' OR cost = '$trimmedCost'"; $result = mysql_query($query); $num = mysql_numrows($result); ?> <p> Your search has resulted in the following;
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Product ID</font></th>
<th><font face="Arial, Helvetica, sans-serif">Product Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Product Description</font></th>
<th><font face="Arial, Helvetica, sans-serif">Supplier</font></th>
<th><font face="Arial, Helvetica, sans-serif">Cost</font></th>
</tr>
<?
$i=0;
while ($i < $num) {
$prodID=mysql_result($result,$i,"productID");
$prodName=mysql_result($result,$i,"prodName");
$prodDesc=mysql_result($result,$i,"prodDesc");
$supplier=mysql_result($result,$i,"supplier");
$cost=mysql_result($result,$i,"cost");
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$prodID"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $prodName; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $prodDesc; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $supplier; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $cost; ?></font></td>
</tr>
<?
$i++;
}
<?
}
else
{
$URL="http://www.lingan.co.uk";
header ("Location: $URL");
}
?>Thats most of the code from my page, everything else is basic html formatting. A bit long winded but hopefully someone can help. I recall the use of % to trim characters being searched from a database and have a feeling it may be something like that which is missing, but obviously i am not sure. Thanks in advance once again |
|
|
|
|
|
#7 |
|
Programming Guru
![]() ![]() ![]() |
If you are still getting all of the entries in the table, try echoing your sql string and running it manually in the mysql console to see what the results are. I would suggest generating the sql string dynamically from what the user requests and doing away with the empty fields being part of your query.
Just some basic db code to test your sql cmd. I haven't ran it as I do not have your db/table structure, but this is how I pull data from mysql. [PHP] // open connection to database $connection = mysql_connect(HOST,USERID,PWD) or die ("Unable to connect!"); mysql_select_db("DBNAME") or die ("Unable to select database!"); // formulate and execute query $query = "SELECT * FROM product WHERE productID = '$ID' OR prodName = '$prodName' OR prodDesc = '$prodDesc' OR supplier = '$supplier' OR cost = '$cost'"; $result = mysql_query($query); $num_results = mysql_num_rows($result); echo "Results: $num_results"; for ($i=0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); echo $row["ProductID"]; echo "<br>" echo $row["prodName"]; echo "<br>" echo $row["prodDesc"]; echo "<br>" echo $row["supplier"]; echo "<br>" echo $row["cost"]; echo "<p>" } // close connection mysql_close($connection); [/PHP]
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
My technique is very similar to IR's. I often use a custom error handler in more critical applications, so I know what's going on. Example:
$queryProducts = "SELECT ID, Item, Description, Color, Size, Price, OnHand, OfferQty FROM products WHERE (OnHand >= OfferQty AND OnHand > 0 AND OfferQty <= 1000) OR (OfferQty = 0) ORDER BY Item"; $catalog = mysql_query ($queryProducts) or dbError (mysql_error(), "product query", $queryProducts, true); while ($rowData = mysql_fetch_array ($catalog))
{
$items ['ID'] = $rowData ['ID']);
$items ['Item'] = $rowData ['Item']);
$items ['Description'] = $rowData ['Description']);
etc.
etc.
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|