Gatsu Posted April 28, 2008 Share Posted April 28, 2008 Hi, I got a row with "id" and "name" as colums, now on my site I know the name but what can I use to get the id of that row with the name I got? Link to comment Share on other sites More sharing options...
Synook Posted April 28, 2008 Share Posted April 28, 2008 SELECT id FROM table WHERE name = "$name" Link to comment Share on other sites More sharing options...
Gatsu Posted April 28, 2008 Author Share Posted April 28, 2008 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 More sharing options...
alexnofue Posted April 28, 2008 Share Posted April 28, 2008 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 More sharing options...
Gatsu Posted April 28, 2008 Author Share Posted April 28, 2008 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 More sharing options...
Synook Posted April 28, 2008 Share Posted April 28, 2008 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 More sharing options...
Gatsu Posted April 28, 2008 Author Share Posted April 28, 2008 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 More sharing options...
Synook Posted April 29, 2008 Share Posted April 29, 2008 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 More sharing options...
Gatsu Posted April 29, 2008 Author Share Posted April 29, 2008 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 More sharing options...
Synook Posted April 29, 2008 Share Posted April 29, 2008 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 More sharing options...
Gatsu Posted April 29, 2008 Author Share Posted April 29, 2008 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 More sharing options...
Synook Posted April 29, 2008 Share Posted April 29, 2008 and with mysql_num_rows($result2) I get 0 rows :SSo 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 More sharing options...
Gatsu Posted April 29, 2008 Author Share Posted April 29, 2008 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 More sharing options...
Synook Posted April 29, 2008 Share Posted April 29, 2008 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 literallyecho "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 More sharing options...
Gatsu Posted April 29, 2008 Author Share Posted April 29, 2008 ooh, then I get SELECT id FROM annons WHERE rubrik= "" Link to comment Share on other sites More sharing options...
Synook Posted April 29, 2008 Share Posted April 29, 2008 So $rubrik isn't set... what does your code look like? Link to comment Share on other sites More sharing options...
Gatsu Posted April 29, 2008 Author Share Posted April 29, 2008 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 More sharing options...
Synook Posted April 29, 2008 Share Posted April 29, 2008 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 More sharing options...
Gatsu Posted April 29, 2008 Author Share Posted April 29, 2008 oh my.. sorry for all this trouble and thank you for makeing my life very simple ^^I never thought that I would be able to get the right ID with that query :)its how I want it now =) Link to comment Share on other sites More sharing options...
Synook Posted April 30, 2008 Share Posted April 30, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.