Jump to content

Select query not working when defining ID


Moorcam
Go to solution Solved by Jacques1,

Recommended Posts

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 by DanEthical
Link to comment
Share on other sites

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 by Jacques1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by DanEthical
Link to comment
Share on other sites

 

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

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.