How to store table Field(column) name into An Array? - php

This is the my code and I am trying to store the value into the Array.
$result = $cid->query("SHOW COLUMNS FROM commissions");
echo $count = $result->num_rows;
while ($row = $result->fetch_assoc()) {
echo $row['Field'];
echo "<br/>";
}
I am trying to do this $ar[]=$row; but there is nothing display !!
$result = $cid->query("SHOW COLUMNS FROM commissions");
echo $count = $result->num_rows;
while ($row = $result->fetch_assoc()) {
ar[] = $row;
echo "<br/>";
}
for($i = 0, $i<12, $i++) {
echo ar['field'][0];
}
Nothing Display in the output

You can do following way to store column name into array. Read comment after every line
For PDO
$rs = $cid->query('SELECT * FROM commissions');// your query
for ($i = 0; $i < $rs->field_count(); $i++) {/// count nu of column
$col = $rs->fetch_fields()($i);//Returns metadata for a column in a result set
$columns[] = $col['name'];// get name from metedata
}
print_r($columns);
For mysqli
$rs = $cid->query('SELECT * FROM commissions');// your query
$col = $rs->fetch_fields();//Returns metadata for a column in a result set
foreach ($col as $val) {
$columns[]=$val->name;
}
print_r($columns);

Related

For loop missing first row from mysql DB PHP

I want to add data from phpmyadmin into a table format, but this code is missing the first row in the table and I don't understand why, I have tried other examples on SO such as
for ($i=0; $i< count($num); $i++)
But this did not work either.
Can someone see the issue?
Thanks
<?php
include('connect.php');
$con=mysqli_connect("localhost","******","*******","********");
$display = mysqli_query($con,"SELECT * FROM markers");
$num = mysqli_num_rows ($display);
$col = mysqli_num_fields ($display);
$row = mysqli_fetch_assoc($display);
$name = $row['name'];
?>
<?php
// start for loop
for ($i=0; $i<$num; $i++){
$row = mysqli_fetch_row($display); // fetching data
//echo results to table
echo "<tr data-name='$row[1]' data-address='$row[2]' data-lat='$row[3]' data-long='$row[4]'>";
for ($j=0; $j <$col; $j++){ // looping through each row of table
$test = $row [$j];
echo "<td test='$test'>" . $row [$j] . "</td>";
}
echo "</tr>";
}
mysqli_close($conn); // closing the connection
?>
Why do it that way?
I'd do a fetch array and loop that.
$result = mysqli_query($con,"SELECT * FROM markers");
while($row = mysqli_fetch_array($result))
{
echo "<tr data-name='$row['dataName']' data-address='$row['Address']' data-lat='$row['LAT']' data-long='$row['LONG']'>"
}
This would loop til you didn't have any more rows.

Associate each row of a table to a different variable

Actually with my code I print out all results together but the goal is to associate to a variable each row.
$sql = "SELECT modello FROM THING;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["modello"]; //this print all result but want to associate first result to variable $first and second to $second
}
} else {
echo "0 results";
}
Change echo $row["modello"]; to $modellos[] = $row["modello"]; like so in the example below:
$result = $conn->query("SELECT modello FROM THING");
$modellos = [];
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$modellos[] = $row["modello"];
}
} else {
echo "0 results";
}
Now $modellos has the first in $modellos[0] and the second in $modellos[1] and the third in $modellos[2] etc etc to do with as you wish after the while loop. If you really really need them in $first and $second, add after the loop:
$first = $modellos[0];
$second = $modellos[1];
You can use array to store your results.
$result = []
while($row = $result->fetch_assoc()) {
$result[] = $row["modello"];
}
But if you really need to associate each row to a variable, you can use:
$i = 0;
while($row = $result->fetch_assoc()) {
${"result" . $i} = $row["modello"];
$i++;
}

Making multiple mysqli queries using a for loop

