Function and mysql_query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
evilclone
Forum Newbie
Posts: 5
Joined: Mon Oct 17, 2005 4:56 am

Function and mysql_query

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

evilclone
Forum Newbie
Posts: 5
Joined: Mon Oct 17, 2005 4:56 am

Post by evilclone »

I see, I didn't know I have to declare the variable as global inside my function to be visible.
Thanks
Post Reply