1

I'm a fairly novice coder and have an issue I'm hoping someone could help me with.

I am following a series of tutorials and I have had moderate success in being able to follow along and even more intriguing is that the site connects to the DB and displays the contents of the DB where requested but when I try to execute a function I'm working on I get an access denied using password NO message.

here's how my function looks

function Member_Data($user_id) {
    $data = array();
    $user_id = (int)$user_id;

    $func_num_args = func_num_args();
    $func_get_args = func_get_args();

    if($func_num_args > 1) {
        unset($func_get_args[0]);

        $fields = '`' . implode('`, `', $func_get_args) . '`';
        $data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `members` WHERE `ID` = $user_id"));
        echo mysql_error();

        print_r($data);
        //die();
        //return $data;
    }
}

and in head.php before the tag I have

if(loggedin() === true) {
    $Session_memberID = $_SESSION['memberID'];
    $MemberData = Member_Data($Session_memberID, 'ID', 'FName', 'LName', 'Email', 'Password');
    }

But when I try to retrieve the data (ie: print_r($data);) I get a blank screen but when I add in echo mysql_error(); I get: Access denied for user 'obscuredforsecurity'@'localhost' (using password: NO)

But all the other pages connect fine as well as I was not having a problem connecting for other tutorials but for some reason (unbeknownst to me) I can't get it to connect so I can follow through with this tutorial.

If anyone wouldn't mind sharing with me what I'm doing wrong, I'd be most appreciative and I thank you all once again.

Stuart K

  • What is unclear is where your database connection code is relative to this function call. – Mike Brant Apr 16 '13 at 15:45
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Apr 16 '13 at 15:48
  • mysql functions are still the dominant SQL api.. they're not deprecating them until PHP6 and they HAVENT EVEN STARTED WORKING ON PHP6.. you people need to stop parroting each other just so you can try to sound smart. there's nothing wrong with using mysql at this point – I wrestled a bear once. Apr 16 '13 at 15:51
  • @Adelphia - many poeple follow old or bad tutorials. In this case mysql_* function are unsafe to use. That is why poeple try to steer users of such functions to a better approach with mysqli or PDO. With that said. It's also better to have a hard time upfront then having to unlearn bad practices later. – Bart Apr 16 '13 at 16:05
  • @Bart i agree with the last line of your comment. but i challenge you to give one single good reason why mysql is a bad choice for this particular script. everyone wants to say "don't do that" but no one can say why. – I wrestled a bear once. Apr 16 '13 at 16:22
  • 1
    @Adelphia - 'This particular script' will be the foundation for future scripts hence my previous statement. See here for the why no mysql_* http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php. – Bart Apr 16 '13 at 16:35
  • @Bart ..Thank you, there was some good info in that link, but you pretty much proved my point. I asked if YOU could provide an answer and you couldn't, instead you referred me to the responses other people have given to a similar question. I am well aware of the differences and advantages but my point stands. As a beginner, the only vice in using mysql_* is that it will not exist in PHP6. Look at python. Version 3 was released quite some time ago, but they don't even teach 3 in schools. I'm taking a class at MIT and they still recommend and teach version 2.7 – I wrestled a bear once. Apr 16 '13 at 16:47
  • 1
    @Adelphia - You ask me to provide so I did. I get your point about deprecated code and it's true. If you don't upgrade you can still use it for a long time. But it's not a good practice to start learning to code with officially deprecated functions. But the biggest point is that mysql_* doesn't support prepared statements. That's a huge security risk. – Bart Apr 16 '13 at 17:00

1 Answers1

1

As Adelphia stated, it would help to see how you connect to the database, as the error you indicate is a connection-related error.

Also, be careful about side-effects outside of this function, perhaps the side-effect of accidentally closing the database connection before this function is called (don't laugh, it happens more times than you'd think; we developers do occasionally make those kinds of silly mistakes!)

Put a echo of mysql_error() BEFORE you make the query - I wouldn't be surprised if you find the database connection was not available before the call was made. If that's the case, find out how the connection was closed. (Hint: search the whole codebase for mysql_close())

Side note: mysql_ functions are deprecated, the advice to start migrating away from them is sound. Look at the mysqli_ functions - they are close to mysql_ in many ways and easy to learn :)

  • I appreciate everyone's help and opinions about moving up to PDO or MySQLi but I wanted to add: The reason I'm still trying to learn the mysql_* basics is because I am novice and (here's the punch line) I'm working with a SLOPPY'ly coded classifieds script that I purchased a while back and I do not have time right now to learn a new language. (It was a very short time ago that I couldNT tell you the difference between echo and $variable) –  Apr 16 '13 at 19:57
  • @bart In reference to: "It's also better to have a hard time upfront then having to unlearn bad practices later." Yes... you're right but like I stated, I'm working with a classifieds script that is chock full of this depreceeated code but until I can understand what I'm looking at, It doesn't seem the time to chase another language. You gotta walk before you run. –  Apr 16 '13 at 20:00
  • and I'm a true example of "having a hard time upfront"... I've been doing that for MONTHS lol –  Apr 16 '13 at 20:01
  • Makes sense. Sometimes, we tend to 'conveniently forget' that there are other external constraints that are unstated. :) Which script did you purchase? Perhaps that will help in understanding your problem, or maybe there's someone on SO who has run up against the same problem with that script. – J Marshall Presnell Apr 17 '13 at 18:10