Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6026357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:26:01+00:00 2026-05-23T04:26:01+00:00

I am trying to verify that a string is unique within a MySQL column.

  • 0

I am trying to verify that a string is unique within a MySQL column. Here is the code sample:

<?php
$con = mysql_connect("hostname", "username", "password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("table_name", $con);


if (!mysql_num_rows(mysql_query("SELECT * FROM table_name WHERE unique_string = '123456' 
LIMIT 1"))) {

die('This string is not unique.');
}
mysql_close($con);
?>

In this code sample I have input the value 123456 as an example. This example assumes that the string 123456 already exists in the column unique_string. Therefore, it should die and return the statement “This string is not unique.” Sadly, it does not. Do you know why? Is there a better way to get this check done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:26:02+00:00Added an answer on May 23, 2026 at 4:26 am

    It’s generally unwise to chain your function calls as you have. Particularly with database calls. The mysql_*() functions expect certain things, like statement handles, to be passed in. But they can return a boolean FALSE instead of a handle if there’s a problem. This FALSE gets passed in instead of a handle and then everything’s broken.

    A general rule is to always assume your query will fail somehow, and program defensively:

    $sql = "SELECT ....";
    $result = mysql_query($sql) or die(mysql_error());
    $rows = mysql_num_rows($result);
    if ($row !== 0) then
        die("Not unique");
    }
    

    Beyond that, your query statement does look to be syntactically correct. Why not see what it’s returning?

    $row = mysql_fetch_assoc($result);
    var_dump($row);
    

    and see exactly what’s being matched. Maybe you DON’T have that value in the table and are mistaken in thinking that it’s there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to verify that a C# string is compliant with XML Schema
I'm trying to determine the regular expression to verify that a string has at
In a given string, I'm trying to verify that there are at least two
I'm trying to verify that certain fields in my form are numeric and not
I'm trying to find a regex that works to match a string of escape
I'm trying to verify if a schema matches the objects I'm initializing. Is there
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to make a make generic select control that I can dynamically add elements
Trying to make a MySQL-based application support MS SQL, I ran into the following
Trying to perform a single boolean NOT operation, it appears that under MS SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.