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
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";	
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.