Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP Function - mysql_query

  • 05-10-2007 10:16am
    #1
    Registered Users, Registered Users 2 Posts: 3,428 ✭✭✭


    Hi Lads,
    I'm trying to shorten my code by using a function to query my database.
    I'm getting this error:

    Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in . . . .

    But the query itself seems fine? Is there something about functions and mysql queries?

    Thanks
    Gary


Comments

  • Registered Users, Registered Users 2 Posts: 673 ✭✭✭Bananna man


    Can you post whats in the mysql_query() ?


  • Registered Users, Registered Users 2 Posts: 3,428 ✭✭✭randombar


    $funareanum="countyid";
    $funareaid="6";
    
    $query_Recordset = "SELECT * FROM pub WHERE $funareanum=$funareaid AND pubname LIKE 'A%' ORDER BY pubname ASC";
    
    $Recordset = mysql_query($query_Recordset, $connRate) or die(mysql_error());
    


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    When is the $connRate variable being initialised?

    This variable should be pointing to a valid open MySQL connection. There are three main reasons why this resource wouldn't be valid:

    1. The resource was never initialised
    2. The mysql_connect() command failed due to incorrect parameters
    3. The link resource was closed before it was called.


  • Registered Users, Registered Users 2 Posts: 673 ✭✭✭Bananna man


    *edited. Never mind my babble, Seamus knows what he's talking about.


  • Registered Users, Registered Users 2 Posts: 3,428 ✭✭✭randombar


    Hi Lads,
    Thanks for your help.

    I always have this at the top of the page:

    <?php require_once('../Connections/connRate.php'); ?>

    which is initialized then. The mysql command should really be ok because it works for all the other queries outside the function?

    Closing the link resource? Not sure what you mean by this? How do you close them?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    For the sake of good cleanup, you can use the function mysql_close() to close any open mysql connections.

    You mention the word "function" :)

    If the above lines of code are contained within a function, then variable scope comes into play. You need to view a function as a self-contained piece of code, which doesn't know that any of the rest of the script exists. So even though you've initialised $connRate, the function doesn't inherently know that this variable exists.

    There are two ways of getting the function to recognise this variable as a MySQL link.

    1. Pass the link as a parameter to the function, e.g. "function myfunction($parameter1, $connRate)"

    2. Use the global keyword, e.g.:
    function myfunction($parameter1) {
        global $connRate;
    
        /// Rest of function here
    
    }
    

    I would always say that the former is preferable when the function is one that can be called by any script (i.e. in an includes file full of other functions), whereas the latter can be used when the function is contained within an existing script - i.e. when you know that $connRate will always exist.


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    Just to expand on my above post without confusing my answer, and at the risk of being corrected by gurus....

    A variable's scope is the areas of your script from where your variable can be accessed.

    A variable with a "global" scope, is a variable which is available to be used throughout the script. It is either initialised outside of a function, or within a function using the "global" keyword.

    A variable with a "local" scope is a variable which is only valid within a function. Once the function ends, the variable is no longer available.

    You can have global and local variables within the same script which have the same name.

    A quick example:
    [php]<?php

    $myVar = 3; //This is a global variable

    echo $myVar; //Outputs "3";

    function myFunction() {
    $myVar = "5"; //This is a local variable
    echo $myVar;
    }

    myFunction(); //Outputs "5"

    echo $myVar; //Outputs "3"

    ?>[/php]

    You can of course access and change global variables from within functions. Here's the same script using the global keyword.

    [php]<?php

    $myVar = 3; //This is a global variable

    echo $myVar; //Outputs "3";

    function myFunction() {
    global $myVar;

    $myVar = "5";
    echo $myVar;
    }

    myFunction(); //Outputs "5"

    echo $myVar; //Outputs "5"

    ?>[/php]

    (I'm bored) :)


  • Registered Users, Registered Users 2 Posts: 3,428 ✭✭✭randombar


    In fact ah nice one! Brilliant stuff

    Just reduced the sixe of my file by 80%

    Thanks again!


Advertisement