Jump to content

MySQL search using PHP problem


fja3omega

Recommended Posts

:confused:

This is my code in PHP for searching two tables.

It works but then an error occurs whenever $calls ="SELECT * FROM  `table2` WHERE  `Company` LIKE '$company'"; gets a variable data with an (') inside.  Like Cow's, It's, Isn't or Don't.  I've been trying the search in MySQL phpMyAdmin and it shows that with ' you need %'% to get it to show.  How do i put %$var% in my $calls variable?

 

Heres my code:

 

<?php

$count = 0;

$con = mysql_connect("localhost","username","password");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

mysql_select_db("Database", $con);

$result = mysql_query("SELECT * FROM table1");

while($row = mysql_fetch_array($result))

{

$count++;

$company = $row['Company'];

$calls ="SELECT * FROM  `table2` WHERE  `Company` LIKE '$company'";

$result2 = mysql_query($calls);

while($row2 = mysql_fetch_array($result2))

{

$email = $row2['Email Address'];

$cntper = $row2['Contact Person'];

}

echo "<br />--------------------------------------------------------------------------------------<br />Company: ".$company."<br />Email Address: ".$email."<br />Contact Person: ".$cntper."<br />Count: ".$count;

ob_flush();

flush();

}

mysql_close($con);

?>

 

Please and Thank You...

Link to comment
Share on other sites

--------------------------------------------------------------------------------------

Company: AQUALINE INTERNATIONAL, INC.

Email Address: export1@aqaulineusa.com

Contact Person: Ana Rodriguez

Count: 22

 

--------------------------------------------------------------------------------------

Company: AQUALINE PRODUCTS INC.

Email Address: tom@aqualinepro.com

Contact Person: Tom Lukk

Count: 23

 

--------------------------------------------------------------------------------------

Company: ARK FLOORS INC.

Email Address: erica.shu@ark-floors.com

Contact Person: Erica Shu

Count: 24

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /mailme.php on line 16

 

The problem is whats inside here

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

--------------------------------------------------------------------------------------

Company: Art's Trading Co

Email Address: erica.shu@ark-floors.com

Contact Person: Erica Shu

Count: 25

 

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

its got the same email and contact person as the one listed above it.  its got an ' inside Art's so that may be the problem.

 

--------------------------------------------------------------------------------------

Company: Asia Direct Imports

Email Address: richardamoore@cox.net

Contact Person: Richard Moore

Count: 26

Link to comment
Share on other sites

Here I re-wrote your script (this shows everything from table1 and table2), try it:

 

$db = mysql_connect('localhost', 'username', 'password');
if (FALSE === $db) {
  header('HTTP/1.0 500 Internal Error');
  trigger_error('Failed to connect to the database server, verify your details. (mysql said: ' . mysql_error() . ')');
  
  echo 'The server is experiencing some temporary problems and will be resolved soon, please check back soon.';
  exit(0);
}

if (FALSE === mysql_select_db('database', $db)) {
  header('HTTP/1.0 500 Internal Error');
  trigger_error('Failed to select the database, verify if the database name exists.');
  
  echo 'The server is experiencing some temporary problems and will be resolved soon, please check back soon.';
  exit(0);
}

$number = 1;
$separator = '-- %03s ' . str_repeat('-', 60);

$result = mysql_query('SELECT * FROM table1 JOIN table2 USING Company');
while ($row = mysql_fetch_assoc($result)) {
  $company = $row['Company'];
  $email = $row['Email Address'];
  $contactPerson = $row['Contact Person'];
  
  // -- 001 -----------------------------------------------
  // Company: 
  // Email Address: 
  // Contact Person: 
  echo sprintf($separator, $number++),
       'Company: ', $company, "<br/>\n",
       'Email Address: ', $email, "<br/>\n",
       'Contact Person: ', $contactPerson, "<br/>\n<br/>\n",
}

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.