weetabix Posted December 4, 2007 Share Posted December 4, 2007 Emm ok, this time I've made a query, tried to display its results but it wont display anything... here's my code $itemNumber = $_POST['itemNum']; //Apply the query $query = "SELECT book_id, book_title, book_isbn, qty_in_stock, publisher_no FROM Book WHERE book_id = $itemNumber"; $result = mysql_query($query); //Loop through results while( $row = mysql_query( $result ) ) { echo "<p>", $row[ 'Book ID' ] . $row[ 'Book Title' ] . $row[ 'Book ISBN' ] . $row[ 'Quantity in Stock' ] . $row[ 'Publisher Number' ]; } Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 4, 2007 Share Posted December 4, 2007 error check that please, and in the future error check all queries before posting Also where is your connection to a database???? <?php $itemNumber = $_POST['itemNum']; //Apply the query $query = "SELECT book_id, book_title, book_isbn, qty_in_stock, publisher_no FROM Book WHERE book_id = $itemNumber"; $result = mysql_query($query) or die(mysql_error()); //Loop through results if(mysql_num_rows($result)>0){ while( $row = mysql_query( $result ) ) { echo "<p>", $row[ 'Book ID' ] . $row[ 'Book Title' ] . $row[ 'Book ISBN' ] . $row[ 'Quantity in Stock' ] . $row[ 'Publisher Number' ]."</p>\n"; } } else{ echo "No results found."; ?> Also you didn't close your paragraph? Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 What do you mean I didn't close my paragraph? And how can I error check? Sorry, I've just joined and I'm new to PHP so I'm trying to improve EDIT: I found the error check. It's connecting fine to my database, just didnt want to post the whole thing I get this error when I load it: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 4, 2007 Share Posted December 4, 2007 if you are in debugging mode (Which you are) always remove all the @ symbols before a function, this supresses any errors that it would yield. For a mysql query specifically write them in the fashion of $query = "Select Some stuff from `a table` where this = that"; $result = mysql_query($query) or die(mysql_error()); if its a select lets make sure we have some valid results first so add somethign like if(mysql_num_rows($result)>0){ //Valid results so we can loop it while($row = mysql_fetch_array($result)){ //Splash out stuff } } else{ //No returned rows //splash out an error } the paragraph closer was in the string, try what I gave you it should splash an error or say no records found. Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 I think I've just messed up my PHP code... Here's the whole thing <form action="stockEnquiry.php" method="post"> <table border="1" width="30%"> <tr> <td colspan="2"> <p align="center">Item Stock Lookup</td> </tr> <tr> <td width="41%">Item Number:</td> <td width="53%"><input type = "Text" name="itemNum"></td> </tr> </table> <input type= "Submit" name="submit" value= "Submit"> //Database connection //Enter the information needed to connect to the database $dbhost="myhost"; $dbuser="username"; $dbpassword="pass"; $database = "dbname"; $connection=mysql_connect($dbhost,$dbuser,$dbpassword); //Attempt to connect if(!$connection) { //Return error message if connection fails die('Could not connect: ' . mysql_error()); } //Declare the used database mysql_select_db($database,$connection); $itemNumber = $_POST['itemNum']; //Apply the query $query = "SELECT book_id, book_title, book_isbn, qty_in_stock, publisher_no FROM Book WHERE book_id = $itemNumber"; $result = mysql_query($query) or die(mysql_error()); //Loop through results if(mysql_num_rows($result)>0){ while( $row = mysql_query( $result ) ) { echo "<p>", $row[ 'Book ID' ] . $row[ 'Book Title' ] . $row[ 'Book ISBN' ] . $row[ 'Quantity in Stock' ] . $row[ 'Publisher Number' ]."</p>\n"; } } else{ echo "No results found."; } Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 4, 2007 Share Posted December 4, 2007 did u run it? Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 yeah I get nothing back Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 4, 2007 Share Posted December 4, 2007 define nothing? if you don't have error reporting on then I can't help you Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 here's what it says Parse error: syntax error, unexpected $end in /u/t/ap278/dos/html/stockEnquiry.php on line 56 Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 4, 2007 Share Posted December 4, 2007 this should work <form action="stockEnquiry.php" method="post"> <table border="1" width="30%"> <tr> <td colspan="2"> <p align="center">Item Stock Lookup</td> </tr> <tr> <td width="41%">Item Number:</td> <td width="53%"><input type = "Text" name="itemNum"></td> </tr> </table> <input type= "Submit" name="submit" value= "Submit"> <?php //Database connection //Enter the information needed to connect to the database $dbhost="myhost"; $dbuser="username"; $dbpassword="pass"; $database = "dbname"; $connection=mysql_connect($dbhost,$dbuser,$dbpassword); //Attempt to connect if(!$connection) { //Return error message if connection fails die('Could not connect: ' . mysql_error()); } //Declare the used database mysql_select_db($database,$connection); $itemNumber = $_POST['itemNum']; if(!empty($itemNumber)){ //Apply the query $query = "SELECT book_id, book_title, book_isbn, qty_in_stock, publisher_no FROM Book WHERE book_id = $itemNumber"; $result = mysql_query($query) or die(mysql_error()); //Loop through results if(mysql_num_rows($result)>0){ while( $row = mysql_query( $result ) ) { echo "<p>", $row[ 'Book ID' ] . $row[ 'Book Title' ] . $row[ 'Book ISBN' ] . $row[ 'Quantity in Stock' ] . $row[ 'Publisher Number' ]."</p>\n"; } } else{ echo "No results found."; } } else{ echo "The Item Number was Blank."; } ?> Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 It does work but no results are returned (it doesnt even return a messaging saying anything about results, it just refreshes the page)... why's that? ??? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 4, 2007 Share Posted December 4, 2007 you must not be showing me the complete code because nothign there will net a header/meta refresh Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 Well I copied the code from my editor. The results are OK when I enter an invalid number, but not when I'm entering the number of an item in the database Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 4, 2007 Share Posted December 4, 2007 change the line $query = "SELECT book_id, book_title, book_isbn, qty_in_stock, publisher_no FROM Book WHERE book_id = $itemNumber"; to these 2 lines $itemNumber = mysql_real_escape_string(strip_tags(trim($itemNumber))); $query = "SELECT book_id, book_title, book_isbn, qty_in_stock, publisher_no FROM `Book` WHERE book_id = '".$itemNumber."'"; Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 Nope, its still the same thing. It works fine when I try the statement in Unix though Quote Link to comment Share on other sites More sharing options...
dg Posted December 4, 2007 Share Posted December 4, 2007 check out this $itemNumber = $_POST['itemNum']; //Apply the query $query = "SELECT book_id, book_title, book_isbn, qty_in_stock, publisher_no FROM Book WHERE book_id = $itemNumber"; $result = mysql_query($query); //Loop through results it should be mysql_fectch_array not mysql_query while( $row = mysql_fectch_array( $result ) ) { echo "<p>", $row[ 'Book ID' ] . $row[ 'Book Title' ] . $row[ 'Book ISBN' ] . $row[ 'Quantity in Stock' ] . $row[ 'Publisher Number' ]; } Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 I was actually just changing my code to adapt to that here's what I get Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /u/t/ap278/dos/html/stockEnquiry.php on line 41 Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 and I just tried the previous methods, none of them seems to return any results... Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 Apparently it works but doesnt list the results.... The selection is doing fine Quote Link to comment Share on other sites More sharing options...
dg Posted December 4, 2007 Share Posted December 4, 2007 //hoping that u have made the connection string correct like below $conn = mysql_connect($server, $user, $pass) ; mysql_select_db($db_name, $conn); $itemNumber = $_POST['itemNum']; //Apply the query $query = "SELECT book_id, book_title, book_isbn, qty_in_stock, publisher_no FROM Book WHERE book_id = ".$itemNumber."; $result = mysql_query($query); //Loop through results while($row = mysql_fetch_array($result)) { echo "<p>".$row["book_id"].$row["book_title"].$row["book_isbn"].$row["qty_in_stock" ].$row[publisher_no]; } //try this now Quote Link to comment Share on other sites More sharing options...
weetabix Posted December 4, 2007 Author Share Posted December 4, 2007 you've just saved my life! Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.