Jump to content

Query Empty


Drezard

Recommended Posts

Hello, Here is my code for my classes document:

[CODE]
<?php
error_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 47

Notice: Undefined variable: query in /home/wintersw/public_html/classes/mysql.php on line 47
Error in query: . Query was empty
[/QUOTE]

And hes the code for my script that runs the class:
[CODE]
<?php

include('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
Share on other sites

you might try creating a function called set_page: so your code would look like this:

[code]<?php
error_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]<?php

include('classes/mysql.php');

$myclass = new mysql;

$myclass->connect();

$myclass->set_page("about");

$myclass->view_page();

?>[/code]
Link to comment
Share on other sites

[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? so

SELECT * FROM pages WHERE page_name='$page'
Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.