Jump to content

[SOLVED] Repeat results


confused_aswell

Recommended Posts

Hi

 

I have got this problem where when I query 1 table the results come out fine, but when I try to query 2 tables that's when the problem starts. I have seven rows in each table, and when I try to query them both I get First Name, Last Name, Email Address in the top line of my excel file, then I get 7 results of the name Phillip below First Name then 7 results of the name Bloggs below Last Name and each email address for each entry in the second table next to the name Phillip Bloggs. I hope that makes sence?

 

Anyway, would someone mind taking a look at my script for obvious errors please.

 

Thanks,

 

Phil

 

Here is the script -

 

<?php
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db = 'database';
$table = 'zen_address_book,zen_customers';
$file = 'export';

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

$replace = array( 
   'entry_firstname' => 'First Name', 
   'entry_lastname' => 'Last Name',
   'customers_email_address' => 'Email Address'
); 

$values = mysql_query("SELECT entry_firstname, entry_lastname, customers_email_address FROM ".$table."");
$i=0;
while ($rowr = mysql_fetch_assoc($values)) {
if($i==0) {
foreach(array_keys($rowr) as $title)
$csv_output .= '"'.str_replace(array_keys($replace), $replace, $title).'",';
$csv_output .= "\n";	
}

foreach ($rowr as $key => $value) {
$csv_output .= '"'.$value.'",'; 

}
$csv_output .= "\n";
$i++;
}

$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;

?>

Link to comment
https://forums.phpfreaks.com/topic/125512-solved-repeat-results/
Share on other sites

try replacing

 

if($i==0) {
foreach(array_keys($rowr) as $title)
$csv_output .= '"'.str_replace(array_keys($replace), $replace, $title).'",';
$csv_output .= "\n";	
}

 

with

 

foreach(array_keys($rowr) as $title) {
$csv_output .= '"'.str_replace(array_keys($replace), $replace, $title).'",';
$csv_output .= "\n";	
}

No, he's getting repeating entries. This happens when you try to combine results from multiple tables in a single query without JOINing them properly.

 

PS I haven't ignored you, I am just following up the easiest answer first.

 

Understandable, but to be honest, you're just beating around the bush. Test your query in phpMyAdmin, you'll see that it's not a PHP issue.

Hi

 

I have got two tables. The first one has got entry_firstname,entry_lastname,entry_address,entry_suburb,entry_city,entry_postcode and the second table has got customers_email_address,customers_telephone.

 

So I obviously want to join them and have the results print out in the excel file.

 

It will show the results from both tables but I have got this repeating problem in the url above, so, is there anything you can help with please.

 

Thanks,

 

Phil

Hi

 

I am sure I responded to this already, but it seems to have disappeared!

 

Anyway I have two tables in the database the first one stores the names and addresses and the other stores the email and telephone numbers. I have found a sql query that works in Dreamweaver but because the code in the script requires FROM ".$table.""); I can't use the query. Here is the query -

 

SELECT zen_address_book.entry_firstname, zen_address_book.entry_lastname, zen_address_book.entry_street_address, zen_address_book.entry_suburb, zen_address_book.entry_city, zen_address_book.entry_postcode, zen_customers.customers_email_address FROM zen_address_book, zen_customers WHERE zen_customers.customers_id = zen_address_book.customers_id

 

Unless we can rewrite the script some how, any ideas?

 

Thanks,

 

Phil

Give this a shot

 

SELECT
a.entry_firstname, a.entry_lastname, a.entry_street_address, a.entry_suburb,
a.entry_city, a.entry_postcode, c.customers_email_address
FROM
zen_address_book as a
LEFT JOIN
zen_customers as c
ON
c.customers_id = a.customers_id

Hi

 

I am not sure what you mean, but all I am saying is because of this line

$table = 'zen_address_book,zen_customers';

then we need to change the line

FROM ".$table."");

in the query. Because the

FROM ".$table."");

is calling from the tables in

$table = 'zen_address_book,zen_customers';

Does that make sense or I am barking up the wrong tree.

 

Thanks,

 

Phil

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.