Still Stuck on the easy stuff...

The more I try, the more confused I get...

I have a table called "member" in MySQL.
The columns within member are:
"ID"; "name"; "email"; "status"

Suppose the value of ID was entered from an HTML form, how could I say:

"If the HTML form entry = the ID in table 'member', output the details from that row.
else loop the above phrase until the bottom of table member is reached or until a match is found"?

*I wasn't born to program!*

Cheers (again)... and double the points this time - woohoo.

Who is Participating?
mafwebConnect With a Mentor Commented:
Hi Dave,

how about this:

//connect to the db

mysql_query("select * from members where ID=$id);
if (mysql_affected_rows()>0)
      //you got a matching id and now,
      //you can go through the entries with
      //output form again

with this, you will get at once all of the entries of members with matching IDs (or none, if no matching IDs exist).

hope this helps

OK then, this should work with a bit of luck...
$database  is the name of your database which contains the member table.
$id is the id as given by the HTML form.
      $database = "databasename";
      $conn = mysql_connect("localhost","root","");
      $db = mysql_select_db($database,$conn);

      $sql = "select name,email,status from member where ID='$id'";
      $query = mysql_query($sql);
      echo "<TABLE>\n";
      echo "<TR><TD>Name</TD><TD>Email</TD><TD>Status</TD></TR>\n";
      while ($row=mysql_fetch_row($query)) {
            echo "<TR><TD>$row[0]</TD><TD>$row[1]</TD><TD>$row[2]</TD></TR>\n";                  
      echo "</TABLE>";

What happens here is quite simple... you query the SQL (in $sql) in MySQL and then get PHP to fetch one row at a time of said query.
The fields of each row are returned in an array, in this case $row.
D'oh !!!
The line above which reads :

"$query = mysql_query($sql);"

Should in fact read :

"$query = mysql_query($sql,$db);"
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

dht1Author Commented:
Thanks, Gravity - your answer certainly answered my question...

Stupidly however, I neglected to mention that I wanted to use the result in order to use an if command...

ie. If a match is found, then do x.

How would I insert a boolean variable to state that a match had been made?

Presumably I could then insert a statement to the effect of:

if (x = "true"): do x, y or z


I appreciate that I am a complete plonker... but if you can incorporate this additional point, I'll increase the points (assuming you care about the points!)

Thanks (and sorry),

dht1Author Commented:

I'm slowly getting there!

Gravity, if you particularly want some points, let me know.


It would be appreciated :)
dht1Author Commented:
I'll post another question for you to answer - sorry, I don't know if there's a tidier method.
Groovyness - unfortunatly, I don't think there's another way of doing it, such is life...

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.