• Hi Guys

    Why is it that when i put this inside a function it does not work, but if I create my own MySQLI connection inside the function it does work

    function name{
    global $wpdb;
        $options = array();
    	$res=mysqli_query($wpdb,"select ID,County from counties ORDER BY County ASC");
    	$value="";
    	$label="Please select a county";
    	$options[] = array('label' => $label, 'value' => $value);
    		while($row=mysqli_fetch_array($res)) {
    		$sC=$row['ID'];
    		$res1=mysqli_query($wpdb,"select County from wedding_shows where County='$sC'");
    			if (mysqli_num_rows($res1) == 0) {
    			} else {
    			$options[] = array('label' => $row['County'], 'value' => $row['ID']);
    			}
    		}
    }

    Here are the errors i get

    Warning: mysqli_query() expects parameter 1 to be mysqli, object given eval()’d code on line 3

    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given eval()’d code on line 4

    and here is it working with my own connection

    function name(
    $dbhost1 = 'localhost';
    $dbuser1 = 'user';
    $dbpass1 = 'pass';
    $dbname1 = 'DBName';
    
    $conn=mysqli_connect("localhost","$dbuser1","$dbpass1","$dbname1");
    $options = array();
    $res=mysqli_query($conn,"select ID,County from counties ORDER BY County ASC");
    	$value="";
    	$label="Please select a county";
    	$options[] = array('label' => $label, 'value' => $value);
    		while($row=mysqli_fetch_array($res)) {
    		$sC=$row['ID'];
    		$res1=mysqli_query($conn,"select County from wedding_shows where County='$sC'");
    			if (mysqli_num_rows($res1) == 0) {
    			} else {
    			$options[] = array('label' => $row['County'], 'value' => $row['ID']);
    			}
                     }
    }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘MySQLI Query issue’ is closed to new replies.