rgrady3 Posted March 7, 2007 Share Posted March 7, 2007 I have tried several different codes to create a database search of a table that includes column name "Product," "Price" and "InStock." I am trying to search the "Product column and in each code when I enter a term I know to be in the table it comes up with an error (usually "Unknown Column 'input' in 'where' or mysql_fetch_array(): supplied argument is not a valid MySQL result resource in"). The only way the search works is if I enter the actual name of the Column (in this case "Product") and it displays the entire table. Please help.... Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/ Share on other sites More sharing options...
redarrow Posted March 7, 2007 Share Posted March 7, 2007 show your current code ok? Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-201472 Share on other sites More sharing options...
Barand Posted March 7, 2007 Share Posted March 7, 2007 Should look sometthing like $input = 'something'; $sql = "SELECT Product, Price, InStock FROM product WHERE Product = '$input' "; Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-201496 Share on other sites More sharing options...
rgrady3 Posted March 7, 2007 Author Share Posted March 7, 2007 The old code with the original problem looked like this: $Link = mysql_connect($Host, $User, $Password); $Query="SELECT * from $TableName WHERE Product = $productid"; $Result= mysql_db_query ($DBName, $Query, $Link); Here is the new code per the suggestion: $Link = mysql_connect($Host, $User, $Password); $Query="SELECT Product, Price, InStock from $TableName WHERE Product = '$productid'"; $Result= mysql_db_query ($DBName, $Query, $Link); Now I get a blank page when I search for anything but "Product." When I search "Product" I get the following output: Product:Product, Price: Price In Stock: InSto Print Code is as follows: while ($Row = mysql_fetch_array ($Result)) { print (" Product:$Row[Product], Price: $Row[Price] In Stock: $Row[inStock]<BR> "); } Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-201756 Share on other sites More sharing options...
rgrady3 Posted March 7, 2007 Author Share Posted March 7, 2007 CORRECTION: I now get no output or error except the echoed input from the search form. Here is the entire code: <?php echo "$productid"; $Host="localhost"; $User="xxxx"; $Password="xxxx"; $DBName="xxxx"; $TableName="xxxxxx"; $Link = mysql_connect($Host, $User, $Password); $Query= "SELECT Product, Price, InStock from $TableName WHERE Product = '$productid'"; $Result= mysql_db_query ($DBName, $Query, $Link); while ($Row = mysql_fetch_array ($Result)) { print (" Product: $Row[Product], Price: $Row[Price] In Stock: $Row[inStock]<BR> "); } mysql_close ($Link); Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-202024 Share on other sites More sharing options...
Barand Posted March 8, 2007 Share Posted March 8, 2007 try $Result= mysql_db_query ($DBName, $Query, $Link) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-202126 Share on other sites More sharing options...
rgrady3 Posted March 8, 2007 Author Share Posted March 8, 2007 I still get the same output. Only my echo of the search value appears. Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-202623 Share on other sites More sharing options...
Barand Posted March 8, 2007 Share Posted March 8, 2007 Does this work? String values as array indexes should be quoted. while ($Row = mysql_fetch_array ($Result)) { printf (" Product: %s, Price: %8.2f In Stock: %s<BR> ", $Row['Product'], $Row['Price'], $Row['InStock']); } Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-202849 Share on other sites More sharing options...
rgrady3 Posted March 8, 2007 Author Share Posted March 8, 2007 Still get the same result. To make sure things were working I changed "Product" in my $Query line to include a typo and got back an error "Unknown column 'Produc' in 'where clause' so at least that part is working. Should I attempt a different print method? Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-202911 Share on other sites More sharing options...
Barand Posted March 8, 2007 Share Posted March 8, 2007 Are you sure the $productid you are inputting exists? try the query without the where clause to see if all data is listed Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-202918 Share on other sites More sharing options...
rgrady3 Posted March 8, 2007 Author Share Posted March 8, 2007 I tried it without the where clause and it printed the entire table. What I do not understand is I echo $productid as the first command and it displays every time including this time when I inputted the search value but did not have the WHERE clause in my SELECT line. Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-202926 Share on other sites More sharing options...
rgrady3 Posted March 8, 2007 Author Share Posted March 8, 2007 I discovered something wrong with the table. When I printed it out as described the value of each line is 0.00, but in the table it contains pricing data. Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-202928 Share on other sites More sharing options...
rgrady3 Posted March 8, 2007 Author Share Posted March 8, 2007 The changes you gave me solved part of the problem!!! I tried searching for some other things and it gave data. Now my only problem is the one column giving only 0.00 instead of the actual column value. Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-202989 Share on other sites More sharing options...
Barand Posted March 8, 2007 Share Posted March 8, 2007 How is the Price column defined in the table? Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-203023 Share on other sites More sharing options...
rgrady3 Posted March 8, 2007 Author Share Posted March 8, 2007 It is defined as a VARCHAR(10). Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-203026 Share on other sites More sharing options...
Barand Posted March 8, 2007 Share Posted March 8, 2007 DECIMAL (8,2) would be a better choice for a currency field, however, change print line to printf (" Product: %s, Price: %s In Stock: %s<BR> ", $Row['Product'], $Row['Price'], $Row['InStock']); and see if that helps Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-203029 Share on other sites More sharing options...
rgrady3 Posted March 9, 2007 Author Share Posted March 9, 2007 Outstanding. Thanks for your help! Link to comment https://forums.phpfreaks.com/topic/41576-search-only-works-with-table-column-name/#findComment-203482 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.