简体   繁体   中英

Selecting Multiple Rows PHP/MySQL

$sqlCommand2 = "SELECT blogid FROM blogtags WHERE tag='$pageid'";
$query2 = mysqli_query($myConnection, $sqlCommand2) or die (mysqli_error());
while ($row = mysqli_fetch_array($query2)) {
    $selectedtag = $row['blogid'];
}   

This sql select should give $selectedtag the value of 1,2,3 but it is only giving the value of 3. The last row that is equal to pageid. Can anyone figure out why it is not pulling all of the rows and only the last one?

This is because you are assigning values to a variable inside a loop. So the last value overwrites all the other values. Use an array instead.

change $selectedtag = $row['blogid']; to $selectedtag[] = $row['blogid'];

try with :

$selectedtag = array();
while ($row = mysqli_fetch_array($query2)) {
    $selectedtag[] = $row['blogid'];
}

print_r($selectedtag);

You keep overwriting $selectedtag with the blogid value of your latest iteration of the rows.

You should either create an array with these values or do something with the value within the loop.

It all depends on what you are planning to do with the data.

If you want to search for blogs with the gathered ID's , you are better off by creating a bigger query in stead of searching for blog id's looping those and searching for blogs.

You could do something like this:

SELECT id, title, content, author, date
FROM blogs
WHERE id IN 
  (SELECT blogid FROM blogtags WHERE tag ='$pageid')

The above is a very rough example with few knowledge of your column / table naming, but with the above query you should get a result of blogs with the specific tag.

Good luck!

change to:

  $sqlCommand2 = "SELECT blogid FROM blogtags WHERE tag='$pageid'";
        $query2 = mysqli_query($myConnection, $sqlCommand2) or die (mysqli_error());
                    while ($row = mysqli_fetch_array($query2)) {
                    $selectedtag[] = $row['blogid'];
                    } 

$selectedtag[] <--put values in the array;

The WHERE clause in your query is what is stopping it.

If the $pageid variable has the value of 3, then the query evaluates to:

SELECT COLUMN blogid FROM TABLE blogtags ONLY WHERE THE COLUMN tag HAS THE VALUE OF 3

So basically, the query is doing exactly what it says.

If you want to select the previous two rows, then you have to be creative with your WHERE clause:

SELECT blogid FROM blogtags WHERE tag <= $pageid AND tag > $pageid - 3

That will select everything that has a tag that is 3 or less and greater then 3 - 3 = 0;

So, tag can be 3, 2 or 1.

EDIT

I may have misunderstood the question but I will leave my answer here incase it helps someone else who stumbles upon it.

Quit mysqli and use PDO . It has everything you need:

$stm = $pdo->prepare("SELECT blogid FROM blogtags WHERE tag=?");
$stm->execute(array($pageid));
$rows = $stm->fetchAll(PDO::FETCH_COLUMN, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM