Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7562913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:31:50+00:00 2026-05-30T13:31:50+00:00

i am querying the DB as follows: $result=mysql_query(SELECT name,items FROM mytable WHERE user_id=’$id’); now,i

  • 0

i am querying the DB as follows:

$result=mysql_query("SELECT name,items FROM mytable WHERE user_id='$id'");

now,i want to create an array to insert the values that are as a result of this query e’g let’s say this is the resultant data:

name    quantity
john    10
joel    20
brian   10
  1. how do I create an array to insert the above data using $name and $quantity as variables;
  2. secondly,(this is a trading platform),so let’s say a user wanted to buy 25 items from the data in the array,and so to achieve this the script has to take the 10 items from john and 15 from joel(that adds up to 25) and then set their items to the remaining value i.e john’s items=0 and joel’s items=5.how do I code this.please assist. i’ve tried to simplify this the best i can .thanks
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:31:52+00:00Added an answer on May 30, 2026 at 1:31 pm

    mysql_fetch_assoc is your friend:

    // Better turn on error_reporting
    error_reporting(E_ALL); 
    ini_set("display_errors", 1); 
    
    // sp@20120227 - added the ')' at the end of the condition in the where part
    $cursor = mysql_query( 'SELECT name,items '
                         . '  FROM mutable '
                         . ' WHERE ( user_id = "' . mysql_real_escape_string( $id ) . '" ) ' 
                         );
    
    $foundItems = array();
    
    // likely to be a parameter of a function...
    $totalUnitsOrdered = 25;
    
    // maps user to amount assigned from him
    $assignedQuantityPerUser = array();
    
    
    while ( $row = mysql_fetch_assoc( $cursor ) ) {
    
      // Still order Quantity left?
      if ( 0 < $totalUnitsOrdered ) {
    
        if ( $row[ "items" ] <= $totalUnitsOrdered ) {
    
           // assign all of $row[ "items" ]
           $totalUnitsOrdered                         -= 0 + $row[ "items" ];
           $assignedQuantityPerUser[ $row[ "name" ] ] += 0 + $row[ "items" ];
    
        }  else {
    
           // assign all the rest: $totalUnitsOrdered
           $totalUnitsOrdered                          = 0;
           $assignedQuantityPerUser[ $row[ "name" ] ] += $totalUnitsOrdered;
    
        }
    
      }
    
    
       $newItem[] = $row[ "name" ];
       $newItem[] = $row[ "items" ];
    
       // Append $newItem to the end of $foundItems 
       $foundItems[] = $newItem;
    
    } // while
    

    Important: Never pass user-input data directly into queries. Always espace using mysql_real_escape_string()!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am querying the DB as follows: $result=mysql_query(SELECT name,items FROM mytable WHERE price='$price'); now,i
I am querying my mysql-db using PDO, with a plain SELECT * FROM Invoices
i'm querying a database like this: SELECT * from Log WHERE cookieId IN (select
I have a piece of code as follows. Select * from abc_table where objectsnames=obj1
I'm querying an MFC implemented COM object that is implemented as follows: class CA
My nested set table is as follows. create table depts ( id int identity(0,
I need to create a configuration file from a data file that looks as
I'm trying to get the following query to work in Hibernate: SELECT m FROM
Does anyone have any idea how to construct a select statement querying a temp
I am querying Yahoo Finance for stock quotes, the format is as follows: [website]?s=[stock

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.