I am trying to dynamically build a table on my page through ajax, using jquery and php. The following is my php code:
<?php
$con = mysql_connect("127.0.0.1","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("tbl_name", $con);
$result = mysql_query("SELECT `Book ISBN` FROM tbl_name");
echo "<table id='booklist'><tr>
<th>Edit</th>
<th class='coursename'>Course Name</th>
<th class='startdate'>Start Date</th>
<th class='booktitle'>Book Title</th>
<th class='author'>Book Author</th>
<th class='isbn'>Book ISBN</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>
<td><input type='checkbox'></input></td>
<td class='coursename'>" . $row[`Course Name`] . "</td>
<td class='startdate'>" . $row[`Start Date`] . "</td>
<td class='booktitle'>" . $row[`Book Title`]. "</td>
<td class='author'>" . $row[`Book ISBN`]. "</td>
<td class='isbn'><input class='ISBN_number' type='text' value='' size='13' maxlength='13'></input></td>
</tr>";
}
echo "</table>";
mysql_close($con);
?>
(Note: the row names have spaces in them (thats how the database came). I used back ticks instead of single quotes because someone online said thats how you can keep the spaces… If I get over this issue I’ll see if that works.)
Next my jquery script:
$(document).ready(function()
{
$("#build_table").click(function()
{
$.get("build_table.php",
function(table)
{ console.log("Entered table function");
$("#input_table").replaceWith("<div id='input_table'>" + table + "</div>");
});
});
});
and the html:
<input id="build_table" style="clear:both" type="button" value="Submit"></input>
When I click on this button, though, I get the following php error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:…pathname…\build_table.php on line 13
What is this error?
The problem lies in your query. Are you sure that your database and table is named tbl_name? Change your statement into this:
And you’ll get a error message from MySQL.