Problem with query within a function

Discussion in 'PHP' started by oscardog, Aug 24, 2010.

  1. #1
    Hello,

    I am fairly new to OOP programming but it's all working so far except one little thing (which is possibly the most important part).

    I have tested the count_rows_in_database method and that works fine. The update/insert calls also work perfectly, however select is not working.

    Below is the code required. Thanks in advance :)

    I call the function like so:

    
    $isValidated = validateEmail($email);
    
    Code (markup):
    This is the function validateEmail:

    
    function validateEmail($email) {
    	require_once("class_database.php");
    	$dbHandler = new database();
    	$dbHandler->connect_to_database();
    	$emailQuery = $dbHandler->do_query("SELECT email FROM farmers WHERE email = '".$email."'"); 
    	$emailVal = $dbHandler->count_rows_in_database($emailQuery);
    	
    	if($emailVal == 1) {
    		return $dbHandler->do_query("UPDATE farmers SET status = '2' WHERE email = '".$email."'");
    	} else {
    		return 0;
    	}
    }
    
    Code (markup):
    And this is class_database.php:

    
    class database {
    
    	public function connect_to_database() {
    		$dbConnect = mysql_connect("localhost", "Ashley456","4xLogin!%58S2f4") or die(mysql_error());
    		if($dbConnect) {
    			mysql_select_db("4xtrahands", $dbConnect) or die(mysql_error());	
    		}
    		return $dbConnect;
    	}
    	
    	public function close_connection_to_database($connection) {
    		mysql_close($connection);
    	}
    	
    	public function do_query($query) {
    		return mysql_query($query);	
    	}
    	
    	public function count_rows_in_database($countQuery) {
    		return mysql_num_rows($countQuery) or die(mysql_error());
    	}
    }
    
    Code (markup):
     
    oscardog, Aug 24, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    ..and where dose the email come from is it a form and how do you get it to $email .

    Can you show us that part ?
     
    MyVodaFone, Aug 24, 2010 IP
  3. learnerabn

    learnerabn Peon

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Select is not working means what error you are having?
    For me, the same code works well.
    put die(mysql_error() ) near mysql_query() function.
    so you can know the error.
     
    learnerabn, Aug 24, 2010 IP
  4. oscardog

    oscardog Peon

    Messages:
    57
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Fixed it. Had a minor problem with one of my other functions that makes the data database-safe.

    Thanks :)
     
    oscardog, Aug 24, 2010 IP