SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast jcharnley's Avatar
    Join Date
    Jan 2008
    Location
    DK 4600
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple function error.

    What is wrong with this simple thing:

    Code PHP:
     
    function GetAuthorNameFromID() {
     
    	$author_query_name = "SELECT first_name FROM cms_users WHERE user_id = 2";
    	$author_name = mysql_query($author_query_name, $con) or die(mysql_error());
     
    	return $author_name;
     
    }

    I keep getting :

    Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /opt/lampp/htdocs/web/rss/index.php on line 33

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jcharnley View Post
    What is wrong with this simple thing:

    Code PHP:
     
    function GetAuthorNameFromID() {
     
    	$author_query_name = "SELECT first_name FROM cms_users WHERE user_id = 2";
    	$author_name = mysql_query($author_query_name, $con) or die(mysql_error());
           $row = mysql_fetch_array($author_name);
    	return $row['author_name'];
     
    }

    Maybe that? I'm a noob though, so...

    "Do or do not. There is no try." -- Yoda

  3. #3
    SitePoint Evangelist sysice's Avatar
    Join Date
    Oct 2004
    Location
    Hackettstown, NJ
    Posts
    418
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $con is not a local variable. Add global $con to the top of the fucntion.

    PHP Code:
    function GetAuthorNameFromID() {
        global 
    $con;

        
    $author_query_name "SELECT first_name FROM cms_users WHERE user_id = 2";
        
    $author_name mysql_query($author_query_name$con) or die(mysql_error());
     
        return 
    $author_name

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There $con variable does not exist.

    Variables don't exist inside of a function automatically. You need to define them, or in some other way pass or import thier values in. Variables have a "scope", which means they only exist in certain contexts.

    http://www.php.net/manual/en/languag...bles.scope.php

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    if GetAuthorNameFromID() is part of a larger class, containing other functions (methods) like this one, they you might try and change $con to:

    $this->con


Bookmarks

Posting Permissions

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