PHP Function With MySQLi Query

Discussion in 'PHP' started by hightide13, Nov 17, 2010.

  1. #1
    I have never actually written a function so I am not sure how to proceed.

    I am trying to write a function that shows how many results there are in the database for a certain city.

    I have a MySQLi query that returns the results properly. This is the code for that:

    
    City (
    <?
    
    if ($result = mysqli_query($link, "SELECT * FROM (units 
                    JOIN unituserdefinedvalues 
                    ON units.unitid = unituserdefinedvalues.unitid)
    		WHERE units.propid ='2'
    		AND units.city = 'City'
    		AND unituserdefinedvalues.userdefinedid ='9'
    		AND unituserdefinedvalues.value ='Yes'")) {
    
    $row_cnt = $result->num_rows;
    
    printf("%d \n", $row_cnt);
    
    $result->close();
    
    
    } else {
        print("0");
    }
    
    
    
    dbClose($link);
    ?>
    )
    
    
    
    Code (markup):
    The results look like: City (10)


    What I would like is to have a function where I could just add the following with the correct results:

    City (<?php property_count("City"); ?>)
    Code (markup):



    I feel like it should be something like this:
    function property_count($city)
    	{
    	$result = mysqli_query($link, "SELECT * FROM (units 
                    JOIN unituserdefinedvalues 
                    ON units.unitid = unituserdefinedvalues.unitid)
    		WHERE units.propid ='2'
    		AND units.city = $city
    		AND unituserdefinedvalues.userdefinedid ='9'
    		AND unituserdefinedvalues.value ='Yes'")) {
    
    $row_cnt = $result->num_rows;
    
    printf("%d \n", $row_cnt);
    
    $result->close();
    
    
    } else {
        print("0");
    }	
    		
    		
    } // end function property_count
    Code (markup):

    But that is giving me an error. I am sure that after starring at it for the past hour, it is something simple, but I am just not seeing it.

    I appreciate any thoughts.
     
    hightide13, Nov 17, 2010 IP
  2. austin-G

    austin-G Peon

    Messages:
    435
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try quotes around $city in the query. You had them before you put in the variable.

    AND units.city = '$city'
    Code (markup):
     
    austin-G, Nov 17, 2010 IP
  3. hightide13

    hightide13 Peon

    Messages:
    127
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks austin. But that did not do the trick. The error I am getting is:

    Warning: mysqli_query() expects parameter 1 to be mysqli, null given in xxxxx...
    Code (markup):
     
    hightide13, Nov 17, 2010 IP
  4. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #4
    What does var_dump($link) have to say ?
     
    ActiveFrost, Nov 17, 2010 IP
  5. hightide13

    hightide13 Peon

    Messages:
    127
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hey ActiveFrost. Excuse the ignorance, but I don't know what that is or how to do it.
     
    hightide13, Nov 17, 2010 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    Maybe this:
    AND units.city = '" . $city . "'
    Code (markup):
     
    MyVodaFone, Nov 17, 2010 IP
  7. Fruktkaka

    Fruktkaka Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #7
    var_dump prints out all information there is about a variable.

    Have a look here for examples:
    var_dump
     
    Fruktkaka, Nov 17, 2010 IP
  8. hightide13

    hightide13 Peon

    Messages:
    127
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ok. Thanks for helping with the var_dump. It is showing the correct city whenever i change the variable.

    ie.
    string(13) "Moncks Corner"
    string(7) "Awendaw"


    Sorry MyVodaPhone but your suggestion did not work. It seems for some reason that the query is producing no results based on the error. But like I said, outside of the function, it works properly...
     
    hightide13, Nov 18, 2010 IP
  9. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #9
    function property_count($city)
    {
    	if ($result = mysqli_query($link, "SELECT * FROM (units JOIN unituserdefinedvalues ON units.unitid = unituserdefinedvalues.unitid) WHERE units.propid ='2' AND units.city = $city AND unituserdefinedvalues.userdefinedid ='9' AND unituserdefinedvalues.value ='Yes'")) {
    
    		$row_cnt = $result->num_rows;
    
    		printf("%d \n", $row_cnt);
    
    		$result->close();
    
    	} else {
    
        	print("0");
    
    	}	
    				
    }
    PHP:
     
    ActiveFrost, Nov 18, 2010 IP
  10. hightide13

    hightide13 Peon

    Messages:
    127
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hey ActiveFrost.

    That still isn't doing it. Still getting the "Warning: mysqli_query() expects parameter 1 to be mysqli, null given in..." error
     
    hightide13, Nov 18, 2010 IP
  11. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #11
    Show us the line where $link is being initialized - I bet the problem isn't in the code you posted but have something to do with the actual connection handler.
     
    ActiveFrost, Nov 18, 2010 IP
  12. hightide13

    hightide13 Peon

    Messages:
    127
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    
    function dbConnect() {
    
        $link = mysqli_init( );
    
    
    
        if (!$link->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
           die('Setting MYSQLI_INIT_COMMAND failed');
        }
    
    
        $link->ssl_set(NULL, NULL, 'xxxx.pem', NULL, NULL);
    
    
        $link->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
    
    
        if(!$link->real_connect('xx.xxxxxxxx.com', "xxxxxx", 'xxxxxx', 'xxxxxx', 3306, NULL, MYSQLI_CLIENT_SSL)){
    
    	die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
    
        }
    
    
        if (!$link) {
           printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error());
           exit;
        }
    
        return $link;
    
    }
    
    
    
    
    function dbClose($link) {
        mysqli_close($link);
    }
    
    
    $link = dbConnect();
    
    
    
    
    PHP:
     
    hightide13, Nov 18, 2010 IP
  13. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #13
    Change your function to:

    
    function property_count($city)
    {
    echo "SELECT * FROM (units JOIN unituserdefinedvalues ON units.unitid = unituserdefinedvalues.unitid) WHERE units.propid ='2' AND units.city = $city AND unituserdefinedvalues.userdefinedid ='9' AND unituserdefinedvalues.value ='Yes'";
    
        if ($result = mysqli_query($link, "SELECT * FROM (units JOIN unituserdefinedvalues ON units.unitid = unituserdefinedvalues.unitid) WHERE units.propid ='2' AND units.city = $city AND unituserdefinedvalues.userdefinedid ='9' AND unituserdefinedvalues.value ='Yes'")) {
    
            $row_cnt = $result->num_rows;
    
            printf("%d \n", $row_cnt);
    
            $result->close();
    
        } else {
    
            print("0");
    
        }  
                   
    }
    
    PHP:
    Copy the query to your database front (if you are using phpmyadmin or command prompt) and see if it gives you the correct result.
     
    ThePHPMaster, Nov 18, 2010 IP