tags:

views:

56

answers:

2

This is a fairly simple question. Basically, I'm having a program send HardWare ID's to my PHP script as GET data. I need the PHP script check to make sure that HardWare ID is in a specific MySQL column, and if it is, { continue the script, } else { exit(); }

Problem is I'm not too good with MySQL and have no idea how to do this. However, I feel that I should know this by now, so if someone could also link me to a good tutorial site for MySQL, that kind of keeps it "humanized" if you know what I mean. One that "dumbs it down." I'm not dumb or anything, I just get sidetracked easily, and if all its doing is showing me code and not explaining it, I won't pick it up. If you don't have any tutorial sites off the top of your head, I'll settle for help with the first question, and try to hunt down a tutorial later.

EDIT: It was noted that it may help to see how my database is set up. I have a table called "hwids" In this table there are 3 columns: "id" "hwid" and "paypal"

(They buy the program from me, I add their HWID to the database so they can use the program. If they chargeback and try to scam me, I can easily check which HWID is assigned for the paypal that did a chargeback, and remove it from the system via a management page)

+2  A: 

When I started learning PHP/MySQL, I really liked the books that O'Reilly publishes on the subject. You may want to check out Learning PHP, MySql, and Javascript. The official sites for PHP and MySql provide a lot of good links and information, as well as documentation. Tizag has a useful tutorial.

To answer your question a little, you could do a select query that checks if the number of rows found is greater than zero. i.e.:

$query = "SELECT * FROM hwids WHERE hwid = 'hardware_id'";
$result = mysql_query($query);
$num = mysql_num_rows($result);
if ($num > 0)
{
    // continue script
}
else 
{
    // exit/inform user
}
JYelton
Hm. I never thought of counting it and making sure it was greater than 0. However, are there any other options for doing it?
Rob
@JYelton, Exactly my approach.
Anthony Forloney
@Rob: You could read the resultset and iterate over the rows, though that would be unnecessary unless you had more than one row matching your hardware id. In that case, you might be checking the contents of another column for validity. For example if two rows had identical hardware ID's, you might want to ensure that some other column matched criteria (like maybe a valid warranty or something.)
JYelton
@Rob: I am editing this again because the query would only ever have one row because it is doing a count. I started writing the query for C# thinking of doing an ExecuteScalar. :)
JYelton
Ah now it makes more sense. Thank you for the help, and for the tutorial links. I'll be checking them out shortly.
Rob
+2  A: 

There are a number of steps you need to go through to achieve your goal, Breaking them up and learning each will help with your attention span (no disrespect I have the same problem)

Steps required to achieve your goal

  1. Connect to MySql database - see documentation on mysql_connect and mysql_select_db
  2. Validate your input (ensure you have a valid Hardware ID)
  3. Prepare a query - I would suggest something like $Sql=sprintf("Select [columnName] from [tableName] where [columnName]=%s", mysql_escape_string($hardwareID));
  4. get the recordset back by using mysql_query($sql) -see documentation on php.net
    5.Check see if it is empty mysql_num_rows() if so exit script

Hope that helps a resource I would point you at would be http://tuxradar.com/practicalphp particularly Chapter 9

All as others have put above really I was a little slow typing :)

Shaun Hare
Hey, been reading that practicalphp book. Not just Ch9 though, figured I may as well read all of it so I started at the beginning. I like it, I've already learned a few new things, thanks.
Rob