I'm trying to place the results of a SQL query into a table for my wordpress site.
My table is called "statetable"
The columns are "State", "num-Asn" , and "Percent"
The following is my code in my custom php template.
global $wpdb;
$results = $wpdb->get_results("SELECT * FROM statetable;");
echo "<table>";
foreach($results as $result){
echo "<tr>";
echo "<td>".$result->State."</td>";
echo "<td>".$result->num-ASN."</td>";
echo "<td>".$result->Percent."</td>";
echo "</tr>";
}
echo "</table>";
?>
I get an empty table with one cell filled with zeros. I think this might not be the best way to set it up, or I have the wrong syntax for the foreach loop.
"num-ASN" is an illegal identifier name. Check what's in $results.
Check the content of $results by using
var_dump($results);
Related
Being a beginner in PHP, I have a table in a database which consists of 14 columns. I need to extract some of the columns through an 'href' link on another page. I am having a hard time trying to get the specific column ids for the specific column as I need the columns to be displayed as plain text separately on an html page.
So this is the fetch code to display columns 'A' and 'G' including two links in a table.
while($row=mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['A'] . "</td>";
echo "<td>" . $row['G'] . "</td>";
echo "<td>FASTA</td>";
echo "<td>Full Entry</td>";
echo "</tr>";
}
I am facing problems to get the columns A to M separately on the next php page of FullEntry.php and I need the data from the columns as plain text.
This is what I could come up with on FullEntry.php page.
$row = $_GET['rowid'];
<!--Here is where I need the multiple row ID's to get separate columnar data-->
echo $row;
?>
So How can use different id's for different columns from the original.php page to display results separately through the FullEntry.php page or If there can be any modifications with sql query as well. Help will be appreciated.
Any help would be appreciated.
Thank you in advance!
HereI have added a delimiter | bewteen $row reults like
echo "<td>Full Entry</td>";
And the result will be like Fullentry.php?rowid=a|b|c|d|e..... and you can access this by exploding the rowid.
$result = $_POST['rowid];
$result = explode("|",$result);
echo $result [0];
echo $result [1];...
I am trying to display html table from a stored procedure using PDO.Resultset contains lot of columns and rows which needs to be exported to an excel.
But I need to get the column names to be on the first row, how can be possible on the below code? Any help ?
$q= $db->prepare( "CALL spalldetails" );
$q->execute();
while ($arrValues = $q->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
foreach ($arrValues as $key=>$value)
{
$column_names[]=$key;
?>
<td><?=$value?></td>
<?
}
echo "</tr>";
}
The below code displays the column name, but I want it to be the top row of the table.
echo "<tr>";
foreach ($column_names as $name)
{ ?>
<td>
<?=$name?>
</td>
<?
}
echo "</tr>";
?>
The question has nothing to do with stored procedures, as it's applicable to any data returned from database.
the most convenient way would be to fetch the data first, get the column names from the first row and then print the data.
besides, use of prepare execute is not justified for this query
$data = $db->query( "CALL spalldetails" )->fetchAll(PDO::FETCH_ASSOC);
$column_names = array_keys($data[0]);
here you have your column names before output which you can make using foreach() instead of while()
I would suggest instead of printing the output immediately while processing the through the for loop, you rather create a string to output later. This can be accomplished simply like this: (mind you the below is untested air code, but to show you an example of what I mean)
$q= $db->prepare( "CALL spalldetails" );
$q->execute();
while ($arrValues = $q->fetch(PDO::FETCH_ASSOC))
{
$tablestring .= "<tr>";
foreach ($arrValues as $key=>$value){
$column_names[]=$key;
$tablestring .= "<td>".$value."</td>";
}
$tablestring .= "</tr>";
}
$headstring .= "<tr>";
foreach ($column_names as $name)
{ $headstring .= "<td>".$name."</td>";
}
$headstring .= "</tr>";
echo "<table>".$headstring.$tablestring."</table>";
I am trying to display results from a database onto my site. I have one entry in the database, and the result of that entry should be a "1". However, when I run the following code, it returns "1111111111111..." and continues to load ones. If there are 4 entries, I want the results to be displayed like:
1
2
3
4
I have looked around on this forum and others but cannot find anywhere where somebody had the same problem, and my code looks similar to the others. What did I do wrong?
functions.php:
$sqlgroup = mysql_query ("SELECT groupid FROM groups");
$grouprow = mysql_fetch_array ($sqlgroup);
Settings.php:
<?php while( ($row = $grouprow))
{
echo "<tr>";
echo "<td>".$row['groupid']."</td>";
echo "</tr>";
} ?>
First of all don't use mysql, use mysqli isntead:
$sqlgroup = mysqli_query ("SELECT groupid FROM groups");
while($row = mysqli_fetch_array($sqlgroup))
{
echo "<tr>";
echo "<td>".$row['groupid']."</td>";
echo "</tr>";
}
Im currently making a private "list management" system in which I store SQL queries in the database. So that I can via the front-end create new "lists" (which basicly are sql queries), and view them.
I have made the front end so you can save queries into the database, and im at the point where I want PHP execute and print out the results of one of my queries. This happens when I select one of my stored "lists" on my frontend. So when I press one of the lists, it should execute the SQL query. So far, so good.
But how can I, via PHP, print a table (like the one you get out from phpMyAdmin when viewing the contents of a table) without knowing how many / what columns exists? I want the script to be dynamic, so I can view results of all kinds of SELECT queries (on different tables).
Any tips or pointers?
Rather than using deprecated libraries, use PDO instead.
$db = new PDO($dsn); //$dsn is the database connection strings. Depends on your DB.
//it can be as simple as "odbc:CONN_NAME"
$stmt = $db->prepare("SELECT * FROM $tablename");
//be sure to sanitize $tablename! use a whitelist filter, not escapes!
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC); //fetch as associative array
if($rows){
//check if there are actual rows!
$first_row = reset($rows); //Resets the internal pointer, return the first elem.
$header_str = '<th>' . implode('</th><th>', array_keys($first_row)) . '</th>';
$table_body_rows = array();
foreach($rows as $row){
$table_body_rows[] = '<td>' .
implode('</td><td>', $row) .
'</td>';
}
$body_str = '<tr>' . implode('</tr><tr>', $table_body_rows) . '</tr>';
$tbl = "<table><thead><tr>$header_str</tr></thead><tbody>$body_str</tbody></table>";
} else {
//something went wrong
}
show tables is probably what you need
echo "<table><tr>";
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)) {
echo "<td> $row[0] </td>";
}
echo "</tr></table>"
mysql_free_result($result);
If you need to print a row with header (column names), you have to do it this way:
$result=mysql_query("SELECT * FROM yourtable WHERE 1");
if (mysql_num_rows($result)<1) echo "Table is empty";
else
{
$row=mysql_fetch_assoc($result);
echo "<table>";
echo "<tr>";
echo "<th>".join("</th><th>",array_keys($row))."</th>";
echo "</tr>";
while ($row)
{
echo "<tr>";
echo "<td>".join("</td><td>",$row)."</td>";
echo "</tr>";
$row=mysql_fetch_assoc($result);
}
echo "</table>";
}
This is just the basic concept. If your table has values which may contain HTML tags and other stuff, you'll need to apply htmlspecialchars() on all values of $row. This can be done with array_walk(). Furthermore you didn't mention what PHP version are you using and what MySQL API do you prefer. Some people suggested to use mysqli or PDO, that's up to you to rewrite the code according to your preferred API.
I have created a table of empty cells as follows
$sql1 = mysqli_query($mysqli,"SELECT * FROM production WHERE date(productiondate)='".$today."' AND productionline=1");
echo "<tr>";
echo "<td>Row #1</td>";
for($i=8;$i<=20;$i++)
{
echo "<td id=".$i."></td>";
} echo "<td></td>";
Now I want to insert values into specific cells according to their ids, but unable to do so since the following function will insert them serially.
while($row=mysqli_fetch_array($sql1))
{
echo "<td>".$row['productionquantity']."</td>";
}
echo "</tr>";
Any help will be greatly appreciated. And please let me know if any other snippets of my code are required to get a better understanding of my motive.
Do you have any $sql2 variable?
Because your mysqli_fetch_array() gets an "$sql2 parameter.