PHP: Displaying table data using fetchall() - php

Quick PHP/MySQL question:
I'm trying to display the data from a MySQL database table as an HTML table, and for some reason my code doubling the output of each piece of data.
This is my code:
$rowarray = $statement->fetchall();
print "<tr>\n";
foreach ($rowarray as $row) {
foreach ($row as $col) {
print "\t<td>$col</td>\n";
}
print "</tr>\n";
}
My results look something similar to this:
User ID | User Name | First Name | Last Name
1 1 User Name User Name First Name First Name Last Name Last Name
Etc etc. You get the idea. Why this is happening? By the way, if I manually add the column information by referring to row[] subscripts 0-3, everything is displayed properly; it's only when I use the nested foreach statements that the data is duplicated.

You are getting both a numeric and a name-indexed value back from PDO fetchall. To get just the numeric index:
$rowarray = $statement->fetchall(PDO::FETCH_NUM);

Related

making combinations of data in while loop

I have two tables. First one contains first names, second one contains last names. (I could do it with one table with two columnes, but I'm using this way for other reason)
I want to make all possible combinations.
So if there are first names: John, Zed, Marko, and last names: Abbot, Zang I would like to get output like:
JohnAbbot
JohnZagn
ZedAbbot
ZedZang
MarkoAbbot
MarkoZang
I did something similar with For loop and alfanumeric signs. I was able to do it with 6 nested for loops, but I can't get this to work with while loop.
this is the code I use:
$query_name = "SELECT name FROM names";
$results=mysqli_query($connect, $query_name);
$query_surname = "SELECT surname FROM surnames";
$results2=mysqli_query($connect, $query_surname);
while ($row = mysqli_fetch_assoc($results)) {
while ($row2 = mysqli_fetch_assoc($results2)) {
echo $row['name'].$row2['surname']."<br/>";
}
}
with this I got only combinations with 1 name and all surnames.
after I added one more echo in first while loop like this:
while ($row = mysqli_fetch_assoc($results)) {
echo $row['name']."<br/>";
while ($row2 = mysqli_fetch_assoc($results2)) {
echo $row['name'].$row2['surname']."<br/>";
}
}
with this I got:
first name from names table
combinations with first name and all last names (surnames)
all others names
I did not get all the combinations with all names and surnames.
What am I doing wrong?
Thank you in advance
(sorry for my bad english)
That's because fetching is only done once per handle, so when your inner loop reaches the end of records, it will never go back to the first one - as you would need it to - but stay at the end and return false.
If your result sets are not million-row ones, try fetching both only once, then combine the arrays and foreach-loops which will obediently start from the beginning each time:
$a1 = array();while ($row = mysqli_fetch_assoc($results)) $a1[]=$row;
$a2 = array();while ($row = mysqli_fetch_assoc($results)) $a2[]=$row;
foreach($a1 as $row1) {
foreach($a2 as $row2) {
echo $row1['name'].$row2['surname']."<br/>";
}
}
You can do with a simple query
select a.name, b.surname
from names as a
FULL JOIN surnames as b

How to get the column name of the cell which returned by result::fetch_row if I use foreach on it?

I have created a simple php code to print the result of a mysqli query no matter what query it is and how many columns and rows in there. My simplified php code is:
$result = $mysqli->query ($query);
while ($row = $result->fetch_row()) {
if ($row["status"] == "0") continue;
foreach ($row as $cell) {
echo $cell;
}
echo "\n";
}
Now I want to omit some column (ex: column named "status") to be printed, but I have to include the column "status" in the query because I need to check this "status" value to determine whether if the entire row will be printed or not (the check is a little more complicated than that and it's impractical to do it on the query itself). But if the row is printed, I don't want the column "status" is printed along in the table. But I have no means to know whether the $cell I get inside foreach is named "status" or not, and I have several other columns that have similar value like "status" so I can't check based on value either. How can I do this? I've read on php mysqli::fetch_row() manual but it doesn't seem that each of the $cell is an object that can be applied method to ask what's its column name. Thanks.
So only select the rows with status !=0
SELECT a,b,c FROM user WHERE status!=0;
Update:
unset($row["status"]);
var_dump($row);

get values from mysql column in php array

I've a database with several columns. One of the columns is 'name' and stores the name of the companies in the database. Another column ('id') assign a unique id number to each company. The table has a minimum of 1 and a maximum of 4 companies.
I'd like to show the name of the companies in different places in a html document and therefore need to refer to $company_name_1, $company_name_2 etc. However, these variables should always be in the html document, irrespective of whether the companies are in the database. If not in the database, the value shown should be empty.
How can I automatically define the names of the companies with an array and a loop? I want to expand the database at a later stage hence manually defining the four company names is not an option.
Thanks!
$result = mysql_query($con, "SELECT name FROM companydetails");
$storeArray = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$storeArray[] = $row['name'];
}
And then I would like to use:
echo $storeArray[0];
to show the first company and
echo $storeArray[1];
to show the second company. Etc.
Before echo first check if the element exists in array. This way there will be no error, something like this
if(count($storeArray)>1)
echo $storeArray[1];
else echo "NA";
if(count($storeArray)>2)
echo $storeArray[2];
else echo "NA";

Looping through resultings record and getting duplicates in foreach

So I am trying what I feel is a simply loop through a record result. My query returns one row of data. I am attempting to simply pull the value of each row from the returning record into a variable called $questions. However, my var $questions when printed out has duplicates in every space. It should read something like Bob|Ted|Joe|Sally and instead it is reading Bob|Bob|Ted|Ted|Joe|Joe|Sally|Sally. Why is the code below running twice in the foreach loop?
while ($row = mssql_fetch_array($result)){
foreach ($row as $col => $value) {
$questions.=$value."|";
}
}
echo "Questions: ".$questions."<br/>";
Here is all you need to do:
$questions = "";
while ($row = mssql_fetch_array($result)){
$questions .= $row['fieldName']."|";
}
echo "Questions: ".$questions."<br/>";
The foreach() in addition to the while() is unnecessary.
The mssql_fetch_array according to PHP doc:
In addition to storing the data in the numeric indices of the result
array, it also stores the data in associative indices, using the field
names as keys.
The result includes a normal array [0...n] with one element for each column, but also an associative array where each value is represented by a key named after the column name.
So if your first column is Id, you could get id from a row in two ways:
$id = $row[0];
# or you could do this
$id = $row['Id'];
This is why you get each value twice when looping through row.

Displaying Values of MYSQL Result via PHP

OK, this is an amateur question but I am having the most trouble displaying the values of my database. I want to display the entire 'phone' column so my mysql query is this:
$result = mysql_query('SELECT phone FROM contactList WHERE phone != 1') or die(mysql_error());
$row = mysql_fetch_array($result)
My PHP script is as this:
while(isset($rows))
{
echo $rows['phone'] . "html break tag";
}
It looks like though I'm only getting the first result instead of looping through the entire column.
Do I need to increment the ID in my loop and get value via ID? The only problem with that is my auto-incremental ID column starts # 130 and skips some numbers here and there.
try this instead
While($row = mysql_fetch_array($result))
echo $row['phone'];
That should help you looping through the whole array.

Categories