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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting query to work

Status
Not open for further replies.

M8KWR

Programmer
Aug 18, 2004
864
GB
I am new to PHP so please bare with me; i have the following code which i have put together using several websites but i can not seem to get it to work.

Code:
<?PHP
$TPSNUMBER = $_POST['TPSNum']; // carry the variable from the user input TPSNum
$db = mysql_connect("localhost","Username","Password"); // host / username / password
mysql_select_db("tps",$db);

$sql_result = mysql_query("SELECT `phone1` FROM `tps` WHERE `phone1` = $TPSNUMBER", $db); // query

$result = mysql_query($sql_result);

if ($result) {
    while ($array= mysql_fetch_assoc($result)) {
        echo "The number was found";
    }
} else {
    echo "No results found";
}

?>

Even though i am using a value that i know is in the table, it is always bring "No result found".

I am assuming it is something really simple, but i can not see it for love or money!!!!

Thanks in advance
 
If the line outputting the mesage "No result found" is being run, then the if-statement is evaluating to FALSE.

If the if-statement evaluates to FALSE, it is because $result has the value FALSE in it.

The value in $result got there from the invocation of mysql_query(). The PHP online manual page for mysql_query() clearly states, "For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error."

Thus, I conclude your query has an error of some kind. Please see section 1.5 of faq434-2999 for advice on debugging database-access PHP code.



Want the best answers? Ask the best questions! TANSTAAFL!
 
consider also enquoting and escaping variables in the where statement,
 
Have you tried placing your variable in quotation marks? Something like this:
"SELECT `phone1` FROM `tps` WHERE `phone1` = '".$TPSNUMBER."'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top