Jump to content

Having an issue with a query that should be very easy...


Jim R

Recommended Posts

I've actually ported the query below from something else I've done that works.  Not sure what I'm missing.

 

Here are the errors I'm getting:

Quote

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home2/csi/public_html/resources/preview1920/functions.php on line 9

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in /home2/csi/public_html/resources/preview1920/functions.php on line 11

 

 

	$query = "SELECT id FROM a_sectionals_bkb";

	$results = mysqli_query($con,$query);  //=>  Line 9
	
	echo mysqli_error($results);  //=> Line 11
	
	
	echo '<div class="sectional_select">';
		echo '<form name="Sectionals">';
		echo '<select name="sectional" size="3" onChange="go()">';

				echo '<option value="NULL">Select Sectional</option>';
				while($row = mysqli_fetch_assoc($results)) {
					echo '<option value="/season-preview/19-20/sectional1920/?sectional='. $row['id'] .'">Sectional '. $row['id'].'</option>';
				}

		echo '</select>';
	
	// End Sectional Select
	echo '</div>';

// End of Sectional Select Function

 

Link to comment
Share on other sites

Just now, benanamen said:

As to the first, is $con supposed to be $conn?

As to the second. You are missing the connection parameter.

 

The connection parameter is there.  I just didn't copy it over, and this query is working just fine...

	$query = "SELECT * FROM a_coach
				WHERE email IS NULL";

	$results = mysqli_query($con,$query);
	
	$num_rows = mysqli_num_rows($results);
	echo mysqli_error($con);
		

 

Link to comment
Share on other sites

I had the above wrapped in a function.  When I remove the function aspect of it, the query works, except the 

    $results = mysqli_query($con,$query);
 

 function sectional_select() {

	$query = "SELECT id FROM a_sectionals_bkb";

	$results = mysqli_query($con,$query);
	
	echo mysqli_error($results);
	
	
	echo '<div class="sectional_select">';
		echo '<form name="Sectionals">';
		echo '<select name="sectional" size="3" onChange="go()">';

				echo '<option value="NULL">Select Sectional</option>';
				while($row = mysqli_fetch_assoc($results)) {
					echo '<option value="/season-preview/19-20/sectional1920/?sectional='. $row['id'] .'">Sectional '. $row['id'].'</option>';
				}

		echo '</select>';
	
	// End Sectional Select
	echo '</div>';

// End of Sectional Select Function
 }	
	

 

Link to comment
Share on other sites

1 minute ago, benanamen said:

$con is outside the scope of your function. The function does not know it exists. You need to pass $con to the function. I would suggest learning about variable scope.

I've been reading up on that, but I get a different error when I add it to the function.  

 

 function sectional_select($con) {  => line 5
 
	$query_sect = ("SELECT * FROM a_sectionals_bkb");

	$results_sect = mysqli_query($con,$query_sect);  => line 9
	
	echo mysqli_error($results_sect);  => line 11
	
	
	echo '<div class="sectional_select">';
		echo '<form name="Sectionals">';
				echo '<select name="sectional" size="3" onChange="go()">';
				echo '<option value="NULL">Select Sectional</option>';
				while($row_sect = mysqli_fetch_assoc($results_sect)) {
					$row_sectionals = $row_sect['id'];
				
					echo '<option value="/season-preview/19-20/sectional1920/?sectional='. $row_sectionals .'">Sectional '. $row_sectionals .'</option>';
				}
				echo '</select>';
	
	// End Sectional Select
	echo '</div>';

// End of Sectional Select Function
 }	
	

 

Produces these errors:

 

Warning: Missing argument 1 for sectional_select(), called in /home2/csi/public_html/resources/preview1920/sectional-header.php on line 22 and defined in /home2/csi/public_html/resources/preview1920/functions.php on line 5

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home2/csi/public_html/resources/preview1920/functions.php on line 9

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in /home2/csi/public_html/resources/preview1920/functions.php on line 11

 

Link to comment
Share on other sites

3 minutes ago, benanamen said:

