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 Link to comment https://forums.phpfreaks.com/topic/33278-query-empty/ 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] Link to comment https://forums.phpfreaks.com/topic/33278-query-empty/#findComment-155466 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] Link to comment https://forums.phpfreaks.com/topic/33278-query-empty/#findComment-155467 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] Link to comment https://forums.phpfreaks.com/topic/33278-query-empty/#findComment-155468 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' Link to comment https://forums.phpfreaks.com/topic/33278-query-empty/#findComment-155472 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 Link to comment https://forums.phpfreaks.com/topic/33278-query-empty/#findComment-155622 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.