Please login or register.

Login with username, password and session length
Advanced search  

News:

(2009-05-04) The Rules and Terms of Service have been updated. Please make sure you read, understand, and follow them.

Author Topic: [SOLVED] Very simple MySql question.. I think.  (Read 355 times)

0 Members and 1 Guest are viewing this topic.

NoobNewbie

  • Irregular
  • Offline Offline
  • Posts: 7
    • View Profile
[SOLVED] Very simple MySql question.. I think.
« on: June 25, 2007, 12:43:36 PM »
Hello! First post ever and I believe some of you will laugh at the question but, hey, we go to start somewhere, right?  ;D

So, I'm really new to MySql and PHP also, but with the help of the almighty Internet I managed to learn a few things. So, my question is actually very simple: how do we know the id of some element in our table in order to do a query? Allow me to explain: for example, we have a table named "people" with 3 columns: "name", "age" and "id". In the html page we click on the name and then I want it to show me the age of that person. So, I thought that we went to php and did something like this:

Code: [Select]

$name = $_POST['name'];
query = "SELECT people FROM people_db WHERE name='".$name."'";
$do_it = mysql_query($query);


But I found out that that's not a good practice, and what I should do was to do the query based on the id, because it's a unique value on the table. But how do we know what's the id before we do the query? Any help you could provide I'd be gratefull.
Logged

Wildbug

  • Devotee
  • Offline Offline
  • Posts: 1,142
    • View Profile
Re: Very simple MySql question.. I think.
« Reply #1 on: June 25, 2007, 01:23:16 PM »
First of all, you should be SELECTing columns, not tables and FROM tables not a database.  In other words:

Code: [Select]
<?php

mysql_select_db
('people_db');
$query "SELECT * FROM people WHERE name='$_POST[name]'";
$do_it mysql_query($query);

?>

So, to answer your question about knowing the id beforehand, since you're displaying names on a page and drawing those names from the database, you might as well take the id from the database at the same time.  You can print the name out on the HTML page as a link, but you should use the id in the link instead of the name.

Example:
Code: [Select]
<a href="moreinfo.php?id=2">NoobNewbie</a>

That's if the id was 2 in the database for the name "NoobNewbie."  Then, when the link is clicked, the following code is executed:

moreinfo.php
Code: [Select]
<?php

if (isset($_GET['id']) && is_numeric($_GET['id'])) {

$do_it mysql_query("SELECT age FROM people WHERE id=$_GET[id]");
if ($do_it && mysql_num_rows($do_it)) echo mysql_result($do_it,0);

}
?>


(mysql_result() is usually only good when returning one expected result.  Otherwise use mysql_fetch_row()/_assoc().)  The above code will query the database for a user by the id in $_GET['id'] and print the age, if a result is found.
Logged
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

NoobNewbie

  • Irregular
  • Offline Offline
  • Posts: 7
    • View Profile
Re: Very simple MySql question.. I think.
« Reply #2 on: June 25, 2007, 01:41:46 PM »
Thanks a lot! And sorry about the select typo. So, in order to get the id, I need to "store" it in the link in order to use it in the next query. Well, that sounds simple. Thanks!
Logged

PHP Freaks Forums

 
 
 

Page created in 0.087 seconds with 21 queries.