Link to home
Create AccountLog in
Avatar of twittoris
twittoris

asked on

Posting and Querying

I am having trouble recreating this output in php:

SELECT *
FROM  `success`
WHERE  `ENTITY_ADD` LIKE  '%CompanyA%'


<?php

 
// Database Connection Settings
  
$host="xxxx";
$username="xxx";
$password="xxx";
$db_name="doslinks";
$tbl_name="success"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$Fname = mysql_real_escape_string($_POST["Fname"]); 

// Make a MySQL Connection
$query = "SELECT * FROM $tbl_name WHERE 'ENTITY_ADD' LIKE $Fname"; 
	 
$result = mysql_query($query) or die(mysql_error());
echo $result;
?>

Open in new window

Avatar of marklogan
marklogan
Flag of United Kingdom of Great Britain and Northern Ireland image

LIKE statements need the % character.


%       Matches any number of characters, even zero characters

http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like
LIKE '%'.$Fname.'%'";

Open in new window

The following should print the values from your table onto your page: Change the "fieldName" in $row['fieldName'] to a field name in your table.
 
mysql_connect('localhost', 'userName', 'password') OR DIE ('Unable to connect to database! Please try again later.');
        mysql_select_db('dbName');
        $sql = "SELECT * FROM `success` WHERE `ENTITY_ADD` LIKE '%CompanyA%'";
        $result = mysql_query($sql) or die('Query failed: ' . mysql_error());
        $counter = 0;
        if($result):
            while($row = mysql_fetch_array($result)) {
                 echo($row['fieldName']);
            }
         endif;

Open in new window

Sorry, I didn't see the sample code you posted til just now. Page didn't load - @marklogan is right.
Avatar of twittoris
twittoris

ASKER

Hi MarkLogan

Tried that and it returned:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.Arwen.'%'' at line 1

From:

$query = "SELECT * FROM $tbl_name WHERE 'ENTITY_ADD' LIKE '%'.$Fname.'%'";
$result = mysql_query($query) or die(mysql_error());
echo $result;
And if I try:

$query = "SELECT * FROM $tbl_name WHERE 'ENTITY_ADD' LIKE '%".$Fname."%'";

With quotes around the variable it returns:

Resource id #2
Avatar of Dave Baldwin
$query = "SELECT * FROM $tbl_name WHERE 'ENTITY_ADD' LIKE '%'.$Fname.'%'";

should be:
$query = "SELECT * FROM $tbl_name WHERE `ENTITY_ADD` LIKE '%".$Fname."%'";

Open in new window

"Resource id #2" is what it should return.  Now you need to use "mysql_fetch_assoc($result)" to get the actual data.  http://us2.php.net/manual/en/function.mysql-fetch-assoc.php  There are a couple of other "mysql_fetch" methods you can use also.
@DaveBaldwin
Still returns Resource #2

If I search within mySQL it returns about 30 rows that have a matching LIKE %% command.
ASKER CERTIFIED SOLUTION
Avatar of Bruce Smith
Bruce Smith
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
That works! Is there a way to filter duplicate results?
Either

SELECT DISTINCT(COLUMNNAME)

OR

GROUP BY COLUMNNAME
There are duplicates in my database. Is there a command I can run on the server side to delete rows that are exactly the same and leave 1?