Jump to content

[SOLVED] Very simple MySql question.. I think.


NoobNewbie

Recommended Posts

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:

 


$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.