MyBB Community Forums

Full Version: MyCMS
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hey guys I just have a quick question. I'm currently building a CMS for my site that works with the mybb database. I'm using the global.php to connect to the database and I have the most recent version of mybb. I just recently got back to developing the CMS after taking a little time off and noticed that a few things have changed. I noticed mybb uses $db->query and $db->fetch_array and think it may have something to do with my problem. I'm trying to use the mysql_insert_id() to obtain an ID of something but it doesn't seem to be showing up. Does anyone know off the top of their head if mybb is causing this because its changed from using just a regular query to the $db->query.

I had it working with an older version of MyBB so the only thing I could think of is that MyBB has changed so much that the function doesn't pick up the query?!?! I dunno, I'm clueless and hope you guys can help!! Its very later so excuse me if I have mistakes in my spelling or I'm not making sense. lol
Sample:

$query = $db->insert_query("users", array('field' =>'foobar'));
$id = $db->insert_id();

Does that help?

You should look at the new database functions...they are great.

$db->fetch_field
$db->num_rows
$db->fetch_array
$db->write_query
$db->update_query
$db->simple_select
$db->delete_query

You really shouldn't use $db->query unless it's like a join. This will help your code be compatible with non-mysql databases. It writes quicker too.
(2009-06-18, 08:53 AM)labrocca Wrote: [ -> ]$query = $db->insert_query("users", array('field' =>'foobar'));
$id = $db->insert_id();

To make it easier, $query will actually be set as the inserted id (so really you can do $id = $query). There is no need for the $db->insert_id();

OCD is a pain, sometimes. Toungue
Okay so i'm starting to use all those functions now that I've found out about them. What I want to know is what I'm doing wrong.
$db->insert_query("articles", array('id' =>'', 'head' =>'$head', 'body' =>'$body'));
the above is just an example. what I'm trying to do is use a variable within the array. What am I doing wrong?
$db->insert_query("articles", array('id' => '', 'head' => $head, 'body' => $body));

I think that's the right way, anyway...
I dont suppose any of you know here I can find more information about the database functions? Sorry if I come off new Sad Just trying to learn the ways. Smile Also, I can't seem to get it to add a row within a table I created using the database functions.
$db->insert_query("articles", array('id' => '', 'head' => $head, 'body' => $body, 'author' => $author, 'timestamp' => $timestamp));
That's what I'm using. The variables do carry a string but none of them get carried to the database. Now before I remove the quote from the variables it was actually adding the variables as the actually string. How come the variables get added when it quotes, but nothing gets added when just the variable is used? am I doing something wrong?
First, what's the name of the table you created? The MyBB db functions will use the TABLE_PREFIX, so in the above, it's actually loading into mybb_articles (where mybb_ is the table prefix). You can find more db functions in [Wiki: Database_Methods] (Broken link, head over to docs.mybb.com instead).

When you say it isn't carried into the database, does it give an error?
Tomm...long as we are talking about queries. I have a question on one I can't get to work right.

$db->update_query("users", array('postnum' => postnum+1), "uid='1'");

That's just an example. I can get it to work fine as regular query.

$db->query("UPDATE mybb_users set postnum=postnum+1 WHERE uid='1'");

Yet in the update_query() I just can't get it to work despite numerous attempts.

Even inside the datahandler/posts.php they use write_query() function.

Any idea?
Yeah I've noticed this one too when I've been working on the tracker. I had to use update_query in it, so I did a little workaround:

$new_issues = $project['num_issues'] + 1; // We're adding a new issue so increase by one
$update_array = array(
	"num_issues" => $new_issues,
	[...]
);
$db->update_query("tracker_projects", $update_array, "proid = '".$insert_array1['projid']."'");

Basically - set the variable outside the array, then (for some reason) it works OK. I have no idea why setting it in the array doesn't work, I'm assuming it must be some sort of PHP limitation...
Well at least I know I'm not the only one having this problem. lol I'll try your workaround , Thanks!

EDIT:
Ok, so I went back to my code and realized that all the variables ARE set RIGHT before the sql statement. The variables are taking form data from another page (same php file) and added them to the database. I'm not updating my row I'm adding it, it doesn't look like the workaround works for that. Sad

I can always go back and use $db->query as that was working fine. It'd just be nice to use the database functions since someone went through the trouble of creating them.

EDIT AGAIN:
LOL, I just noticed you were answering someone else Confused Can anyone help me with my problem? Sad
Pages: 1 2