0

I need to be able to check and see in a certain string is anywhere within my SQL table. The table I am using only has one column of char's. Right now it is saying that everything entered is already within the table, even when it actually is not.

Within SQL I am getting the rows that have the word using this:

SELECT * FROM ADDRESSES WHERE STREET LIKE '%streeetName%';

However, in PHP the word is being entered by the user, and then I am storing it as a variable, and then trying to figure out a way to see if that variable is somewhere within the table.

 $duplicate = mysql_query("SELECT * FROM ADDRESSES WHERE STREET_NAME LIKE '%$streetName%'", $connect);
    if(!empty($duplicate))
    {
       echo "Sorry, only one of each address allowed.<br /><hr>";
    }
3
  • 1
    How about you count the rows returned by the query? And there is no more support for mysql_* functions, they are officially deprecated, no longer maintained and removed in PHP 7.0.0. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future. Commented May 3, 2017 at 5:08
  • First thing, don't use mysql_* function, instead use mysqli_* functions. use check mysql_num_rows() > 0 means some rows return from the query Commented May 3, 2017 at 5:09
  • You need to ditch mysql and go for the PDO or Improved standards (mysqli) with PHP, mysql is depreciated as of the last few builds of PHP. "The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MySQLi or PDO_MySQL extensions." PHP 5.5+ Depreciated Features Commented May 3, 2017 at 5:11

3 Answers 3

2

You need to do a little bit more than building the query, as mysql_query only returns the resource, which doesn't give you any information about the actual result. Using something like mysql_num_rows should work.

$duplicate = mysql_query("SELECT * FROM ADDRESSES WHERE STREET_NAME LIKE '%$streetName%'", $connect);
if(mysql_num_rows($duplicate))
{
   echo "Sorry, only one comment per person.<br /><hr>";
}

Note: the mysql_* functions are deprecated and even removed in PHP 7. You should use PDO instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Makes sense! Worked perfectly!
0

In the SQL you used

%streeetName%

But in the query string below, you used

%$streeetName%

Change the correct one

Comments

0
 $duplicate = mysql_query("SELECT * FROM ADDRESSES WHERE STREET_NAME LIKE '%$streetName%'", $connect);
if(!empty($duplicate))
{
   echo "Sorry, only one comment per person.<br /><hr>";
}

if($results->num_rows) is what you need to check if you have results back from your query. An example of connection and query, check, then print or error handle, the code is loose and not checked for errors. Best of luck...

//Typically your db connect will come from an includes and/or class User...
$db = new mysqli('localhost','user','pass','database');
$sql = "SELECT * FROM `addresses` WHERE `street_name` LIKE '%$streetName%'",$connect;
//test your queries in PHPMyAdmin SQL to make sure they are properly configured.
//store the results of your query in a variable 
$results = $db->query($sql);
$stmt = '';//empty variable to hold the values of the query as it runs through the while loop
###########################################################
#check to see if you received results back from your query#
###########################################################
if($results->num_rows){
        //loop through your results and echo or assign the values as needed
        while($row = $results->fetch_assoc()){
            echo "Street Name: ".$row['STREET_NAME'];
            //define more variables from your DB query using the $row[] array.
            //concatenate values to a variable for printing in your choice further down the document.
            $address .= $row['STREET_NAME'].' '.$row['CITY'].' '$row['STATE'].' '$row['ZIP'];
        }

}else{ ERROR HANDLING }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.