Jump to content

get id of row with name


Gatsu

Recommended Posts

SELECT id FROM table WHERE name = "$name"

Thank you but why am I getting this error?Parse error: syntax error, unexpected T_STRING in F:\xampp\htdocs\list.php on line 12from this line:
$result2 = "SELECT id FROM annons WHERE rubrik= "$rubrik" ";

This is what I use and I want the id of the ' . $add['rubrik'] . ' to show in the $result2

echo "<td>" . '<a href="open.php?id='.urlencode($result2).'">' . $add['rubrik'] . '</a>' . "</td>";

This is my whole code:

<a href="skapaannons.php">Lägg till annons!</a><?php$con = mysql_connect("","","");if (!$con) {	die('Could not connect: ' . mysql_error());}$db_selected = mysql_select_db("annons", $con);$result = mysql_query("SELECT annons.id, annons.datum, annons.rubrik, annons.pris, annons.område, annons.säljare, annons.email, annons.tele, annons.kat, annons.text, områden.plats AS område FROM annons, områden WHERE annons.område=områden.id");$result2 = "SELECT id FROM annons WHERE rubrik= "$rubrik" ";echo '<h2>Annonser</h2>';echo '<table cellpadding="2" rules="groups" frame="box">';echo '<thead><tr><th width="150px" align="left">Datum</th><th width="350px" align="left">Rubrik</th><th width="50px" align="left">Pris</th><th width="100px" align="left">Område</th></thead>';echo '<tbody>';while ($add = mysql_fetch_array($result, MYSQL_BOTH)){  echo "<td>" . $add['datum'] . "</td>";  echo "<td>" . '<a href="open.php?id='.urlencode($result2).'">' . $add['rubrik'] . '</a>' . "</td>";  echo "<td>" . $add['pris'] . ":-</td>";  echo "<td>" . $add['område'] . "</td>";  echo '</tr>';}  echo "</tbody></table>";echo "<br \>";echo "<br \>";mysql_close($con);?>

Link to comment
Share on other sites

Hi, the only error i see is that you missed to concat the line, just try adding this two dots:

$result2 = "SELECT id FROM annons WHERE rubrik= " . $rubrik . " ";

Link to comment
Share on other sites

I've tried with dots but then I get the next error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in F:\xampp\htdocs\list.php on line 20

line 20:

while ($add = mysql_fetch_array($result, MYSQL_BOTH)){

Link to comment
Share on other sites

The $rubrik variable still needs to have quotes around it. You can escape them. Also, you need to call it in mysql_query()

$result2 = mysql_query("SELECT id FROM annons WHERE rubrik= \"$rubrik\"");

Link to comment
Share on other sites

uhm.. now I'm getting:Warning: urlencode() expects parameter 1 to be string, resource given in F:\xampp\htdocs\list.php on line 23line 23:

echo "<td>" . '<a href="open.php?id='.urlencode($result2).'">' . $add['rubrik'] . '</a>' . "</td>";

Link to comment
Share on other sites

The mysql_query() function returns a MySQL resource, which you can do things with. If you wanted to retrieve the value of the id you could do

$result2 = mysql_query("SELECT id FROM annons WHERE rubrik= \"$rubrik\"");$row = mysql_fetch_assoc($result2);$id = $row['id'];

Link to comment
Share on other sites

oh, nice. I should buy a book :) Do you suggest any books?Anyway!This removed the warning but still wont show any id in the link :S

$result2 = mysql_query("SELECT id FROM annons WHERE rubrik= \"$rubrik\"");$row = mysql_fetch_assoc($result2);$id = $row['id'];...echo "<td>" . '<a href="open.php?id='.urlencode($id).'">' . $add['rubrik'] . '</a>' . "</td>";

Link to comment
Share on other sites

Are you sure there is an annon with the rubric you specified?You can check the number of rows returned with mysql_num_rows().

Link to comment
Share on other sites

Yes the rubrik exists and I want the ID of the row with ' . $add['rubrik'] . ' that you can find here:

echo "<td>" . '<a href="open.php?id='.urlencode($id).'">' . $add['rubrik'] . '</a>' . "</td>";

and with mysql_num_rows($result2) I get 0 rows :S

Link to comment
Share on other sites

and with mysql_num_rows($result2) I get 0 rows :S
So that means that there isn't a row with that rubrik. Are you ure you are initiallizing $rubrik properly? Try echoing:
echo "SELECT id FROM annons WHERE rubrik= \"$rubrik\"";

and see what you get.

Link to comment
Share on other sites

I put $rubrik as you told me to :)I echo $result2 and get "Resource id #4" wich I dont understand waht it means, 4 is not the id of that rubrik.Take a look at my site: ***********there you can see in the "Rubrik" colum that it exists and that whe nyou hold your mouse over them you get the adress "http://90.230.82.9/open.php?id=" for both :S

Link to comment
Share on other sites

I echo $result2 and get "Resource id #4" wich I dont understand waht it means, 4 is not the id of that rubrik.
I meant echo the querystring, not the result. So literally
echo "SELECT id FROM annons WHERE rubrik= \"$rubrik\"";

See what you get then.Edit: Also, I have a feeling there may be some thing wrong with your code as a whole. What does it all look like?

Link to comment
Share on other sites

great, now I know how to check that :)this is all my code:

<a href="skapaannons.php">Lägg till annons!</a><?php$con = mysql_connect("","","");if (!$con) {	die('Could not connect: ' . mysql_error());}$db_selected = mysql_select_db("annons", $con);$result = mysql_query("SELECT annons.id, annons.datum, annons.rubrik, annons.pris, annons.plats, annons.namn, annons.email, annons.tele, annons.kat, annons.text, platser.plats AS plats FROM annons, platser WHERE annons.plats=platser.id");$result2 = mysql_query("SELECT id FROM annons WHERE rubrik= \"$rubrik\"");$row = mysql_fetch_assoc($result2);$id = $row['id'];echo '<h2>Annonser</h2>';echo '<table cellpadding="2" rules="groups" frame="box">';echo '<thead><tr><th width="150px" align="left">Datum</th><th width="350px" align="left">Rubrik</th><th width="50px" align="left">Pris</th><th width="100px" align="left">Område</th></thead>';echo '<tbody>';while ($add = mysql_fetch_array($result, MYSQL_BOTH)) {  echo "<td>" . $add['datum'] . "</td>";    echo "<td>" . '<a href="open.php?id='.urlencode($id).'">' . $add['rubrik'] . '</a>' . "</td>";  echo "<td>" . $add['pris'] . ":-</td>";  echo "<td>" . $add['plats'] . "</td>";  echo '</tr>';}  echo "</tbody></table>";echo "SELECT id FROM annons WHERE rubrik= \"$rubrik\"";echo "<br \>";echo "<br \>";mysql_close($con);?>

Link to comment
Share on other sites

But... you are already selecting the ID in your query

$result = mysql_query("SELECT [b]annons.id[/b], annons.datum, annons.rubrik, annons.pris, annons.plats, annons.namn, annons.email, annons.tele, annons.kat, annons.text, platser.plats AS plats FROM annons, platser WHERE annons.plats=platser.id");

So why not just

echo "<td>" . '<a href="open.php?id='.urlencode($add['id']).'">' . $add['rubrik'] . '</a>' . "</td>";

Link to comment
Share on other sites

Remember you can use aliases if there is ambiguity.

SELECT annons.id AS annon_id, platser.id AS platser_id FROM annons, platser

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...