0

I am trying to follow this previously answered question How to check if value already exists in MySQL database

Here is my code

<?php
$con=mysql_connect(Localhost,"mwtwoone_xbl","password","mwtwoone_xbl");
mysql_select_db( 'mwtwoone_xbl' );

$txn_id = $_GET['txn'];

echo $txn_id;

$checktxnID = mysql_query("SELECT txn_id from codes WHERE txn_id = '$txn_id'");

if (!$checktxnID) {
    die('Query failed to execute for some reason');
}

if (mysql_num_rows($checktxnId) > 0) {
echo "User id exists already.";
$user = mysql_fetch_array($checktxnId);
print_r($user); // the data returned from the query
}

echo "User id doesnt exist";

mysql_close($con);
?>

$txn_id will be defined and I simply need to check if its value is already in table codes on column txn_id. Thanks for all the help, it is very appreciated.

Now when I access URL .php?txn=123 I get 123User id doesnt exist already. The problem is, multiple rows on table codes columb txn_id contain value 123!

Community
  • 1
  • 1

2 Answers2

3

You've used $checktxnId rather than $checktxnID in your conditional > 0check.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Ingmar Boddington
  • 3,440
  • 19
  • 38
  • Damn it I can't believe this past hour of frustration was because of that small error, thank you so much. – user3029524 Nov 25 '13 at 00:02
  • And that's why I **never never never** use any mixed letter case. Too much room for typo errors. I always use underscores as a seperator. @user3029524 – Funk Forty Niner Nov 25 '13 at 00:06
  • @user3029524 I suggest that you always turn on error report. `error_report(E_ALL)`. refer to http://php.net/manual/en/function.error-reporting.php – Jason Heo Nov 25 '13 at 00:12
  • @Fred-ii- camelCase for variables and under_scores for array keys are a basic coding standard. His problem is _not_ using a proper IDE: NetBeans (free), phpStorm (paid)...among other things. – keyboardSmasher Nov 25 '13 at 02:02
  • You have a point there. @keyboardSmasher I'm "Old School" myself ;-) – Funk Forty Niner Nov 25 '13 at 02:03
-2

try to change this line

$checktxnID = mysql_query("SELECT txn_id from codes WHERE txn_id = '$txn_id'");

to

$checktxnID = mysql_query("SELECT txn_id from codes WHERE txn_id = '".$txn_id."'");
Robert Stevens
  • 488
  • 1
  • 7
  • 21