I'm new to both SQL and PHP and having a dickens of a time getting any info back out of MySQL using PHP. I want to use a loop to check for a duplicate variable ($ISBN) in the ISBN column of the table books. I can't get any of the loops to print or echo the values in any part of the table let alone work the test code. My query when run from the MySQL command line lists all the data in the column ISBN. What am I doing wrong?
Below is the COD I am using:
Code: Select all
// Storing the posted names from the form of the previous page
$ISBN = $_POST["ISBN"];
$Title = $_POST["Title"];
$Auth = $_POST["Auth"];
$AuthURL = $_POST["AuthURL"];
$Pub = $_POST["Pub"];
$PubURL = $_POST["PubURL"];
$Desc = $_POST["Desc"];
$Personal = $_POST["Personal"];
$link = mysql_connect("localhost","$userID","$pw")
or die ("Unable to connect to MySQL server."); // This works!!
$db = mysql_select_db("$dBase")
or die ("Unable to select requested database."); // This works!!
// The line below works fine from MySQL command line
$query = "SELECT ISBN FROM Books"
$result = mysql_query("$query") // Also working - no error!!
or die ("Unable to query ISBN.");
//Here is wehre the problems begin...
//This sould print out the number of rows repeatedly
while ($array = mysql_fetch_array($result)){
$data = count($array);
print ("<br>the Result: " . $data);
}
// These lines enter the new values into the database table AND WORK!!
$Insert = mysql_query("INSERT INTO books (ISBN, Title, Author,
AuthorURL,Publisher, PublisherURL, Description, Personal)
VALUES ('$ISBN', '$Title', '$Auth', '$AuthURL', '$Pub', '$PubURL',
'$Desc', '$Personal')") or die (mysql_error());
print ("<center><h1>Your book listing has been posted.</h1></center>");
// This line closes the database server connection
mysql_close($link);