Jump to content


Photo

passing resource type parameters to a function


  • Please log in to reply
2 replies to this topic

#1 Balto

Balto
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 29 March 2006 - 07:49 AM

Dear all,
the following PHP script produces the warnings

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
in fetchArray function

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource
in releaseQuery function

is there a way to tell these function that we are passing a resource for them?

thanks,
balto



function connectMydb(){
$link=mysql_connect('localhost', 'mydb', 'pass') or die ('could not connect to Database');
mysql_select_db("mydbases");
return $link;
}

//--------------------------------- Query ------------------------------------
function queryMydb($stmt){
return mysql_query ($stmt) or die (mysql_error());
}

//------------------------------- Release Query ---------------------------
function releaseQuery($query){
mysql_free_result ($query);
}
//----------------------------- fetch a row ---------------------------------
function fetchArray ($query){
return mysql_fetch_array($query, MYSQL_ASSOC);
}

$link = connectMydb();
$result=queryMydb("select * from mytable");
$row=fetchArray($result);
//.... do something with data.


#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 March 2006 - 02:58 PM

Why do you want to add a layer of complexity to calling the MySQL functions?

What happens if you change your function arguments to include the database connect variable ($link in your code)?

Ken

#3 Balto

Balto
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 30 March 2006 - 12:37 AM

well, mainly because I want the same code to be able to sit on mysql and odbc databases. now the functions follow the same paradigm but have different names. I figured that a layer of abstraction would help provide independency from the underlying engine.

anyway, since resource variables are just pointers to resources, they must be the easiest thing to pass to functions, like many other languages. it is strange that PHP doesn't allow them to be declared. I wonder if there is a reason or a workaround.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users