Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What I want to do is execute a quer

Status
Not open for further replies.

Apollo6

Technical User
Jan 27, 2000
418
US
What I want to do is execute a query. Then populate a dropdown box with the results. What I need to be in the dropdown is, both FirstName and LastName. When the following code runs, I get the dropdown, and it has the correct number of "spaces" but I am not seeing any names. For example, my query returns 7 people, the dropdown has 7 empty spots. What am I missing in the code...

--QUERY--

$s_emp_query = "SELECT UPEMPL.LASTNAME, UPEMPL.FIRSTNAME, UPEMPL.SSN
FROM
soinc.UPEMPL
WHERE
UPEMPL.SUPERVSR = '$s_supervisor'";
$s_result3 = odbc_exec($connect_soinc, $s_emp_query);
$s_emp_count = 0;
while (odbc_fetch_row($s_result3))
{
$s_emp_count++;
}

--DROPDOWN--

if ($s_emp_count > 0)
{
echo &quot;<select>\n&quot;;
for ($i=0;$i<$s_emp_count;$i++)
{
$s_employees = pg_fetch_array($s_result3);
echo &quot;<option value=$s_employees[0]>$s_employees[0]</option>\n&quot;;
}
echo &quot;</select>\n&quot;;
}
 
Your code confuses me.

In one place you are using odbc_* function calls, in another, pg_* function calls. I strongly recommend that you use only one or the other, and the you use the most specific function family.

The resource handle is not an object. It most resembles a file handle. When you loop through the entire return, the pointer into the data is set to the end of the data.

Then you use a pg_fetch_array() fetch technique on a resource handle that is at the end of the data. The only reason at all it is outputting the options is that you're forcing the fetches by using the loop.

How about you use a program structure like:

pg_connect (...);
$result = pg_query (&quot;your query&quot;);
if (pg_num_rows($result) > 0)
{
print &quot;<select>&quot;;
while ($avariable = pg_fetch_array ($result))
{
print '<option value=&quot;' . $avariable[0] . '&quot;>' . $avariable[0] . '<option>';
}
print '</select>';
}

The code is much simpler, does not have to loop through the data twice, and does not mix odbc and pg families of functions.

Want the best answers? Ask the best questions: TANSTAAFL!
 
Ok, here's what's happening... you're fetching all the rows, then you're at the end. So you go to fetch 7 more times, but you're already at the end.

I'm not quite sure why you're mixing pg_fetch_array and odbc_fetch_row though... I'll assume ODBC is correct... then what you should do is...
Code:
echo &quot;<select>\n&quot;;
while ($row = odbc_fetch_row($s_result3)) {
  echo '<option value=&quot;'.$row[0].'&quot;>'.$row[0].'</option>'.&quot;\n&quot;;
}
echo &quot;<select>\n&quot;;

and drop that $s_emp_count++ code at the top.

-Rob
 
Both of you are exactly right about the mixing of the function calls. The project is actually pulling data from four different types of databases. In this specific issue, I need to be using the odbc_* functions.

You both basically sent the same solution but I went with skiflyer's suggestion but still only come back with a blank drop down (I pasted the exact post.) I can echo out the number of records that comes back from the query and that is correct.

Could my query be causing this due to pulling two fields per record and then I'm trying to put them into a dropdown that can only take 1 element? I would post the code for the dropdown but again, it's exactly skiflyer's suggestion.

Thanks for the explanation on it as well as the help. I'll keep trying.

 
try this...
Code:
echo &quot;<pre>&quot;;
while ($row = odbc_fetch_row($s_result3)) {
  print_r($row);
  echo &quot;<hr />&quot;;
}

See what that shows you, it should print out the structure of row in such a way that you can see how to access the data.

-Rob
 
I took out the code that looped and incremented a counter. I still use $s_count to determine which database returns a hit. I know get the dropdown, again with 7 spaces (7 records is correct amount). Under that I should have seen the results of $s_result3, but do not see anything. This is leading me to think I've got issues somewhere else in the script.

