vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Help with mysql php update (https://vborg.vbsupport.ru/showthread.php?t=68883)

Tekton 08-30-2004 06:23 PM

Help with mysql php update
 
Hiya. Basically what I'm trying to do now is grab a

$upstck = $DB_site->query("SELECT stock FROM user");

What is being grabbed is a string and I need to split it, grab the first two chars, change the next two to 00, and then grab the rest, combine it and put it back :P

So for '12345678', I'd need to grab '12' as the first, '34' and change it to '00' and then grab whatever is left over and throw it all back together and get '12005678'

I'm totally lost or baffled by what I'm trying to do atm. This is for like 40,000 things, not just one btw. :P :ermm:

Colin F 08-30-2004 06:35 PM

OK, so you'll have to run through all those 40'000 things with a while() clause:

while($stock = $DB_site->fetch_array($upstck))
{


Then, parse the string:
fetch the first two chars:

$firstpart = substr($stock[stock],0,2);

fetch the last part

$lastpart = substr($stock[stock],4);

and put it all together:

$wholething = $firstpart . "00" . $lastpart;

all together that makes this:

PHP Code:

$upstck $DB_site->query("SELECT stock FROM user");
while(
$stock $DB_site->fetch_array($upstck))
{
$firstpart substr($stock[stock],0,2);
$lastpart substr($stock[stock],4);
$wholething $firstpart "00" $lastpart;
//here's the place



now I don't know what you want to do with the data, but do whatever you want where I placed the comment.

Tekton 08-31-2004 12:14 AM

Then would something like this put them all back in their right spots?

$DB_site->query("UPDATE user SET stock = '$wholething'");

That'd get them all back where they are supposed to be? =/

or perhaps I'd need to take another field like userid to make sure they all match back up?

Tekton 08-31-2004 03:41 AM

Ok, I'm pretty sure what I just said is bad. So how do I set it correctly? :P

Colin F 08-31-2004 03:44 AM

exactly, you need another field (like userid) to only update the one you changed (and not all of them).

$DB_site->query("UPDATE user SET stock = '$wholething' WHERE userid = $stock[userid]");

you'll also have to fetch the userid first, making it all together:

PHP Code:

$upstck $DB_site->query("SELECT userid,stock FROM user"); 
while(
$stock $DB_site->fetch_array($upstck)) 

$firstpart substr($stock[stock],0,2); 
$lastpart substr($stock[stock],4); 
$wholething $firstpart "00" $lastpart
$DB_site->query("UPDATE user SET stock = '$wholething' WHERE userid = $stock[userid]");



Tekton 08-31-2004 04:46 AM

Ah, worked. You're my php/mysql hero Colin! :P

Colin F 08-31-2004 05:09 AM

Glad to have helped :)


All times are GMT. The time now is 03:35 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01151 seconds
  • Memory Usage 1,730KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (7)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete