Drezard Posted January 8, 2007 Share Posted January 8, 2007 Hello, Here is my code for my classes document:[CODE]<?phperror_reporting(E_ALL);/* * Mysql.php * * This class is meant to do most of the interaction with the MySQL database. * * Last updated: Jan 8, 2007. */ /* * This class contains these functions: * * Connect(); * View_page($page); * * * */class mysql { var $dbuser = "0"; var $dbpass = 0; var $dbhost = "0"; var $db = "0"; var $page = NULL; var $connection = NULL; function Connect() { $this->connection = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass) or die ("Unable to connect!"); mysql_select_db($this->db) or die ("Unable to select database!"); } function view_page() { $page = $this->page; $sql = "SELECT '$page' FROM pages"; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_row($result)) { echo $row[0]; } } else { echo "Page can not be found"; } } }?>[/CODE]Now, when i run this code it outputs:[QUOTE]Notice: Undefined variable: query in /home/wintersw/public_html/classes/mysql.php on line 47Notice: Undefined variable: query in /home/wintersw/public_html/classes/mysql.php on line 47Error in query: . Query was empty[/QUOTE]And hes the code for my script that runs the class:[CODE]<?phpinclude('classes/mysql.php');$myclass = new mysql;$myclass->connect();$myclass->page = "about";$myclass->view_page(); ?>[/CODE]Now what have I done wrong?- Cheers, Daniel Quote Link to comment Share on other sites More sharing options...
trq Posted January 8, 2007 Share Posted January 8, 2007 [code=php:0]$result = mysql_query($sql) or die ("Error in query: $sql. ".mysql_error());[/code] Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted January 8, 2007 Share Posted January 8, 2007 you might try creating a function called set_page: so your code would look like this:[code]<?phperror_reporting(E_ALL);/* * Mysql.php * * This class is meant to do most of the interaction with the MySQL database. * * Last updated: Jan 8, 2007. */ /* * This class contains these functions: * * Connect(); * View_page($page); * * * */class mysql { var $dbuser = "0"; var $dbpass = 0; var $dbhost = "0"; var $db = "0"; var $page = NULL; var $connection = NULL; function Connect() { $this->connection = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass) or die ("Unable to connect!"); mysql_select_db($this->db) or die ("Unable to select database!"); } function set_page($thePage) { $this->page = $thePage; } function view_page() { $page = $this->page; $sql = "SELECT '$page' FROM pages"; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_row($result)) { echo $row[0]; } } else { echo "Page can not be found"; } } }?>[/code]and you would call it on your second page like this:[code]<?phpinclude('classes/mysql.php');$myclass = new mysql;$myclass->connect();$myclass->set_page("about");$myclass->view_page(); ?>[/code] Quote Link to comment Share on other sites More sharing options...
trq Posted January 8, 2007 Share Posted January 8, 2007 Also....[code=php:0]$sql = "SELECT $page FROM pages";[/code] Quote Link to comment Share on other sites More sharing options...
DarkendSoul Posted January 8, 2007 Share Posted January 8, 2007 [quote author=thorpe link=topic=121445.msg499424#msg499424 date=1168223057]Also....[code=php:0]$sql = "SELECT $page FROM pages";[/code][/quote]Not only the missing quotes... im guessing you have a field in your database for the page name, then another for the content? soSELECT * FROM pages WHERE page_name='$page' Quote Link to comment Share on other sites More sharing options...
cshireman Posted January 8, 2007 Share Posted January 8, 2007 The question has been answered above, but I notice it hasn't been spelled out. You run the query using the $query variable, but you actually stored your query in the $sql variable. To correct the problem, use the previously suggested code:[quote author=thorpe link=topic=121445.msg499422#msg499422 date=1168222986][code=php:0]$result = mysql_query($sql) or die ("Error in query: $sql. ".mysql_error());[/code][/quote]Chris Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.