I'm trying to make a series of mysqli queries by looping through the values of an array - (called column) - but nothing is being returned.
for ($i = 0; $i < sizeof($column); $i++)
{
$mutualInterests = mysqli_query ($conn, "SELECT USER_1 FROM INTERESTS WHERE answer = " . $column[$i] );
while ($row = mysqli_fetch_array ($mutualInterests))
{
echo " $row[USER_1]";
}
}
Just replace your given code with following and see result ..........
$query='SELECT USER_1 FROM INTERESTS WHERE answer ="'.$column[0].'"';
for ($i = 1; $i < sizeof($column); $i++)
{
$query.=' OR answer ="'.$column[$i].'"';
}
$mutualInterests = mysqli_query ($conn,$query);
while ($row = mysqli_fetch_array ($mutualInterests))
{
echo " $row[USER_1]";
}
Replace this:
while ($row = mysqli_fetch_array ($mutualInterests))
{
echo " $row[USER_1]";
}
with this:
$count = 0;
while ($row = mysqli_fetch_array ($mutualInterests))
{
echo $row[$count]."<br>";
$count++;
}
Let me know if it works now! :)

Delete database tables with php\mysql

I am trying to list all the tables in a database into an array and then proceed to delete the tables contents. What is wrong with my code?
<?php
mysql_connect('localhost', 'root','');
mysql_select_db(database_name);
$res = mysql_query("SHOW TABLES");
$tables = array();
while($row = mysql_fetch_array($res, MYSQL_NUM)) {
$tables[] = "$row[0]";
}
$length = count($tables);
for ($i = 1; $i < $length; $i++) {
$res = "DELETE FROM $tables[$i]";
mysql_query($res);
echo $res;
$i++;
}
?>
At the end of your for loop you don't need to do $i++; anymore, as this operation is already included in the for loop.
The code you have at the moment will delete the contents of one table and then skip a table, delete contents of one table again and skip one again...
Also $i should start with 0 as the first element in an array in php is element 0.
for ($i = 0; $i < $length; $i++) {
$res = "DELETE FROM $tables[$i]";
mysql_query($res);
echo $res;
}

how do I traverse rows in a table

I have a table named members with 3 rows. The following code attempts to display all the rows in the members table. It displays the 1'st record 3 times instead of displaying each record once.
<?php
$con = new mysqli("localhost", "root", "jce123", "profile");
if (mysqli_connect_errno()) {
printf("Connection failed: %s\n", mysqli_connect_error());
exit();
}
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
$array = mysqli_fetch_assoc($res);
$keysarray = array_keys($array);
$valuearray = array_values($array);
for ($i=0; $i < $num; $i++) {
for($j = 0; $j < count($array); $j++) {
echo "<table>";
echo "<tr><td>".$keysarray[$j].": </td><td>".$valuearray[$j]."</td></tr>";
echo "</table>";
}
echo "<br><br>";
}
mysqli_close($con);
?>
I've updated this per suggestions:
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
$array = mysqli_fetch_assoc($res);
while ($row = mysqli_fetch_assoc($res)) {
foreach($array as $key => $value){
echo "<table>";
echo "<tr><td>".$key.": </td><td>".$value."</td></tr>";
echo "</table>";
}
echo "<br><br>";
}
That's because, your $num = 3, count($array) = 3 and the outer for loop has no bearing on inner for loop.
Where you have $array = mysqli_fetch_assoc($res);, you will only receive one row from your database. You need something like:
while ($row=mysqli_fetch_assoc($res))
{
// processing for each row
}
Any basic tutorial will tell you this. Even the PHP docs provide an example.
you can use do something like this..
while ($row=mysqli_fetch_assoc($res))
{
// processing for each row
e.g
echo $row['id'];
echo $row['name'];
etc
}
try this... this peace of code is not tested but just to give you an idea...
$sql = "SELECT * FROM members";
$res = mysqli_query($con,$sql);
$num = mysqli_num_rows($res);
while ($row = mysqli_fetch_assoc($res)) {
echo "<table>";
echo '<tr><td> id = "'.$row['id'].'":
</td><td> name = "'.$row['name'].'"</td></tr>";
echo "</table>";
}

Categories