PHP and SQL question...?
-
How do I: A) Place a link to another PHP file on my BookISBN's? The file name is moreInfo.php B) Get the table headers to work? <?php include ( 'dbconnect.php' ); // connect to the database // Make up an SQL query (put into variable) $bookSQL = "SELECT bookISBN, bookTitle, bookYear, catID, bookPrice FROM nbc_book ORDER BY bookTitle"; // execute the SQL query $rsBooks = mysql_query( $bookSQL )or die ( mysql_error() ); // Loop through the record set of data returned by the query and display it on the screen. echo "<table border=\"1\">"; echo"<tr>"; echo "<th><strong>BookISBN</strong></th>"; echo "<th><strong>Title</strong></th>"; echo "<th>Year</th>"; echo "<th>Catalogue ID</th>"; echo "<th>Price</th>"; echo "</tr>"; while ( $row = mysql_fetch_assoc( $rsBooks ) ) { echo "<tr><td>" . $row['bookISBN'] . "</td><td>" . $row['bookTitle'] . "</td><td>" . $row ['bookYear'] . "</td><td>" . $row ['catID'] . "</td><td>" . $row ['bookPrice'] . "</td></tr>"; } echo "</table>"; mysql_free_result ( $rsBooks ); ?> I don't know what I'm doing wrong :/
-
Answer:
try debugging it with these steps: 1) echo $bookSQL & paste this into an SQL tool like MSQL Workbench to see if it is without errors 2) echo $rsBooks (or print_r $rsBooks) to see if it really is filled with content. 3) try using mysql_fetch_row instead of mysql_fetch_assoc Missing the opening & closing of the database, so I can't really say anything about it, but I guess the connection fails.
siobhan l at Yahoo! Answers Visit the source
Other answers
First: i don't see you selecting the db. $db_selected = mysql_select_db('booksDB', $link); if (!$db_selected) { die ('Can\'t use that db! : ' . mysql_error()); } Maybe you did it in your dbconnect.php file. Also, not sure if your mysql_connect is right since we can't see it. Are you sure the connection is even happening? Look at your error logs. What errors are output over http? is the user/pass right? Are you sure your environment is correctly setup. Is PHP correctly working with MySQL on other pages you've written on this server? This worked fine for me: <?php $link = mysql_connect('localhost', 'username', 'password'); if (!$link) { die('Could not connect: ' . mysql_error()); } //the part u might have missed $db_selected = mysql_select_db('books', $link); if (!$db_selected) { die ('Can\'t use foo : ' . mysql_error()); } $bookSQL = "SELECT isbn, title, year, cat, price FROM books ORDER BY title"; $rsBooks = mysql_query( $bookSQL )or die ( mysql_error() ); echo "<table border=\"1\">"; echo"<tr>"; echo "<th><strong>BookISBN</strong></th>"; echo "<th><strong>Title</strong></th>"; echo "<th>Year</th>"; echo "<th>Catalogue ID</th>"; echo "<th>Price</th>"; echo "</tr>"; //spit the results up while ( $row = mysql_fetch_assoc( $rsBooks ) ) { echo "<tr><td>" . $row['isbn'] . "</td><td>" . $row['title'] . "</td><td>" . $row ['year'] . "</td><td>" . $row ['cat'] . "</td><td>" . $row ['price'] . "</td></tr>"; } echo "</table>"; mysql_free_result ( $rsBooks ); ?> learn where your web server logs go, and learn how to read them. like /var/log/apache2/error.log for example.
Related Q & A:
- Are SQL Injection vulnerabilities in a PHP application acceptable if mod_security is enabled?Best solution by Programmers
- How can I optimize this dynamic SQL query in oracle with PL/SQL?Best solution by docs.oracle.com
- How to prevent an SQL injection in PHP?Best solution by Stack Overflow
- How to store data in php and get data from php?Best solution by Stack Overflow
- How to remove your question from yahoo question/answers?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.