Here is the current code after the suggestions were applied:

--Two queries above this block of code--

$s_result1 = odbc_exec($connect_soinc, $s_query1);
$s_result2 = odbc_exec($connect_soinc, $s_query2);
$s_count=(int)odbc_result($s_result2,1);
--This $s_count will return either 1 or 0...

$s_sup_first = trim(odbc_result($s_result1, 1));
$s_sup_last = trim(odbc_result($s_result1, 2));
$s_supervisor = $s_sup_first . &quot; &quot; . $s_sup_last;

--QUERY--
$s_emp_query = &quot;SELECT UPEMPL.LASTNAME, UPEMPL.FIRSTNAME
FROM
soinc.UPEMPL
WHERE
UPEMPL.SUPERVSR = '$s_supervisor'&quot;;
$s_result3 = odbc_exec($connect_soinc, $s_emp_query);

--DROPDOWN--
if ($s_count > 0)
{
echo &quot;<select>\n&quot;;
while ($row = odbc_fetch_row($s_result3)) {
echo '<option value=&quot;'.$row[0].'&quot;>'.$row[0].'</option>'.&quot;\n&quot;;
}
echo &quot;<select>\n&quot;;
}
echo &quot;<pre>&quot;;
while ($row = odbc_fetch_row($s_result3)) {
print_r($row);
echo &quot;<hr />&quot;;
}
 
Code:
if ($s_count > 0)
{
echo &quot;<select>\n&quot;;
while ($row = odbc_fetch_row($s_result3)) {
echo '<option value=&quot;'.$row[0].'&quot;>'.$row[0].'</option>'.&quot;\n&quot;;
}
echo &quot;<select>\n&quot;;
}

Comment that out, and see what gets printed.

-Rob
 
I return nothing. That's telling me that there is a problem with my $s_result3. I just don't understand how I can count the number of records in $s_result3 but when trying to echo out the results I get blanks... Still looking at the other parts of the script. I'm fairly sure that it's not the syntax of populating the dropdown. Thanks again for the help.
 
Having trouble with this dropdown issue. I'm filling a dropdown from a PostgreSQL table with no problems. The only difference is using the odbc_* functions against a Pervasive.SQL database. I've pasted the entire script if anyone out there has time to see what I'm not doing correctly.


<?
include_once(&quot;logonemp.inc&quot;);
session_start();

$connection = pg_connect (&quot;host=$host dbname=$db user=$user
password=$pass&quot;);
if (!$connection)
{
die(&quot;Could not open connection to PostgreSQL&quot;);
}

$connect_soinc = odbc_connect(&quot;soinc&quot;,&quot;&quot;,&quot;&quot;);
if (!$connect_soinc)
{
die(&quot;Error in SOINC connection&quot;);
}

$connect_topllc = odbc_connect(&quot;TOPLLC&quot;,&quot;&quot;,&quot;&quot;);
if (!$connect_topllc)
{
die(&quot;Error in TOPLLC connection&quot;);
}

$user_id = $_SESSION['SESSION_UID'];
$ss_num = $_SESSION['SUP_SSN'];

//TOPLLC queries...
$t_query1 = &quot;SELECT UPEMPL.FIRSTNAME, UPEMPL.LASTNAME
FROM
topllc.UPEMPL
WHERE
UPEMPL.SSN = '$ss_num'&quot;;

$t_query2 = &quot;SELECT COUNT(*)
FROM
topllc.UPEMPL
WHERE UPEMPL.SSN = '$ss_num'&quot;;

$t_result1 = odbc_exec($connect_topllc, $t_query1);
$t_result2 = odbc_exec($connect_topllc, $t_query2);
$t_sup_first = trim(odbc_result($t_result1, 1));
$t_sup_last = trim(odbc_result($t_result1, 2));
$t_supervisor = $t_sup_first . &quot; &quot; . $t_sup_last;

