Query problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Query problem

Post by oscardog »

I've been trying to learn/code with a more OOP approach so I tried to use a class for database queries. However it's not working as intended and when having full error messages on I get a warning on mysql_num_rows() which ofcourse means it's not returning a result. It's not the mysql_num_rows method as I tried using a standard query instead of using a method. Below is the relevant code:

registration.php

Code: Select all

require_once("includes/functions_registration.php"); //Required functions (validateEmail())
require_once("includes/functions.php"); //Holds the sanitize_data() function
$email = sanitize_data($_GET['email']); //This just stripslashes/trim/mysql_real_escape_string, works fine
$isValidated = validateEmail($email); //Calls function that is in functions_registration.php
function_registration.php

Code: Select all

function validateEmail($email) {
	require_once("class_database.php");
	$dbHandler = new database();
	$dbHandler->connect_to_database(); //This works fine
	$emailQuery = $dbHandler->select_from_database("email", "farmers", "email = '".$email."'"); //This is the problem line!
        //$emailQuery = mysql_query("SELECT email FROM database WHERE email = '".$email."'"); This works perfectly fine when I comment out the above line
	$emailVal = $dbHandler->count_rows_in_database($emailQuery); //Works fine when I use
	
	echo "HHH" . $emailVal; //Me testing if $emailVal has a value, it doesn't.
	if($emailVal == 1) {
		return $dbHandler->update_database("status='2'", "farmers", "email='".$email."'"); //This too works fine
	} else {
		return 0;
	}
}
class_database.php

Code: Select all

class database {

	public function connect_to_database() {
		$dbConnect = mysql_connect("localhost", "xxxxxx","xxxxxxxxxx") or die(mysql_error());
		if($dbConnect) {
			mysql_select_db("tbl_name", $dbConnect);	
		}
		return $dbConnect;
	}

	public function select_from_database($fields, $tbl, $where, $order, $limit) {
		$selectQuery = "SELECT " . $fields . " FROM " . $tbl;
		if(isset($where)) {
			$selectQuery .= " WHERE " . $where;	
		}
		if(isset($order)) {
			$selectQuery .= " ORDER BY " . $order;	
		}
		if(isset($limit)) {
			$selectQuery .= " LIMIT " . $limit;	
		}
		echo $selectQuery;
		return mysql_query($selectQuery) or die(mysql_error());
	}

	public function count_rows_in_database($countQuery) {
		return mysql_num_rows($countQuery);
	}
}
Just added in a full query(As in I wrote a full query into a variable with no outside variables, just text, and it threw an error) into the select_from_database method and it threw an 'error in the SQL syntax'. Not sure why though.

'Updated' select_from_database() method that throws the error:

Code: Select all

		public function select_from_database($fields, $tbl, $where, $order, $limit) {
		//$selectQuery = "SELECT " . $fields . " FROM " . $tbl;
		$selectQuery = "SELECT email FROM farmers WHERE email = 'ashley@designaweb.co.uk'";
		/* if(isset($where)) {
			$selectQuery .= " WHERE " . $where;	
		}
		if(isset($order)) {
			$selectQuery .= " ORDER BY " . $order;	
		}
		if(isset($limit)) {
			$selectQuery .= " LIMIT " . $limit;	
		}
		echo $selectQuery;  */
		return mysql_query($selectQuery) or die(mysql_error());
	}
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Query problem

Post by oscardog »

Bump, anyone?

I imagine it's quite a simple fix for someone that understands OOP concepts (or it might even be a simple stupid fix).

Help greatly appreciated.
Post Reply