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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Warning: odbc_fetch_array(): 7 is not a valid ODBC result resource... 1

Status
Not open for further replies.

hsilveira

Programmer
Mar 25, 2009
7
PT
Hi all,

I have a problem and i don't know how to solve it.

I'm using odbc_fecth_array to retrieve some data from my database. (See below)

while($registo=odbc_fetch_array($resultado))
{
$CreatedBy = $registo["CreatedBy"];
$CreationDate = retrieve_data_DB($registo["CreationDate"]);
$AssignTo = $registo["AssignTo"];
$Status = $registo["Status"];
$StatusName = RetrieveStatus($db, $user, $pass, $Status);

print("
<tr>
<td align=center>$CreatedBy</td>
<td align=center>$CreationDate</td>
<td align=center>$AssignTo</td>
<td align=center>$StatusName</td>
</tr>");
}

$StatusName is calculated with function RetrieveStatus that receives one Status Key and return the name of that status.

When i run the script i got the error: "Warning: odbc_fetch_array(): 7 is not a valid ODBC result resource in C:\Inetpub\ on line 184
".

If i comment the line where i call the function to retrieve the Status Name everything works fine.

Somebody help me please...

Thanks
 
We would need to know what is going on inside the function RetrieveStatus();

Can you show us the functions code?

Also as per the error, what is line 184 and the lines around it in projectissuesdetail.php.

It look like you are connecting to the Db again inside the function, something may be going wrong in there to generate the error.







----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi vacunita,

I thing the error happens because i can't call a functions inside the "while($registo=odbc_fetch_array($resultado))
". This is the line nº 184.

****
function RetrieveStatus($db, $user, $pass, $Status)
{
$ligacao = odbc_connect("$db", "$user", "$pass");

$comando_sql = "SELECT * FROM [ProjectIssuesStatus] WHERE ID LIKE '$Status'";

$resultado = odbc_do($ligacao, $comando_sql);

While($registo=odbc_fetch_array($resultado))
{
$StatusName = $registo["Code"];
}

odbc_close($ligacao);

return $StatusName;

}

Do you know if i can use other info to display inside the odbc_fetch_array?

Thanks in advance
 
I thing the error happens because i can't call a functions inside the "while($registo=odbc_fetch_array($resultado))

I don't think that's the problem. there is no limitation to calling functions inside a while loop.

Looks like maybe the secondary query is not returning anything.

Add this to the function:

Code:
  $comando_sql = "SELECT * FROM [ProjectIssuesStatus] WHERE ID LIKE '$Status'";

  $resultado = odbc_do($ligacao, $comando_sql)[red]or die(odbc_errormsg($ligacao));[/red];


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
You might get this error if the SQL didn't execute correctly e.g. because of a syntax error in the SQL. Can you put a call to odbc_errormsg() and odbc_error() after the odbc_do(). It might be worthwhile putting them after the connect as well.
 
I think the limitation exist when we use the odbc_fetch_array.

I follow your suggestion but the error persists.

On the other hand, the function returns the correct result and put it in the screen. The problem is the warning that is also displayed. If i don't call my function, php returns 1 that is the value that i have in the DB. My function allow me to display "Open" instead of "1".

Any other idea?

Thanks again
 
The point of the added c ode was not to clear the error, but to find out whether the query was nor returning any results.

Do you get anything else when you add the "or die(odbc_errormsg());"

Or just echo it by itself after having executed the query?:

echo odbc_errormsg();

Does it produce a more descriptive error?


Removing the call to the function doesn't fix the error, it just prevents the code that is generating the error from running. So no error is generated.






----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Can you create a stand alone version of the code i.e. connect to the Database, execute the query and loop using odbc_fetch_array. In essense a version of your retreivestatus function.
Show us the output including any error messages.
Sonmething subtly must be going on as odbc_fetch_array just passes a value in $registo to use later, your code looks ok.
Run the code from the command prompt by using something like.
c:\php testfile.php
 
Do you get anything else when you add the "or die(odbc_errormsg());"

Nothing else was shown on the screen

Or just echo it by itself after having executed the query?:

echo odbc_errormsg(); returns no data
 
Which means the query must be running o.k.

I agree with ingresman.

If you just run your function alone by itself, do you get the same error?

Additionally is $status a string or an integer?

If you echo your query does it look o.k? Can you run it directly on the DB?

echo $comando_sql;




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I do a simple test that is create a new php test file with this code that uses my function RetrieveStatus:

<?php
require("information.php"); //this file has DB Info
require("Methods/GlobalFunctions.php"); //This file has Retrieve Status function
echo(RetrieveStatus($db, $user, $pass, '1'));
?>

This code display Open on the screen so the function is working perfectly.

In the code below:

while($registo=odbc_fetch_array($resultado))
{
$CreatedBy = $registo["CreatedBy"];
$CreationDate = retrieve_data_DB($registo["CreationDate"]);
$AssignTo = $registo["AssignTo"];
$Status = $registo["Status"];
$StatusName = RetrieveStatus($db, $user, $pass, $Status);

print("
<tr>
<td align=center>$CreatedBy</td>
<td align=center>$CreationDate</td>
<td align=center>$AssignTo</td>
<td align=center>$StatusName</td>
</tr>");
}

If i run this i got the warning and all my data below the warning. if i comment the line:
$StatusName = RetrieveStatus($db, $user, $pass, $Status);

everything works fine, i have all my lines displayed in the screen without any warning.

In conclusion, both pieces of code work fine independently but when i use the function inside the while i git the warning.

 
So, you execute a query and then in that loop you call your RetrieveStatus which also issues a select?.
I'm wondering if your function is upsetting the value in $registo.
You see this is why we like to see the code and the run time results.
 
That's it... i have a query and then in that loop i call RetrieveStatus which also issues a select.

How can i workaround this problem?

Thanks in advance

 
I'm sorry I have to go now. Phil might solve it but in the mean time can you show the code for your entire test code and the output it shows.
Just for good measure can you show the table structure and what database are you working with ? - does it support multiple connections ?
 
Thank you all.

Done.

I'm using SQL Server 2005 so i create another ODBC connection and it worked. thank's a lot ingresman.

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top