I'm returning here after a couple of years... life has taken me on a different path, but I'm returning to this and STILL trying to get something that works.
I'm not a pro front-end developer, am in the process of learning, but in the interest of "code-first, learn-second" I'm trying to get a page to load using multiple dropdowns fed by MySQL stored procedures. While I'm always interested in the "best way to do this" (e.g. "why aren't you doing OOP?" or "why are you trying to do such-and-such THAT way?), I'll simply say I'm learning, but for right now, I'm just trying to figure out "Why won't this work?" I've watched many videos on YouTube - scoured articles here - and still can't figure out what is wrong with this. This seems like it should be a lot simpler than it is, and can't figure out why I can't get this to work, even IF it's not the best practice. The videos I watch inevitably show how to get A (single) procedure to populate a dropdown. I can get A (single) dropdown to populate; I just can't get two or more on the same page.
The tables in my DB are overly simple, and again, not really the point. I have a "year" table which basically (for simplicity's sake) has an "id" which is the year and a column (which is also the year). So:
year_id year
2017 2017
2018 2018
Don't ask my "why" - it's simple just to try to prove the concept (to get it to work).
My "teams" table has a seq and a name
team_seq team_name
1 joe_smith
2 tom_jefferson
I have two procedures, one called ref_sp_Year_Ref, which is a simple select on Year, and another procedure called ref_sp_Team_Ref, which is also a simple select.
The procedures run fine in MySQL; no problem.
If I take the SELECT statements out of the MySQL procedure, and type them as a "Select" into a variable in my PHP code, it works fine and the dropdowns populate. i.e. If I have:
$qry1 = "Select ... <the rest of the Team query"
$qry2 = "Select ... <the rest of the Year query"
Then the dropdowns work.
If I use $qry1 = "Call ref_sp_Team_Ref" and $qry2 = "Call ref_sp_Year_Ref" then I can get the FIRST dropdown to populate, but the second one won't.
Below is my current code. Again, I'm SURE there are better ways - the answers to my original question two years ago have already suggested that. But I want to understand WHY THIS won't work. Why does it work with "Selects" in the PHP but not with Stored procs?
Running this results in the "Teams" dropdown populating correctly, but then an error message appears where the "Year" dropdown would be, and says: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, book given in: and it references this line: if (mysqli_num_rows($years) > 0) The second error is essentially the same, referencing this line: mysqli_free_result($years);
In both cases, it seems evident to me that the second proc either isn't being called or isn't running, and it came back empty. I don't "get" why...
I will say - in response to the original answers - that previously I was using "multi_query" when clearly it wasn't a multi query. At the time, I had just typed that out of a book and didn't understand what that was about. Presently though, since the top procedure (ref_sp_team_ref) DOES return results, my guess is that my problem isn't the call itself or the syntax of the call. I just don't know why ref_sp_year_ref won't also run and load
Additionally I might add: In my PHP code, if I have a SELECT query in my first call (Teams), and a procedure call in my second call (Years), it works. Both dropdowns populate. THAT is what I don't "get". Why will two "Selects" work, or one "select" and one procedure call, but two procedure calls won't work?
Thanks in advance for any assistance.
<form method="post" action="">
<?php
require_once('../../../mysqlconnect_mysqli.php');
/* TEAM DROPDOWN */
$qry1 = "CALL ref_sp_Team_Ref()";
$teams = mysqli_query($conn,$qry1);
if (mysqli_num_rows($teams) > 0)
{
echo "<label for='teams'>Teams: </label>";
echo "<select name='teams' size='1' required>";
echo "<option>Team Names</option>";
while($row = mysqli_fetch_array($teams))
{
echo "<option value=\"{$row['team_seq']}\">{$row['team']}</option>";
}
echo "</select>";
echo "<br /><br />";
}
else
{
echo "Why is this empty?";
}
mysqli_free_result($teams);
/* YEAR DROPDOWN */
$qry2 = "CALL ref_sp_Year_Ref()";
$years = mysqli_query($conn,$qry2);
if (mysqli_num_rows($years) > 0)
{
echo "<label for='years'>Years: </label>";
echo "<select name='years' size='1' required>";
echo "<option>Seasons</option>";
while($row = mysqli_fetch_array($years))
{
echo "<option value=\"{$row['year_id']}\">{$row['year']}</option>";
}
echo "</select>";
echo "<br /><br />";
}
else
{
echo "Why is this empty?";
}
mysqli_free_result($years);
echo " <input type='submit' name='submit1' value='Get Results'/>";
echo "</form>";
?>
My earlier post (from 2 years ago) is below.
I've read a number of posts on this topic, but still can't seem to locate my problem. I would consider myself a beginner when it comes to HTML Forms and PHP. I have extensive experience using stored procedures (T-SQL, Oracle, and new to MySQL procedures). My belief is that my problem here is in PHP or HTML - the MySQL stored procedures seem ok when executed.
I'm trying to create multiple dropdowns in the HTML page populated from the Procs. I'm not doing anything fancy yet, e.g. dynamic selection of 1st value sent to proc of second... I'm just trying to do basic "load" of all dropdowns.
The following code causes the first dropdown ("Years") to populate fine, but the second dropdown ("Teams") is empty. If I switch the order of the blocks, "Teams" will populate fine, but "Years" will be empty.
I'm sure I'm just missing something very simple... but help would be greatly appreciated.
My code is:
<form method="post" action="">
<p>
Years:
<select name="years">
<?php
#the following returns the connection variable $dbh
require_once('../../../mysqlconnect_mysqli.php');
$sql = "call ref_sp_Year_Ref";
if ($mysqli->multi_query($sql))
{
#Get first data set from Procedure - do nothing with it
$result = $mysqli->store_result();
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
echo
"<option value='" . $row['year_id'] . "'>" . $row['year'] . "</option>";
}
mysqli_free_result($result);
}
?>
</select>
</p>
<p>
Teams:
<select name="teams">
<?php
#the following returns the connection variable $dbh
require_once('../../../mysqlconnect_mysqli.php');
$sql = "call ref_sp_Team_Ref";
if ($mysqli->multi_query($sql))
{
#Get first data set from Procedure - do nothing with it
$result = $mysqli->store_result();
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
echo
"<option value='" . $row['team_id'] . "'>" . $row['team'] . "</option>";
}
mysqli_free_result($result);
}
?>
</select>
</p>
<br>
<input type="submit" name="submit1" value="Get Results"/>
</form>