Moorcam Posted January 22, 2017 Share Posted January 22, 2017 (edited) Hi folks, I know people will say, "You should use PDO" but I prefer MySQLi for the time being. I am trying to display data based on its ID from the database by using the following. However, if I use: WHERE id='$id' Nothing appears. Basically, what I am doing is, when someone clicks a link, it will open a new page displaying the content related to the link they clicked. The URL will show the ID, which works fine, such as: domain.com/details.php?id=245 If I remove the WHERE clause, all rows are shown. If I use the WHERE clause, nothing is shown. No errors either. Here is the code in question: <?php $id = mysqli_real_escape_string($mysqli, $row['id']); $sql = "SELECT * FROM tours WHERE id = '" . $id. "'"; $result = $mysqli->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { ?> <h3><?php echo $row['tour_name']; ?></h3> <?php } } ?> Any help would be appreciated. Cheers Danno Edited January 22, 2017 by DanEthical Quote Link to comment https://forums.phpfreaks.com/topic/303004-select-query-not-working-when-defining-id/ Share on other sites More sharing options...
Jacques1 Posted January 22, 2017 Share Posted January 22, 2017 (edited) Unfortunately, we're not clairvoyant (even though that would definitely be useful for a lot of questions). So what does $id actually say? Is it what you think it is? Where does it come from? Oddly, you seem to take it from the result set of some other query when it should come from the URL paramaters according to your description. Besides that: You say you prefer mysqli, but you have not really bothered to learn it. mysqli supports prepared statements, there's absolutely no reason to rely on obsolete and fragile manual escaping. There's also no reason to check the number of rows before the fetch loop when the fetch loop itself already does that. You don't seem to have any error checking. And you should not mix the procedural mysqli API with the object-oriented API. Pick one and stick with it. This kind of shows why we recommend against mysqli. It's obviously too complicated for the average programmer. Edited January 22, 2017 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/303004-select-query-not-working-when-defining-id/#findComment-1541823 Share on other sites More sharing options...
Moorcam Posted January 22, 2017 Author Share Posted January 22, 2017 Unfortunately, we're not clairvoyant (even though that would definitely be useful for a lot of questions). So what does $id actually say? Is it what you think it is? Where does it come from? Oddly, you seem to take it from the result set of some other query when it should come from the URL paramaters according to your description. Besides that: You say you prefer mysqli, but you have not really bothered to learn it. mysqli supports prepared statements, there's absolutely no reason to rely on obsolete and fragile manual escaping. There's also no reason to check the number of rows before the fetch loop when the fetch loop itself already does that. You don't seem to have any error checking. And you should not mix the procedural mysqli API with the object-oriented API. Pick one and stick with it. This kind of shows why we recommend against mysqli. It's obviously too complicated for the average programmer. Hi, Thanks for the reply. I am learning mate, which is why I have come here. It's easy for experienced programmers like your good self to say do this and that and use the jargon to describe what it is we should be doing. But for us, less average programmer-wannabes the jargon is something that we find hard at times to understand. So to say I am not bothered to learn MySQLi is a little harsh to be honest. Not being rude, just stating, because I am TRYING to learn. I am using MySQLi because I haven't coded in like 15 years and always used MySQL. I know I should use PDO. I will eventually. This project is just to get my feet wet again. Here is what I am using to call the ID into the URL, as I feel this maybe contributing to the issue. However, I am unsure if this is the correct way or not. Been trying to find similar things on Google but can't find anything on it. <td><a href="tourdetails.php?tour_id=<?php echo $row['tour_id']; ?>"><?php echo $row['tour_name']; ?></a></td> Thanks in advance if anyone can put me in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/303004-select-query-not-working-when-defining-id/#findComment-1541824 Share on other sites More sharing options...
Jacques1 Posted January 22, 2017 Share Posted January 22, 2017 Passing the ID via the URL is perfectly fine. I'm talking about your query. You said there's a problem when you add a WHERE clause with the ID, so I'm interesting in what the ID actually says: $id = mysqli_real_escape ...; // inspect the content of $id var_dump($id); You can also just echo the ID. Anything that puts it onto your screen. I don't think this is “jargon”. Quote Link to comment https://forums.phpfreaks.com/topic/303004-select-query-not-working-when-defining-id/#findComment-1541825 Share on other sites More sharing options...
Moorcam Posted January 22, 2017 Author Share Posted January 22, 2017 Thank you. I meant in general. Most programmers will fill you with Jargon while trying to help. I get lost at times. The dump shows: string(0) Strange hey. Quote Link to comment https://forums.phpfreaks.com/topic/303004-select-query-not-working-when-defining-id/#findComment-1541827 Share on other sites More sharing options...
Jacques1 Posted January 22, 2017 Share Posted January 22, 2017 The dump shows: string(0) Then obviously $row['id'] is not what you want (wherever it comes from). Why are you not using the ID from the URL? Quote Link to comment https://forums.phpfreaks.com/topic/303004-select-query-not-working-when-defining-id/#findComment-1541828 Share on other sites More sharing options...
Moorcam Posted January 22, 2017 Author Share Posted January 22, 2017 (edited) Then obviously $row['id'] is not what you want (wherever it comes from). Why are you not using the ID from the URL? I am. The one in the URL is from the DB. <?php echo $row['tour_id']; ?> I am using the same echo statement on the page but not getting anything. I changed to tour_id in the db and code to see if that would help. Thought there might be a possible mixup with another piece of code but still nothing. But if I remove the WHERE clause, it will show data. I just want to show the data that is compared to the id in the url. Edited January 22, 2017 by DanEthical Quote Link to comment https://forums.phpfreaks.com/topic/303004-select-query-not-working-when-defining-id/#findComment-1541829 Share on other sites More sharing options...
Solution Jacques1 Posted January 22, 2017 Solution Share Posted January 22, 2017 Then use the ID from the URL. Not from the database. The URL. As in: $id = mysqli->real_escape_string($_GET['tour_id']); 1 Quote Link to comment https://forums.phpfreaks.com/topic/303004-select-query-not-working-when-defining-id/#findComment-1541830 Share on other sites More sharing options...
Moorcam Posted January 22, 2017 Author Share Posted January 22, 2017 Then use the ID from the URL. Not from the database. The URL. As in: $id = mysqli->real_escape_string($_GET['tour_id']); Thank you. I tried that and it first stated that -> was unexpected. I changed -> to _ and get the following: $tour_id = mysqli_real_escape_string($mysqli, $_GET['tour_id']); It works. Thank you so much. I really appreciate your help and guidance. I promise not to come and ask questions unless I am really stuck, just like today. *virtual handshake * Danno Quote Link to comment https://forums.phpfreaks.com/topic/303004-select-query-not-working-when-defining-id/#findComment-1541831 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.