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

  • Home
  • SEARCH
  • 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 1006981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:32:53+00:00 2026-05-16T08:32:53+00:00

I have an array, $scans. I want to query MySQL with all the values

  • 0

I have an array, $scans. I want to query MySQL with all the values in that array and get my results back in an array. For example, sample data in scans would be:

E1234
E2244
E3654

The MYSQL table PARTS has fields part, size, length, plate, side, type.

I want to end up with $output["E1234"][0] to be the size result for that part, 1 to be length, etc. and I want the array to be sortable by the MYSQL query. (order by SIDE desc, PLATE asc).

Right now, i’m just stepping through the $SCANS array and doing query after query, but I’m not able to then sort all the results properly.

Is this possible? This is what I’m doing now, but obviously since each query returns one row which is then outputted, there’s no sortability. I want to be able to perform one query, sort the results within the array, and then output that data after the sort.

    foreach ($scans as $currentscan) {
        $partselect = substr(mysql_real_escape_string($currentscan), 0, 5);

        $query = "SELECT french, length, plate, side, type FROM parts WHERE part = '$partselect' ORDER BY side DESC, plate ASC LIMIT 1";
        $result = mysql_query($query);
        #echo 'query is '.$query.'   <br>';
        $error = mysql_error();
        $num_rows = mysql_num_rows($result);
        if ($num_rows == 0) {
            echo 'BAD PART: '.$currentscan.' '.$partselect.' error is '.$error.'<br \>
            ';
        } else {
            $row = mysql_fetch_array($result);
            print $partselect.' is a '.$row['french'].'/'.$row['length'].' - '.$row['type'].' - '.$row['plate'].'('.$row['side'].')<br \>';
        }
    };

EDIT: This is the code as it is now following some suggestions here:

$scans = explode("\n",$_POST['scans']);

foreach ($scans as $currentscan) {
   if ($currentscan[0] == "E") { //this is a cheap trick to ignore a bad scan inherent in the scanning mechanism
   $partselect = substr(mysql_real_escape_string($currentscan), 0, 5);
   $tempQuery .= 'part = "'.$partselect.'" OR ';

   };
};//end foreach 
$tempQuery = substr($tempQuery, 0, -3); //remove last OR (fixed substr to 0,-3 to scrip final OR - stephen) 
$tempQuery .= ") ORDER BY side DESC, plate ASC LIMIT 1"; //add on end of query 
$query = "SELECT french, length, plate, side, type FROM parts WHERE ".$tempQuery;
$result = mysql_query($query);
echo $result;
while($row = mysql_fetch_array($result)){
   print $row['french']." / ".$row['length']; //just doing something pointless to verify data was pulled.
}

result is:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/foo/bar/sort.php on line 35

Line 35 is
while($row = mysql_fetch_array($result)){

FINAL EDIT:

It works.

//DECLARED CONSTANTS//

if (!$_POST) { // Have scans been entered? If not, display the form.
print '
<html>
<body>
Scans:
<form action="index.php" method="post">
<textarea rows="20" cols="6" name="scans" id="scans">
</textarea>
<br />
<input type="submit" value="Submit" />
</body>
</html>
';
} else { //Scans have been entered. Start scan processing logic

//==openDB==//
mysql_connect(SQLSERVER, SQLUSER, SQLPASSWORD) or die("Can not connect to DB server.");
mysql_select_db(DATABASE) or die("Can not connect to Database.");
//==openDB==//

$scans = explode("\n",$_POST['scans']); // Explode posted scans into scans array

foreach ($scans as $currentscan) { // step through each scan
   if ($currentscan[0] == "E") { //Cheap check for real part numbers.
   $partselect = substr(mysql_real_escape_string($currentscan), 0, 5); // Strip off the extraneous data from the scan
   $count{$partselect}++; //Count instances of particular parts. ideally this would be in an array in the form of $count[$partnumber] so I can easily display that data at the end. each part needs to be displayed every time it's scanned.
   $tempQuery .= 'part = "'.$partselect.'" OR '; //Starts building query

   };
};//end foreach 
$tempQuery = substr($tempQuery, 0, -3); //remove last OR 

$tempQuery .= ") ORDER BY side DESC, plate ASC"; //add on end of query 
$query = "SELECT part, french, length, plate, side, type FROM parts WHERE (".$tempQuery; //Form beginning of query
$result = mysql_query($query);// execute query
while($row = mysql_fetch_array($result)){ // step through results
   for ($i = 0; $i < $count{$row['part']}; $i++) { //if a part was scanned $count{$row['part']} times, display it that many times
   print $row['part']." ".$row['french']." / ".$row['length']." ".$row['plate']."(".$row['side'].")<br>";  //data parsing goes here. this will be expanded.
   };// close for loop
};//close while loop        
};//close else

?>
  • 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-16T08:32:54+00:00Added an answer on May 16, 2026 at 8:32 am
    <?php
    // Make a MySQL Connection
    $query = "SELECT * FROM example"; 
    
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_array($result) or die(mysql_error());
    echo $row['name']. " - ". $row['age'];
    
    ?>
    

    Courtesy of http://www.tizag.com/mysqlTutorial/mysqlfetcharray.php

    You can also if you expect multiple results do:

    <?php
    // Make a MySQL Connection
    $query = "SELECT * FROM example"; 
    
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
       echo $row['name']. " - ". $row['age'];
    }
    
    
    ?>
    

    Added code for your comment:

    foreach ($scans as $currentscan) {
       $partselect = substr(mysql_real_escape_string($currentscan), 0, 5);
       $tempQuery .= "(part = ".$partselect." OR";
    }//end foreach 
    $tempQuery = substr($tempQuery,-2); //remove last OR 
    $tempQuery .= ") ORDER BY side DESC, plate ASC LIMIT 1"; //add on end of query 
    $query = "SELECT french, length, plate, side, type FROM parts WHERE ".$tempQuery;
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
       //do something with row in result set... 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a structure in my program that contains a particular array. I want
I have an array of data that I'll echo to a page using PHP,
I have array of strings, String[] data and it's 10 elements has value P
I want all of the validators in an ASP.Net 3.5 website to have a
Let's say that I have a bunch of records in MySQL, something like this:
Say I have a string with comma delimited values enclosed in single quotes that
I have a big text file containing data that needs to be extracted and
I want to pass the contents of an array to another method and have
I have code that scans and returns NSString like this: NSString *GetText = [[NSString
I want to scan a 2D array with the help of pointers and have

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.