NoobNewbie Posted June 25, 2007 Share Posted June 25, 2007 Hello! First post ever and I believe some of you will laugh at the question but, hey, we go to start somewhere, right? 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: $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. Quote Link to comment https://forums.phpfreaks.com/topic/57114-solved-very-simple-mysql-question-i-think/ Share on other sites More sharing options...
Wildbug Posted June 25, 2007 Share Posted June 25, 2007 First of all, you should be SELECTing columns, not tables and FROM tables not a database. In other words: <?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: <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 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/57114-solved-very-simple-mysql-question-i-think/#findComment-282229 Share on other sites More sharing options...
NoobNewbie Posted June 25, 2007 Author Share Posted June 25, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/57114-solved-very-simple-mysql-question-i-think/#findComment-282246 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.