<?php
$con=mysqli_connect("localhost","user","password","database");
$post_id = '5';
$query= mysqli_query($con,"select t.tag_name from tag_map tm join article p on p.post_id = tm.post_id join tag t on t.tag_id = tm.tag_id where p.post_id = '$post_id'");
while($que = mysqli_fetch_row($query))
{
echo "<pre>";
print_r($que);
}
?>
Array
(
[0] => audit
)
Array
(
[0] => income tax
)
The output is not exactly what I want. I need exactly only the values like audit, income tax only. Can any one help me?
audit
income tax
For starters, you should be using prepared statements to avoid SQL injection. After that, you can use either mysqli::bind_result
OR mysqli_stmt::get_result
and mysqli_result::fetch_array
. I'll demonstrate both methods here.
It is important to take these practices on board, you are using mysqli instead of mysql, so it makes sense to avail yourself of the enhanced features this library brings to bear.
First, preparing a statement:
// initiate database connection
$db = mysqli_connect(
"localhost",
"user",
"password",
"database"
);
// set up your statement with ? parameters
$statment = $db->prepare('
SELECT
t.tag_name
FROM
tag_map tm
JOIN
article p ON
p.post_id = tm.post_id
JOIN
tag t ON
t.tag_id = tm.tag_id
WHERE
p.post_id = ?
');
// bind values for each parameter
$statement->bind_param('i', $post_id);
// execute the statement
$statement->execute();
The code above will safely prepare the query and fire it at the database, you've got some results to work with now. As mentioned, you can use two different methods. First is bind_result
, as follows:
// list the columns you want to get from each row as variables
$statement->bind_result($tagName);
// then loop the results and output the columns you bound above
while($statement->fetch()){
echo $tagName.'<br>';
}
The second method is to use get_result
, then loop those results and use fetch_array
to grab an associative array, from which you can get the column you're after, as follows:
$result = $statement->get_result();
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
echo $row['tag_name'].'<br>';
}
... in either case, use free_result
when you're done.
$statement->free_result();
Documentation
mysqli::prepare
- http://www.php.net/manual/en/mysqli.prepare.phpmysqli_stmt::get_result
- http://www.php.net/manual/en/mysqli-stmt.get-result.phpmysqli_stmt::bind_param
- http://www.php.net/manual/en/mysqli-stmt.bind-param.phpmysqli_stmt::bind_execute
- http://www.php.net/manual/en/mysqli-stmt.execute.phpmysqli_stmt::bind_result
- http://www.php.net/manual/en/mysqli-stmt.bind-result.phpmysqli_stmt::fetch
- http://www.php.net/manual/en/mysqli-stmt.fetch.phpmysqli_result::fetch_array
- http://www.php.net/manual/en/mysqli-result.fetch-array.php<?php
$con=mysqli_connect("localhost","user","password","database");
$post_id = '5';
$query= mysqli_query($con,"select t.tag_name from tag_map tm join article p on p.post_id = tm.post_id join tag t on t.tag_id = tm.tag_id where p.post_id = '$post_id'");
while($que = mysqli_fetch_row($query))
{
echo $que[0];
}
?>
Try with $que[0]:
while($que = mysqli_fetch_row($query))
{
echo "<pre>";
print_r($que[0]);
}
Try this,
<?php
while($que = mysqli_fetch_row($query))
{
echo "<pre>";
echo $que[0];
}
?>
If you just want to get the result as an assoc array and don't want to use bind_param etc. you could just use php function array_column
with the specific column name on your result:
$result = array_column($connection->query('SELECT xy FROM table')->fetch_all(MYSQLI_ASSOC), 'xy');