May 5, 200917 yr Ok, so I'm coding this script, and I need to see if a member is logged in to an IPB forum. I know how to do that and all, but one function I need in order to majorly simplify and shorten the script, is refusing to work. Basically it's a database query, anyway have a look at what I've got and you should be able to figure out what I want to do, and hopefully where I went wrong. function query_ibf($table,$field){ $query = sprintf("SELECT ". $field ." FROM ". $table); $result = mysql_query($query); echo $result; // I'm almost certain this is the problem } I call the function, but nothing happens. Also, don't psyc out at me for trying to echo the result, lol. It was a last resort as I've also tried mysql_fetch_array, mysql_fetch_assoc and mysql_result but they all returned errors. If you're a little confused still, here's how I used it. $ipb_session = query_ibf("sessions","id"); Thanks to anyone who can help!
May 5, 200917 yr Have you tried changing this: echo $result; // I'm almost certain this is the problem to this: return $result;
May 5, 200917 yr Author Doesn't show anything. I've just discovered that mysql_result only fails in a function for some reason. If I use that coding outside a function it works, but if it's in a function I get this error: Warning: mysql_result(): supplied argument is not a valid MySQL result resource in * on line *
May 5, 200917 yr I just remembered that MySQL results are contained within an array. So you need to save the array results to a variable: $row = mysql_fetch_array( $result ); So something like this should work: function query_ibf($table,$field){ $query = sprintf("SELECT ". $field ." FROM ". $table); $result = mysql_query($query); $row = mysql_fetch_array($result); return $row; } Now $ipb_session should contain the array, so you can use something like var_dump($ipb_session); to find out what's inside the variable and write a variable to store what you need from the array.
May 5, 200917 yr Author Well the main point of creating the function was so I didn't have to keep creating extra variables for every query I made, so I think it'll be easier just to use variables like this: $ipb_session = mysql_result(mysql_query(sprintf("SELECT id FROM ". $ipb_prefix ."_sessions WHERE member_id='%s'", mysql_real_escape_string($user_id))),0); Thanks for your help though.
May 5, 200917 yr function query_ibf($table,$field) Have you checked the bit of your code which is identifying these vars? $table & $field Try echoing them to see if they are set correctly.
May 5, 200917 yr But you don't have to create new variables etc. really - $ipb_session contains the results of the query so you can access the array like $ipb_session[1] or $ipb_session['blah'] or whatever is contained in it.
May 5, 200917 yr Author Have you checked the bit of your code which is identifying these vars? $table & $field Try echoing them to see if they are set correctly. Those variables are set when the function is called. eg: function test($test){ echo "testing function". $test ."testing function"; } test("abcd"); would be the same as this: echo "testing function abcd testing function"; But you don't have to create new variables etc. really - $ipb_session contains the results of the query so you can access the array like $ipb_session[1] or $ipb_session['blah'] or whatever is contained in it. Oh I see... I haven't learnt how to do arrays properly yet, lol. It's something I'm going to have to get around to. Anyway, now my script is working well, if anyone wants to know how to detect if you are logged on to an IPB forum externally, let me know, lol.
Create an account or sign in to comment