Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get a boolean value from... 1

Status
Not open for further replies.

Tasuki

MIS
Jul 26, 2002
169
US
I'm using PHP with MySQL. When a user enters a record, I want to check if a newly entered field is a match with a field in the MySQL database. So this is what I have (lets assume all the connections work):

$query = "SELECT * FROM tblTable WHERE sampleNum = 'sampleNum'";
$result = mysql_query($query);

// Trying to figure this part out...
if ($result) echo "<p>Match!</p>";
else echo "<p>No Match!</p>";

I tried echo-ing the $result field but it always returned:

Resource id #8

Whether or not the field is matching or not matching. Hope someone can help me with this problem.

Thanks,

-T
 
mysql_query() does not return the data from the database. It returns a resource handle (similar to a file handle) to the data.

You then have to use one of the mysql_fetch_* functions to fetch the data from the handle. See the example code in the PHP online manual entry for mysql_fetch_assoc()

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Addendum:

mysql_query() will return a boolean FALSE value, but only in the event that the query you passed it generated an error. If your query is well-formed and returns no records, you will still get back a resource handle. It's just a resource handle that points to an empty recordset. This would be similar to opening a file handle to a zero-length file. The file handle is still valid, it's just that there's no data there.

If you want to know how many records matched your SELECT query, see mysql_num_rows().

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks sleipnir214,

The latter reply seems to be exactly what I'm looking for.

$result = mysql_num_rows(mysql_query($query));
echo $result;

Testing it out (from the webform)... I keep getting a 0 even on a match. SQL seems to work fine in MySQL. I'll continue to play around.

Thanks again,

-T
 
Look in FAQ434-2999, titled "Debugging PHP". In there you will find a section with suggestions on debugging database-access code.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
AGH, I figured it out... Looking at first post at my SELECT query, this is what I had:

$query = "SELECT * FROM tblTable WHERE sampleNum = 'sampleNum'";

I was suppose to have this to pass the variable correctly:

$query = "SELECT * FROM tblTable WHERE sampleNum = '$sampleNum'";

Alongside the helpful function you provided (mysql_num_rows):

$result = mysql_num_rows(mysql_query($query));
echo $result;

Yields the correct results, returning a 1 if there is a match (the field is UNIQUE in mySQL so there is no possiblity of having two of the same sampleNum) and a 0 if no rows were returned!

Thanks and * for you!

-T
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top