Help with mySQL database and datatables.
Help with mySQL database and datatables.
Hello All. I'm new here so don't attack me! Haha.
Anyway, I need some assistance. I have a page where my sql database table shows up on my pages and I would like to implement datatables into it. I have tried but the farthest i've gotten is that
[quote]DataTables warning (table id = 'the_table'): Requested unknown parameter '0' from the data source for row 0[/quote]
I'm not entirely sure whats wrong, but heres my code for the particular page.
[code]
<?php
session_start();
include("globalfunctions.php");
if(!isset($_SESSION['username']))
{
$password = $_POST["pass"];
$username = $_POST["id"];
include("validatedata.php");
}
?>
Philternet Movie Database
@import "../datatables/media/css/demo_table.css";
$(document).ready(function(){
$('#the_table').dataTable();
});
<?php include("header.php");?>
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = 'password';
$database = 'moviedb';
$table = 'movies';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "Acquired Show Titles";
echo " ";
echo "";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "{$field->name} ";
}
echo "";
echo "";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "";
echo "";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "$cell ";
echo "\n";
}
mysql_free_result($result);
echo "";
include ("footer.php");
?>
[/code]
If anyone could help out, I'd appreciate that!
Anyway, I need some assistance. I have a page where my sql database table shows up on my pages and I would like to implement datatables into it. I have tried but the farthest i've gotten is that
[quote]DataTables warning (table id = 'the_table'): Requested unknown parameter '0' from the data source for row 0[/quote]
I'm not entirely sure whats wrong, but heres my code for the particular page.
[code]
<?php
session_start();
include("globalfunctions.php");
if(!isset($_SESSION['username']))
{
$password = $_POST["pass"];
$username = $_POST["id"];
include("validatedata.php");
}
?>
Philternet Movie Database
@import "../datatables/media/css/demo_table.css";
$(document).ready(function(){
$('#the_table').dataTable();
});
<?php include("header.php");?>
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = 'password';
$database = 'moviedb';
$table = 'movies';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "Acquired Show Titles";
echo " ";
echo "";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "{$field->name} ";
}
echo "";
echo "";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "";
echo "";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "$cell ";
echo "\n";
}
mysql_free_result($result);
echo "";
include ("footer.php");
?>
[/code]
If anyone could help out, I'd appreciate that!
This discussion has been closed.
Replies
[code]
@import "css/demo_table.css";
[/code]
Be careful where you are putting your table tags. You had an open tbody tag in your loop with no closed tag. You had some tr and td tags where you did not need them. Like fbas said you were missing a closed table tag. You need to change your code to look like this
[code]
echo " ";
echo "";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "{$field->name} ";
}
echo "";
echo "";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "$cell ";
echo "\n";
}
mysql_free_result($result);
echo "";
echo "";
[/code]
I think that should work just fine for you. If not let me know.