Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

mysql/php Query problem

Options
  • 26-10-2006 2:54pm
    #1
    Registered Users Posts: 1,987 ✭✭✭


    This is the function im using, baiscly what im trying to do is query the database to check and see if there is a row which matches the two variables passed in, and if a row exists that matches these two variables then to return 'true' but it theres no row with both fields the same as the variables passed in then it returns 'false'!
    Im going crazy because it half works, i've googled mysql and php querying but nothing seems to work!
    Any and all help appreciated!
    function check_ip($c,$club)
    {
    	$result = mysql_query("SELECT * FROM ip_address");  
    	$row = mysql_fetch_array( $result );
    
    	if(($row['ip'] == $c) && ($row['club'] == $club))
    	{
    		//Entry already exists
    		return "true";
    	}
    	else
    	{
    		//No entry exists
    		return "false";
    	}
    }
    


Comments

  • Closed Accounts Posts: 286 ✭✭Kev


    You're only checking the first row.


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    function check_ip($c,$club)
    {
    	$num_rows = count_ip();
    	for ($counter = 1; $counter <= $num_rows; $counter++)
    	{
    		$result = mysql_query("SELECT ip,club FROM ip_address WHERE id=".$counter."");  
    		$row = mysql_fetch_array( $result );
    
    		if(($row['ip'] == $c) && ($row['club'] == $club))
    		{
    			//Entry already exists
    			return "true";
    			break;
    		}
    		else
    		{
    			//No entry exists
    			return "false";
    		}
    	}
    }
    [COLOR="Red"]function count_ip()
    {
    	$sql = 'SELECT COUNT(id) AS numrows FROM ip_address';
    	$data = mysql_query($sql);
    	$row     = mysql_fetch_array($data, MYSQL_ASSOC);
    	$numrows = $row['numrows'];
    	
    	return $numrows;
    }[/COLOR]
    
    I tried adding in the above code to search the entire table but still no luck! Is there an easy way to search a table??


  • Closed Accounts Posts: 286 ✭✭Kev


    It's still returning false if the first row doesn't match, it should only return false after checking all rows


  • Registered Users Posts: 1,987 ✭✭✭Ziycon


    I rhink i got it, it seems to be working now! Thanks key!:D
    function check_ip($c,$club)
    {
    	$valid = false;
    	$num_rows = count_ip();
    	for ($counter = 1; $counter <= $num_rows; $counter++)
    	{
    		$result = mysql_query("SELECT ip,club FROM ip_address WHERE id=".$counter."");  
    		$row = mysql_fetch_array( $result );
    
    		if($row['ip'] == $c)
    		{
    			if($row['club'] == $club)
    			{
    				//Entry already exists
    				$valid = true;
    				break;
    			}
    		}
    	}
    	return $valid;
    }
    function count_ip()
    {
    	$sql = 'SELECT COUNT(id) AS numrows FROM ip_address';
    	$data = mysql_query($sql);
    	$row     = mysql_fetch_array($data, MYSQL_ASSOC);
    	$numrows = $row['numrows'];
    	
    	return $numrows;
    }
    


  • Registered Users Posts: 249 ✭✭frost


    If this is a small table, you might not see any difference in performance, but you're making more work for yourself and your server than you need to. That looping and checking through the data that you've done in PHP really should be done by your database, that's what it's good at!

    So if you want to look for a row that matches some criteria, put those criteria in the WHERE clause. No looping through the whole table in PHP is required by you, just a single SQL query, something like this:
    $sql = "SELECT 1 FROM ip_address WHERE ip='$c' AND club='$club' ";
    $result = mysql_query( $sql, $connection );
    
    if ($result && mysql_num_rows($result)) {
        $valid = true;
    } else { 
        $valid = false; 
    }
    

    I haven't tested this code so I may have syntax slightly wrong, but hopefully it points you in the right direction.


  • Advertisement
  • Registered Users Posts: 2,781 ✭✭✭amen


    not a PHP head (more C,C# etc) but is there a concept of a recordset in PHP ?
    Just curious


Advertisement