Page 1 of 1

Function and mysql_query

Posted: Mon Oct 17, 2005 5:14 am
by evilclone
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi everyone, I got a problem while using mysql_query inside a function I have made. Simply I connect to mysql outside the function, I select the db (inside the function) and finally I use the mysql_query function but it fails without any reason. If I write all the entire code outside my function, it works!
The main code (an example):

Code: Select all

<?php
  require_once('path of the php files that manage the connection'); 

  function deleteReport($userid)
 {		
    mysql_select_db($database_dblinker, $dblinker);
    $query_report = "SELECT * FROM report WHERE userid = $userid";
    $rs_report = mysql_query($query_report, $dblinker) or die(mysql_error());
    $reportArray = mysql_fetch_assoc($rs_report);
    mysql_free_result($rs_report);
    return $reportArray;
  }
?>
HTML parts

Code: Select all

<?php
  $result = deleteReport(50);
?>

After echoing a sentence after every istructions, I found that the problem was the mysql_query function, but I can't understand why it doesn't work inside my function, but it's ok outside.


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Mon Oct 17, 2005 7:21 am
by feyd

Posted: Mon Oct 17, 2005 9:24 am
by evilclone
I see, I didn't know I have to declare the variable as global inside my function to be visible.
Thanks