Jim R Posted September 1, 2019 Share Posted September 1, 2019 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 Quote Link to comment Share on other sites More sharing options...
benanamen Posted September 1, 2019 Share Posted September 1, 2019 As to the first, is $con supposed to be $conn? As to the second. You are missing the connection parameter. Quote Link to comment Share on other sites More sharing options...
Jim R Posted September 1, 2019 Author Share Posted September 1, 2019 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); Quote Link to comment Share on other sites More sharing options...
Jim R Posted September 1, 2019 Author Share Posted September 1, 2019 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 } Quote Link to comment Share on other sites More sharing options...
Jim R Posted September 2, 2019 Author Share Posted September 2, 2019 It has something to do with being in a Function, as best as I can tell. Quote Link to comment Share on other sites More sharing options...
benanamen Posted September 2, 2019 Share Posted September 2, 2019 (edited) $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 would also highly recommend you turn on error reporting. Edited September 2, 2019 by benanamen 1 Quote Link to comment Share on other sites More sharing options...
Jim R Posted September 2, 2019 Author Share Posted September 2, 2019 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 Quote Link to comment Share on other sites More sharing options...
benanamen Posted September 2, 2019 Share Posted September 2, 2019 (edited) 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 a lengthy thread, PM your code files to me and I will review them. Edited September 2, 2019 by benanamen Quote Link to comment Share on other sites More sharing options...
Jim R Posted September 2, 2019 Author Share Posted September 2, 2019 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(); } Quote Link to comment Share on other sites More sharing options...
benanamen Posted September 2, 2019 Share Posted September 2, 2019 How and where are you calling the function? Quote Link to comment Share on other sites More sharing options...
benanamen Posted September 2, 2019 Share Posted September 2, 2019 (edited) 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 September 2, 2019 by benanamen Quote Link to comment Share on other sites More sharing options...
Jim R Posted September 2, 2019 Author Share Posted September 2, 2019 3 hours ago, benanamen said: How and where are you calling the function? echo '<div class="jump_menu">' . sectional_select() . '</div>'; Quote Link to comment Share on other sites More sharing options...
Jim R Posted September 2, 2019 Author Share Posted September 2, 2019 (edited) 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 September 2, 2019 by Jim R Quote Link to comment Share on other sites More sharing options...
Barand Posted September 2, 2019 Share Posted September 2, 2019 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; } 1 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 2, 2019 Share Posted September 2, 2019 What's with this line and the parens??? $query_sect = ("SELECT * FROM a_sectionals_bkb"); Quote Link to comment Share on other sites More sharing options...
Jim R Posted September 2, 2019 Author Share Posted September 2, 2019 5 hours ago, Barand said: You need to pass the $con to your function. IE That was the final straw with it. I tried it in each place, in the functions file and on the main file, but I didn't have it in both places at the same time. Quote Link to comment Share on other sites More sharing options...
Jim R Posted September 2, 2019 Author Share Posted September 2, 2019 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 2, 2019 Share Posted September 2, 2019 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. 1 Quote Link to comment Share on other sites More sharing options...
Jim R Posted September 2, 2019 Author Share Posted September 2, 2019 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? Quote Link to comment Share on other sites More sharing options...
Jim R Posted September 2, 2019 Author Share Posted September 2, 2019 Never mind. Figured that last part out. Thank you everyone! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.