How to get all values in a certain column with php - php

I need to print out all the usernames of my members in my site as a list, but I don't know how to do it. I tried to use 'mysqli_fetch_array' function to fetch names from username column, but I've been failing. So, please assist me on how I can do this in a most efficient way.
This is my code
$sql = "SELECT username FROM users";
$query = mysqli_query($db_con, $sql);
$array = Array;
while($row = mysqli_fetch_array($query)) {
$array = row[];
}
foreach ($names as $array) {
echo $names;
}
Also I want to format the output, like separating each username by " | "

You are quite close..
$sql = "SELECT username FROM users";
$query = mysqli_query($db_con, $sql);
while($row = mysqli_fetch_assoc($query)) {
echo $row['username'].'|';
}

Couple of things, "Array" should be "array()" and you're doubling up, by assigning array values into an array, results are returned in an array already. Code below should work...
$sql = "SELECT username FROM users";
$query = mysqli_query($db_con, $sql);
$array = mysqli_fetch_array($query));
foreach ($array as $name) {
echo $name["username"] . "|";
}
Looks like you also had your foreach loop round the wrong way

no need to use extra foreach, just do this way-
$sql = "SELECT username FROM users";
$query = mysqli_query($db_con, $sql);
$array = array();
while($row = mysqli_fetch_assoc($query)) {
echo $row["username"] . "|";
}

Don't worry it is really very easy
<?php
//All your preceding code
$sql = "SELECT username FROM users";
$query = mysqli_query($db_con, $sql);
?>
<ul>
<?php
while($row = mysqli_fetch_array($query)) {
echo "<li>" . $row['username'] . "</li>";
}
?>
</ul>

Related

How can i print $row array data into diferent parts of my webpage?

Usually when you do the basic php excercises you just use the following to print data on a blank page:
<?php
include('../includes/dbh.php');
$titulo = mysqli_real_escape_string($conn,$_GET['titulo']);
$sql = "SELECT * FROM proyectos WHERE proyect_name='$titulo'";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if($queryResults > 0){
while ($row = mysqli_fetch_assoc($result)){
echo "<div>".$row['proyect_name']. "
</div>";
}
}
?>
My question is, if I try to separate and echo out $row proyect_name in another part of the webpage nothing happens. I used both echo and var_dump and yes, the query works but it just doesn't print anything. I am new to this so yes, it may be obvious to you but not to me. I think it may be the while loop, but I do not know how to "blend it in".
<?php
include('../includes/dbh.php');
$titulo = mysqli_real_escape_string($conn,$_GET['titulo']);
$sql = "SELECT * FROM proyectos WHERE proyect_name='$titulo'";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
$data=array();
if($queryResults > 0){
while ($row = mysqli_fetch_assoc($result)){
echo "<div>".$row['proyect_name']. "
</div>";
$data[] = $row;
}
}
foreach($data as value) {
echo $value;
}
// OR
foreach($data as $key => $value) {
echo $key . " - " . $value;
}
?>
Set variable before loop then add $row data to it then loop it out with a foreach loop Does this work??
Use mysqli_fetch_array() - Example
So in your case it would be
<?php
include('../includes/dbh.php');
$titulo = mysqli_real_escape_string($conn,$_GET['titulo']);
$sql = "SELECT * FROM proyectos WHERE proyect_name='$titulo'";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if($queryResults > 0){
while ($row = $result->fetch_array()){
$rows[] = $row;
}
foreach ($rows as $row) {
echo "<div>".$row['proyect_name']."</div>";
}
}
?>

Nested Navigation

I want to make a Navigation with 2 levels.
My Code so far
<?php
$sql = ("SELECT name, id, pid FROM tl_table WHERE pid='' ORDER BY name");
$result = mysql_query($sql);
$list = array();
while ($row = mysql_fetch_assoc($result)) {
$list[] = $row;
}
foreach ($list as $kat) {
echo '<li>' . $kat['name'] . '</li>';
}
?>
Nested Sets are at the moment too tricky for me.
I want at the end this.
<li>$kat['name']
<li>$kat['name'] from PID</li>
</li>
MySQL:
http://i46.tinypic.com/35052m0.png - IMG
No I want to get the things our of the MySQL DB see the image Link.
MySQL:
id—–pid——name
1——0——–name1
2——0——–name2
3——0——–name3
4——3——–name3.1
5——3——–name3.2
<?php
$sql = ("SELECT name, id, pid FROM tl_table WHERE pid='' ORDER BY name");
$result = mysql_query($sql);
$list = array();
while ($row = mysql_fetch_assoc($result)) {
$list[$row['id']] = $row;
$sql = ("SELECT name, id, pid FROM tl_table WHERE pid='".$row['id']."' ORDER BY name");
$res = mysql_query($sql);
while($rw = mysql_fetch_assoc($res)){
$list[$row['id']]['sub'][] = $rw;
}
}
echo "<pre>";
print_r($list);
?>