Seems to me that whatever your file setup is, $con is not accessible to the file you have the function in. And your still missing the $con parameter for the error call. To avoid an lengthy thread, PM your code files to me and I will review them.

 

I like to solve as much here so others can learn.  If you want to PM after this, that'll be fine, but here is what you're looking for...

 

include (ABSPATH ."resources/con.php");


 function sectional_select($con) {
 
	$query_sect = ("SELECT * FROM a_sectionals_bkb");

	$results_sect = mysqli_query($con,$query_sect);
	
	echo mysqli_error($results_sect);
	
	
	echo '<div class="sectional_select">';
		echo '<form name="Sectionals">';
				echo '<select name="sectional" size="3" onChange="go()">';
				echo '<option value="NULL">Select Sectional</option>';
				while($row_sect = mysqli_fetch_assoc($results_sect)) {
					$row_sectionals = $row_sect['id'];
				
					echo '<option value="/season-preview/19-20/sectional1920/?sectional='. $row_sectionals .'">Sectional '. $row_sectionals .'</option>';
				}
				echo '</select>';
	
	// End Sectional Select
	echo '</div>';

// End of Sectional Select Function
 }	

 

Here is con.php

 

$con = mysqli_connect("localhost","username","password", "table");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
Link to comment
Share on other sites

I ran the code and it works. The only problem is you need to change the error variable like so: echo mysqli_error($con);

Add this to the top of your page: mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

If your still having problems it is likely with the path to $con. You need to check that ABSPATH ."resources/ points to where con.php is.

Edited by benanamen
Link to comment
Share on other sites

3 hours ago, benanamen said:

The only problem is you need to change the error variable like so: echo mysqli_error($con);

As I understand it, if I put $con in there, it will only show errors related to $con.  There aren't any errors related to $con.

3 hours ago, benanamen said:

If your still having problems it is likely with the path to $con. You need to check that ABSPATH ."resources/ points to where con.php is.

 It's the same connection I'm using throughout my site, not to mention what is used to populate the rest of the page that is linked.  

 

I'd like to know why it's not working within the confines of a Function.  If I give it its own file and add it as an include, it works.  I'd just like to be more efficient in my code.

Edited by Jim R
Link to comment
Share on other sites

38 minutes ago, Jim R said:

 echo '<div class="jump_menu">' . sectional_select() . '</div>';

You need to pass the $con to your function. IE

echo '<div class="jump_menu">' . sectional_select($con) . '</div>';
                                                  ^^^^ 

Also your function should return the text value to be printed instead of echoing within the function EG

function sectional_select($con)
{
    $output = '<select>';
    $output .= "<option value=''>- select sectional -</option>";
      // add options ...
    $output .= "</select>";
    
    return $output;
}

 

  • Like 1
Link to comment
Share on other sites

11 minutes ago, ginerjm said:

What's with this line and the parens???

$query_sect = ("SELECT * FROM a_sectionals_bkb");

 

I was trying everything.  I saw someone who had code working that had parenthesis.  It didn't make a difference with my problem, and I just never deleted them.  

Link to comment
Share on other sites

They are entirely unnecessary.

PS - it is generally not recommended to use the "select *" query format.  Always select the fields that you truly want to obtain from the query and not just do the catchall "*" operand.  This way you (and those behind you) know what field names you have acquired from the query and in what order they are in should you choose to use numeric indices.

  • Like 1
Link to comment
Share on other sites

5 hours ago, Barand said:

You need to pass the $con to your function. IE


echo '<div class="jump_menu">' . sectional_select($con) . '</div>';
                                                  ^^^^ 

Also your function should return the text value to be printed instead of echoing within the function EG


function sectional_select($con)
{
    $output = '<select>';
    $output .= "<option value=''>- select sectional -</option>";
      // add options ...
    $output .= "</select>";
    
    return $output;
}

 

 

Last part of this now:  I really mean for that form to be a jump menu, so when they make a choice it automatically redirects the User to a new page.  

Where do I put the javascript?  On the Functions file, the main file or its own file and reference it?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.