Jump to content

Queries not working


weetabix

Recommended Posts

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' ];
} 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.";
}

Link to comment
Share on other sites

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.";
}
?>

Link to comment
Share on other sites

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."'";

Link to comment
Share on other sites

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' ];

}

Link to comment
Share on other sites

//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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.