When i Open My Browser This Error Accourd . "Failed to execute query! ( -1)"
plz help me to solve this issue ( UPDATE name
SET parentID
=[value-1],id
=[value-2] WHERE 1)
<?php
/**
* Handles creating and/or printing a Tree-Like HTML output, complete with
* all necessary CSS styles.
*
* Assumes a MySQL database table structure like so:
* CREATE TABLE `name` (
* `id` int(11) NOT NULL AUTO_INCREMENT,
* `parentID` int(11) DEFAULT NULL,
* PRIMARY KEY (`id`)
* );
*
* Public methods:
* createTree - Returns the HTML tree-view.
* printTree - Prints the HTML tree-view.
*
* Private methods
* fetchTree - Reads the complete tree structure into an array.
* buildHtml - Builds the HTML div hierarchy based.
*/
class TreeView
{
private $bgColor = "rgba(0, 100, 0, 0.10)";
private $dbLink;
private $tblName;
/**
* Default constructor
* @param mysqli $dbLink A open MySQL (mysqli) connection.
* @throws Exception
*/
public function __construct(mysqli $dbLink)
{
if($dbLink != null && $dbLink->connect_errno == 0)
{
$this->dbLink = $dbLink;
// This number is added the the container DIV ID, so that we can
// tell the DIVs a part if there are more than one view created.
if(!isset($GLOBALS['TreeView_DivID'])) {
$GLOBALS['TreeView_DivID'] = 0;
}
}
else
{
throw new Exception("The mysqli object provided is invalid.");
}
}
/**
* Creates a descending tree-like view of the tree-structure in the given
* database table and returns it as a string.
* @param <type> $tblName The name of the database table to use.
* @return <string> The string output.
* @throws Exception
*/
public function createTree($tblName)
{
if(!isset($tblName) || empty($tblName))
{
throw new Exception("Failed to create the tree. Table or database information is invalid");
}
else
{
// Set up variables
$this->tblName = $tblName;
$treeData = array();
$output = "";
// Create the output
$this->fetchTree($treeData);
// Set up the CSS styles, and create the container DIV.
$divID = "TreeView_ContainerDiv_" . $GLOBALS['TreeView_DivID'];
$output = <<<HTML
<style type="text/css">
div#{$divID} { margin: 0; padding: 0; text-align: center; }
div#{$divID} div { margin: 0; padding: 0 10px; float: left; background-color: {$this->bgColor}; }
div#{$divID} p { margin: 0; padding: 0; }
</style>
<div id="{$divID}">
HTML;
// Add the DIV hierachy.
$this->buildHtml($treeData, $output);
// Increment the DIV ID number
$GLOBALS['TreeView_DivID']++;
return $output;
}
}
/**
* Prints a descending tree-like view of the tree-structure in the given
* database table.
* @param <type> $tblName The name of the database table to use.
* @throws Exception
*/
public function printTree($tblName)
{
echo $this->createTree($tblName);
}
/**
* A recursive function that fetches a tree-structure from a database into an array.
* @global <mysqli> $dbLink A open MySQLI connection.
* @param <number> $parentID The ID the current recursion uses as a root.
*/
private function fetchTree(&$parentArray, $parentID=null)
{
global $dbLink;
// Create the query
if($parentID == null) {
$parentID = -1;
}
$sql = "SELECT `id` FROM `{$this->tblName}` WHERE `parentID`= ". intval($parentID);
// Execute the query and go through the results.
$result = $dbLink->query($sql);
if($result)
{
while($row = $result->fetch_assoc())
{
// Create a child array for the current ID
$currentID = $row['id'];
$parentArray[$currentID] = array();
// Print all children of the current ID
$this->fetchTree($parentArray[$currentID], $currentID);
}
$result->close();
}
else {
die("Failed to execute query! ( $parentID)");
}
}
/**
* Builds a HTML <div> hierarchy from the tree-view data.
* Each parent is encased in a <div> with all their child nodes, and each
* of the children are also encased in a <div> with their children.
* @param <array> $data The tree-view data from the fetchTree method.
* @param <string> $output The <div> hierachy.
*/
private function buildHtml($data, &$output)
{
// Add the DIV hierarchy.
foreach($data as $_id => $_children)
{
$output .= "<div><p>{$_id}</p>";
$this->buildHtml($_children, $output);
$output .= "</div>";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Tree-view Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
</head>
<body>
<?php
// Turn on error reporting, just in case.
ini_set('display_errors', true);
error_reporting(E_ALL);
// Fetch the TreeView class from the other file.
include("class.TreeView.php");
// Open a database connection
// TODO: Replace the info here with your real info.
$dbLink = new mysqli("localhost", "root", "", "tree");
// Create an instance of the TreeView class.
$treeView = new TreeView($dbLink);
// Print the tree view
// TODO: Insert your real table name here.
$treeView->printTree('tblName');
$dbLink->close();
?>
</body>
</html>