Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Databse Query Function

Featured Replies

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. :p

 

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! :D

Have you tried changing this:

 

echo $result; // I'm almost certain this is the problem

 

to this:

 

return $result;

  • 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 *

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.

  • 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. ;)

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.

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.

  • 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. :p

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.