remove last character

i noticed that there are a lot of topics like this in stackoverflow, but the ones that i read are with just 1 variable, and i have two and i'm a little confuse.
so this is my code,
$query = mysql_query("SELECT Provider.provider_id, provider.company_name FROM Provider");
while($row = mysql_fetch_array($query)){
echo "'".$row['provider_id']."':'".$row['company_name']."',";
}
I am using this to generate javascript code for Jeditable.
I need to take out the last comma, so I can wrap up with the rest of the code...
I like to use an array:
$query = ...;
$out = Array();
while($row = mysql_fetch_assoc($query)) {
$out[] = "'".$row['provider_id']."':'".$row['company_name']."'";
}
echo implode(",",$out);
It looks like you're trying to output some kind of JSON though, so perhaps you could use:
$query = ...;
$out = Array();
while($row = mysql_fetch_assoc($query)) {
$out[$row['provider_id']] = $row['company_name'];
}
echo json_encode($out);
So i usually just stack things like this into an array and then implode:
$parts = array();
while($row = mysql_fetch_array($query)){
$parts[] = "'".$row['provider_id']."':'".$row['company_name']."'";
}
echo implode(',', $parts);
That said it looks like youre outputting the body of a JSON string. If so you shouldnt manually build it you should use json_encode instead.
$data = array();
while($row = mysql_fetch_array($query)){
$data[$row['provider_id']] = $row['company_name'];
}
echo json_encode($data);
$query = mysql_query("SELECT Provider.provider_id, provider.company_name FROM Provider");
$result = "";
while($row = mysql_fetch_array($query)){
$result .= "'".$row['provider_id']."':'".$row['company_name']."',";
}
$result = rtrim($result, ',');
OR
$query = mysql_query("SELECT Provider.provider_id, provider.company_name FROM Provider");
$result = array;
while($row = mysql_fetch_array($query)){
$result[] = "'".$row['provider_id']."':'".$row['company_name']."',";
}
$result = implode(',' $result);
OR
get count of records (mysql_num_rows($query)) and when you hit last row just do what you want

Store Looped MySQL Array as String Variable

I have a collection of results from a MySQL query being fetched in a loop.
I need to store them as a single variable with a space in between each result.
$result = mysql_query("SELECT Names FROM table");
while($row = mysql_fetch_array($result)){
echo $row['Names'] . " ";
}
So later, I can call 'echo $Names;' and get the string of names with a space in between.
ex) Clinton Bush Huckabee Romney etc....
Thanks for your help!
$Names = '';
$result = mysql_query("SELECT Names FROM table");
while($row = mysql_fetch_array($result)){
$Names.= $row['Names'] . " ";
}
$result = mysql_query("SELECT Names FROM table");
$names = "";
while($row = mysql_fetch_array($result)){
$names .= $row['Names'] . " ";
}
echo $names;
First of all, please, stop using mysql extention. It's deprecated. Try mysqli or PDO.
$names = array();
$result = mysql_query("SELECT Names FROM table");
while($row = mysql_fetch_array($result)){
$names[] = $row['Names'];
}
echo implode(' ', $names);
I would use an array and then implode it using a space:
$result = mysql_query("SELECT Names FROM table");
$arr = array();
while($row = mysql_fetch_array($result)) $arr[] = $row['Names'];
echo implode(' ', $arr);

Set value of multidimensional array

I'm currently adding fileID to idarray[] and then do a foreach. But in addition to fileID I also need serverID and id in the array and the foreach, how do I do this? So basically I need to find out how to add 2 more values to the array and then also use those extra values in the foreach.
$result = mysql_query("SELECT * FROM redundfiles WHERE removed=1");
while($row = mysql_fetch_array($result))
{
$idarray[] = $row['fileID'];
}
foreach ($idarray as $value)
{
$result = mysql_query("SELECT * FROM redundfiles WHERE fileID=$value AND removed=0 AND serverID=$XXXserverIDhereXXX");
while($row = mysql_fetch_array($result))
{
$sql = "DELETE FROM redundfiles WHERE id='".$XXXidhereXXX."' ";
mysql_query($sql) or die(mysql_error());
}
}
Why not just store the whole row into an array?
$rowarray[] = array();
$result = mysql_query("SELECT * FROM redundfiles WHERE removed=1");
while($row = mysql_fetch_array($result))
{
$rowarray[] = $row;
}
foreach ($rowarray as $row)
{
$fileid = $row['fileID'];
$serverid = $row['serverID'];
$result = mysql_query("SELECT * FROM redundfiles WHERE fileID=$fileid AND removed=0 AND serverID=$serverid");
while($row = mysql_fetch_array($result))
{
$sql = "DELETE FROM redundfiles WHERE id='".$XXXidhereXXX."' ";
mysql_query($sql) or die(mysql_error());
}
}
But really, your whole code piece here is a mess. You can accomplish this way more efficiently using JOINs.

Categories