Can't Connect Within Function?!

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Can't Connect Within Function?!

    If I try to connect to a database within a function, I get the
    following error. Why is this?


    MySQL Connect Error : Can't connect to local MySQL server through
    socket '/Private/tmp/mysql.sock' (2)

    function getUserPermissions() {
    if ($_SESSION['permissions'] == '') {
    // Connecting, selecting database
    $link = mysql_connect($shs_MySQLServer, $shs_MySQLUser,
    $shs_MySQLPassword)
    or die("MySQL Connect Error : " . mysql_error());

    etc....
    }
    }


    Seth Willits
    ------------------------------------------------------------------------
    ---
    President and Head Developer of Freak Software - [url]http://www.freaksw.com[/url]
    Q&A Columnist for REALbasic Developer Magazine -
    [url]http://www.rbdeveloper.com[/url]
    Webmaster for REALbasic Game Central - [url]http://www.freaksw.com/rbgames[/url]

    "When purchasing that gift for your Special Someone guys remember, girls
    like cute things. If it makes you want to puke, your chick will totally
    love it."
    -- Someone else out There
    ------------------------------------------------------------------------
    ---
    Seth Willits Guest

  2. Similar Questions and Discussions

    1. A function to connect lines (paths) that endpoints are near each other?
      I am pretty sure I read this in the manual somewhere, but is there a function to connect lines (paths) that endpoints are near each other? I have...
    2. [PHP] Can't Connect Within Function?!
      On Monday, September 1, 2003, at 11:33 AM, Mike Morton wrote: Ohh, I didn't even think of that. Thanks. Seth Willits...
    3. note 33988 added to function.mssql-connect
      Using MSSQL 2000 and Windows Server 2003 Enterprise running on a separate machine from the web server, I was unable to get PHP to connect to MSSQL...
    4. note 33678 added to function.mysql-connect
      Çäåñü âû íàéäåòå êó÷ó ïîëåçíîãî ìàòåðèàëà , ïîñåòèòå ãàëåðåþ 3D ðàáîò äðóãèõ äèçàéíåðîâ, ÑÊÀ×ÀÒÜ ÃÎÒÎÂÓÞ ÐÀÁÎÒÓ èëè çàäàòü ñâîé âîïðîñ íà...
    5. note 33624 added to function.mssql-connect
      Just a little comment that I would like to add: If you intend to access MS SQL server remotely, you need to install SQL Server Client Tools on the...
  3. #2

    Default Re: [PHP] Can't Connect Within Function?!

    You need to read up on the scope of variables within functions.

    You should have something like:

    function
    getUserPermissions($shs_MySQLServer,$shsMySQLUser, $shs_MySQLPassword) {
    if ($_SESSION['permissions'] == '') {
    // Connecting, selecting database
    $link = mysql_connect($shs_MySQLServer, $shs_MySQLUser,
    $shs_MySQLPassword)
    or die("MySQL Connect Error : " . mysql_error());

    etc....
    }
    }

    And call it with:
    GetUserPermissions("server","user","password") where server,user and
    password are the variables that contain those values.


    On 9/1/03 2:20 PM, "Seth Willits" <seth@freaksw.com> wrote:
    > If I try to connect to a database within a function, I get the
    > following error. Why is this?
    >
    >
    > MySQL Connect Error : Can't connect to local MySQL server through
    > socket '/Private/tmp/mysql.sock' (2)
    >
    > function getUserPermissions() {
    > if ($_SESSION['permissions'] == '') {
    > // Connecting, selecting database
    > $link = mysql_connect($shs_MySQLServer, $shs_MySQLUser,
    > $shs_MySQLPassword)
    > or die("MySQL Connect Error : " . mysql_error());
    >
    > etc....
    > }
    > }
    >
    >
    > Seth Willits
    > ------------------------------------------------------------------------
    > ---
    > President and Head Developer of Freak Software - [url]http://www.freaksw.com[/url]
    > Q&A Columnist for REALbasic Developer Magazine -
    > [url]http://www.rbdeveloper.com[/url]
    > Webmaster for REALbasic Game Central - [url]http://www.freaksw.com/rbgames[/url]
    >
    > "When purchasing that gift for your Special Someone guys remember, girls
    > like cute things. If it makes you want to puke, your chick will totally
    > love it."
    > -- Someone else out There
    > ------------------------------------------------------------------------
    > ---
    --
    Cheers

    Mike Morton

    ************************************************** **
    *
    * Tel: 905-465-1263
    * Email: [email]mike@webtraxx.com[/email]
    *
    ************************************************** **

    "Indeed, it would not be an exaggeration to describe the history of the
    computer industry for the past decade as a massive effort to keep up with
    Apple."
    - Byte Magazine

    Given infinite time, 100 monkeys could type out the complete works of
    Shakespeare. Win 98 source code? Eight monkeys, five minutes.
    -- NullGrey
    Mike Morton Guest

  4. #3

    Default Re: [PHP] Can't Connect Within Function?!

    * Thus wrote Seth Willits (seth@freaksw.com):
    > If I try to connect to a database within a function, I get the
    > following error. Why is this?
    >
    >
    > MySQL Connect Error : Can't connect to local MySQL server through
    > socket '/Private/tmp/mysql.sock' (2)
    >
    > function getUserPermissions() {
    > if ($_SESSION['permissions'] == '') {
    > // Connecting, selecting database
    > $link = mysql_connect($shs_MySQLServer, $shs_MySQLUser,
    > $shs_MySQLPassword)
    Youre $shs_ vars are not defined here.


    Curt
    --
    "I used to think I was indecisive, but now I'm not so sure."
    Curt Zirzow Guest

  5. #4

    Default Re: [PHP] Can't Connect Within Function?!

    add this in the 1st line of the body of the function:

    global $shs_MySQLServer;
    global $shs_MySQLUser;
    global $shs_MySQLPassword;

    On Mon, 2003-09-01 at 14:20, Seth Willits wrote:
    > If I try to connect to a database within a function, I get the
    > following error. Why is this?
    >
    >
    > MySQL Connect Error : Can't connect to local MySQL server through
    > socket '/Private/tmp/mysql.sock' (2)
    >
    > function getUserPermissions() {
    > if ($_SESSION['permissions'] == '') {
    > // Connecting, selecting database
    > $link = mysql_connect($shs_MySQLServer, $shs_MySQLUser,
    > $shs_MySQLPassword)
    > or die("MySQL Connect Error : " . mysql_error());
    >
    > etc....
    > }
    > }
    >
    >
    > Seth Willits
    > ------------------------------------------------------------------------
    > ---
    > President and Head Developer of Freak Software - [url]http://www.freaksw.com[/url]
    > Q&A Columnist for REALbasic Developer Magazine -
    > [url]http://www.rbdeveloper.com[/url]
    > Webmaster for REALbasic Game Central - [url]http://www.freaksw.com/rbgames[/url]
    >
    > "When purchasing that gift for your Special Someone guys remember, girls
    > like cute things. If it makes you want to puke, your chick will totally
    > love it."
    > -- Someone else out There
    > ------------------------------------------------------------------------
    > ---
    Dan Anderson Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139