Solved

PHP // MYSQL get multiple rows if any and display them

Posted on 2009-07-13
7
267 Views
Last Modified: 2013-12-13
If a search finds multiple rows I want to display them, I don't know if the while loop needs to be in my query function, or if I need to use /mysql_fetch_assoc()/

//this works and pulls in 1 record
function get_order_info($search_value, $search_field, $table){
	global $mysqlconn;
	$query_error="Data Not Found";
	// building query 
	$query="SELECT * ";
		if($table=="usps"){
			$query .="FROM ". USPS_TABLE ;
		}
		else{
			$query .="FROM ". UPS_TABLE ;
		}
	$query .= " WHERE ". $search_field;
	$query .= " = ". $search_value;
	$query .= " LIMIT 0, 10";
	
	$query_result_set = mysql_query($query, $mysqlconn)
	
	//this test to see if there is actual data will reurn false if the query cannot find anything
	if($order_info= mysql_fetch_array($query_result_set)){
		return $order_info;
	}else{
		return $query_error;
	}
}

Open in new window

0
Comment
Question by:frtools
  • 3
  • 2
7 Comments
 
LVL 1

Accepted Solution

by:
tehpnkprdgy earned 250 total points
ID: 24846028
You were close, just need a while loop.
<?php
function get_order_info($search_value, $search_field, $table){
        global $mysqlconn;
        $query_error="Data Not Found";
        // building query 
        $query="SELECT * ";
                if($table=="usps"){
                        $query .="FROM ". USPS_TABLE ;
                }
                else{
                        $query .="FROM ". UPS_TABLE ;
                }
        $query .= " WHERE ". $search_field;
        $query .= " = ". $search_value;
        $query .= " LIMIT 0, 10";
        
        $query_result_set = mysql_query($query, $mysqlconn)
        
        
        while ($row = mysql_fetch_array($query_result_set))
{
       //Do what every you want to do with your data here, it will loop
       //through the results row by row
}
 
    return $yourResults;
?>

Open in new window

0
 

Author Comment

by:frtools
ID: 24846191
So now I would return $row and out of the while loop return the query_error()
0
 

Author Comment

by:frtools
ID: 24846483
ok here is the code and i want to display multiple rows if there is any. My form brings in a search string, my main <?php> block checks for the post variable;
//This is main .php page calls functions
 
<?php
$table="usps";
 
//Form validation check for search 
if(isset($_POST['form_zipcode'])){
 while($sel_order=search_for($_POST['form_zipcode'], $table_field,$table)){
    echo output_track_info($sel_order);
  }
}else{
  echo "Enter a search above.";
}
?>
 
 
 
//functions.php
 
 
function search_for($form_result, $table_field, $table){
    $end="END OF SEARCH";
    //Call functions to get array data, and clickable link data 
    while($result= get_order_info($form_result, $table_field ,$table)){
	return $result;
    }
    return $end;
}
 
 
 
function get_order_info($search_value, $search_field, $table){
 global $mysqlconn;
 $end="END OF SEARCH";
 $query_error="Data Not Found";
	
 // building query 
 $query="SELECT * ";
  if($table == "usps"){
   $query .="FROM ". USPS_TABLE ;
  }
  else{
   $query .="FROM ". UPS_TABLE ;
  }
  $query .= " WHERE ". $search_field;
  $query .= " = ". $search_value;
  $query .= " LIMIT 5, 10";
 
  $query_result_set = mysql_query($query, $mysqlconn);
  //this test to see if there is actual data
  if(!$query_result_set){
	die("Failed to find search criteria ". mysql_error());
  }else{
       while ($row = mysql_fetch_array($query_result_set)){
	return $row;
       }
  }
	return $end;
}
 
 
				

Open in new window

0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24847033
You can also return the result as a global array if you need to use the recordset later.

Is there a particular field you are interested in extracting?

//This is main .php page calls functions
 
<?php
$table="usps";
 
//Form validation check for search 
if(isset($_POST['form_zipcode'])){
 while($sel_order=search_for($_POST['form_zipcode'], $table_field,$table)){
    echo output_track_info($sel_order);
  }
}else{
  echo "Enter a search above.";
}
 
?>
 
 
 
//functions.php
 
global $data;
 
function search_for($form_result, $table_field, $table){
    $end="END OF SEARCH";
    //Call functions to get array data, and clickable link data 
    while($result= get_order_info($form_result, $table_field ,$table)){
        return $result;
    }
    return $end;
}
 
 
 
function get_order_info($search_value, $search_field, $table){
 global $mysqlconn;
 $end="END OF SEARCH";
 $query_error="Data Not Found";
        
 // building query 
 $query="SELECT * ";
  if($table == "usps"){
   $query .="FROM ". USPS_TABLE ;
  }
  else{
   $query .="FROM ". UPS_TABLE ;
  }
  $query .= " WHERE ". $search_field;
  $query .= " = ". $search_value;
  $query .= " LIMIT 5, 10";
 
  $query_result_set = mysql_query($query, $mysqlconn);
  //this test to see if there is actual data
  if(!$query_result_set){
        die("Failed to find search criteria ". mysql_error());
  }else{
       $x=0;
       while ($row = mysql_fetch_array($query_result_set)){
	foreach ($row as $key => $value){
         $data[$x][$key]=$value
        }
	$x++;
       }
   return $data;
  }
}
                             

Open in new window

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24847039
Typo fixed.

Again is there a certain field you need for the tracking function?

if so instead of the forloop you could do:
$data[$x] = $row['{field_name_here}'];

//This is main .php page calls functions
 
<?php
$table="usps";
 
//Form validation check for search 
if(isset($_POST['form_zipcode'])){
 while($sel_order=search_for($_POST['form_zipcode'], $table_field,$table)){
    echo output_track_info($sel_order);
  }
}else{
  echo "Enter a search above.";
}
 
?>
 
 
 
//functions.php
 
global $data;
 
function search_for($form_result, $table_field, $table){
    $end="END OF SEARCH";
    //Call functions to get array data, and clickable link data 
    while($result= get_order_info($form_result, $table_field ,$table)){
        return $result;
    }
    return $end;
}
 
 
 
function get_order_info($search_value, $search_field, $table){
 global $mysqlconn;
 $end="END OF SEARCH";
 $query_error="Data Not Found";
        
 // building query 
 $query="SELECT * ";
  if($table == "usps"){
   $query .="FROM ". USPS_TABLE ;
  }
  else{
   $query .="FROM ". UPS_TABLE ;
  }
  $query .= " WHERE ". $search_field;
  $query .= " = ". $search_value;
  $query .= " LIMIT 5, 10";
 
  $query_result_set = mysql_query($query, $mysqlconn);
  //this test to see if there is actual data
  if(!$query_result_set){
        die("Failed to find search criteria ". mysql_error());
  }else{
       $x=0;
       while ($row = mysql_fetch_array($query_result_set)){
	foreach ($row as $key => $value){
         $data[$x][$key]=$value;
        }
	$x++;
       }
   return $data;
  }
}
                                

Open in new window

0
 

Author Comment

by:frtools
ID: 24854356
Figured it out I need to echo inside the while loop not return;
Thanx everyone
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Incorrect definition of table mysql.proc 7 46
What is wrong with this PDO Delete Query? 2 17
PHP Curl to output a url 7 46
Wordpress Only run code if on a certain page 11 22
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question