$t_query3 = &quot;SELECT UPEMPL.FIRSTNAME, UPEMPL.LASTNAME, UPEMPL.SSN
FROM
topllc.UPEMPL
WHERE
UPEMPL.SUPERVSR = '$t_supervisor'&quot;;
$t_result3 = odbc_exec($connect_topllc, $t_query3);

$t_count=(int)odbc_result($t_result2,1);

//SOINC queries...

$s_query1 = &quot;SELECT UPEMPL.FIRSTNAME, UPEMPL.LASTNAME
FROM
soinc.UPEMPL
WHERE
UPEMPL.SSN = '$ss_num'&quot;;

$s_query2 = &quot;SELECT COUNT(*)
FROM
soinc.UPEMPL
WHERE UPEMPL.SSN = '$ss_num'&quot;;

$s_result1 = odbc_exec($connect_soinc, $s_query1);
$s_result2 = odbc_exec($connect_soinc, $s_query2);
$s_count=(int)odbc_result($s_result2,1);
$s_sup_first = trim(odbc_result($s_result1, 1));
$s_sup_last = trim(odbc_result($s_result1, 2));
$s_supervisor = $s_sup_first . &quot; &quot; . $s_sup_last;

$s_emp_query = &quot;SELECT UPEMPL.LASTNAME, UPEMPL.FIRSTNAME
FROM
soinc.UPEMPL
WHERE
UPEMPL.SUPERVSR = '$s_supervisor'&quot;;
$s_result3 = odbc_exec($connect_soinc, $s_emp_query);
?>
<!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
&quot;<html>
<head>
<title>Employee List</title>
</head>
<body bgcolor=&quot;#FFFFFF&quot; background=&quot;background2.gif&quot;>
<form name=&quot;SubMain&quot; action=&quot;benefit_summary.org.php&quot; method=&quot;post&quot;><div align=&quot;center&quot;>
<img src=&quot;tnologo.gif&quot; /> <div align=&quot;center&quot;>
<hr width=&quot;50%&quot; />
<div align=&quot;center&quot;> <font color=&quot;#000000&quot;><font size=3><strong>AVAILABLE
EMPLOYEES:</strong></font><br />
<br />
<?
if ($t_count > 0)
{
echo &quot;<select name=employees>\n&quot;;
while ($row = odbc_fetch_row($t_result3)) {
echo '<option value=&quot;'.$row[0].'&quot;>'.$row[0].'</option>'.&quot;\n&quot;;
}
echo &quot;</select>\n&quot;;
}
else
if (s_count > 0)
{
echo &quot;<select name=employees>\n&quot;;
while ($row = odbc_fetch_row($s_result3)) {
echo '<option value=&quot;'.$row[0].'&quot;>'.$row[0].'</option>'.&quot;\n&quot;;
}
echo &quot;<select>\n&quot;;
}
echo &quot;<br />&quot;;
pg_close ($connection);
odbc_close ($connect_soinc);
odbc_close ($connect_topllc);
?>
</div>
<hr width=&quot;50%&quot; />
<p>
<div align=&quot;center&quot;>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;SUBMIT&quot;>
</p></form>
</body>
</html>
 
Finally... For those who might want to know, after some reading, odbc_fetch_row function returns boolean. So this will not bring back a recordset. I used the following code to populate the dropdown box using odbc_* functions:

if ($s_count > 0)
{
echo &quot;<select name=employees>\n&quot;;
while ($row = odbc_fetch_row($s_result3)) {
$firstName = odbc_result ($s_result3,'firstname');
$lastName = odbc_result ($s_result3,'lastname');
$out = $firstName . $lastName;
echo '<option value=&quot;'.$out.'&quot;>'.$out.'</option>'.&quot;\n&quot;;
}
echo &quot;<select>\n&quot;;
}
 
Me too, and I had done some programming with ODBC. My apologies for giving the bad advice :(

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top