So I have been looking for ways to display data from a database. However, they all require a loop and I do not want a loop as I only have 1 row in this table.
I came across mysqli_fetch_row() but I am not sure how to implement this. I am starting to learn PHP and MySQL and so any help is appreciated! This is what I have so far...
$displayIntro = mysqli_query($connection,"SELECT * FROM Introduction");
$displayTitle = mysqli_fetch_row($displayIntro);
echo $displayTitle['Title'];
echo $displayTitle['Description'];
Also after displaying the plain text, how can I format it with HTML? For example, the title will need to be enclosed in <h1></h1> and the subscription in paragraph <p></p>.
Much thanks to any answers!
The problem is mysqli_fetch_row returns enumerated results, array with numeric indexes, so this should be like:
$displayIntro = mysqli_query($connection,"SELECT `Title`,`Description` FROM Introduction");
$displayTitle = mysqli_fetch_row($displayIntro);
echo $displayTitle[0]; // assuming column 'Title' is first row
echo $displayTitle[1]; // assuming column 'Description' is second row
What you should use here is mysqli_fetch_assoc to fetch a result row as an associative array:
$displayIntro = mysqli_query($connection,"SELECT `Title`,`Description` FROM Introduction");
$displayTitle = mysqli_fetch_assoc($displayIntro);
echo $displayTitle['Title'];
echo $displayTitle['Description'];
Use code from #Maximus2012 answer to form html row. Also to get only one row from table with more than one records you can just add LIMIT 1 at the end of the MySQL query like this:
"SELECT `Title`,`Description` FROM Introduction LIMIT 1"
Hope this helps :)
From PHP manual entry for mysqli_fetch_row (link):
"Fetches one row of data from the result set and returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero)." The function returns an enumerated array, not associative array.
Untested, but I would expect this to work:
echo $displayTitle[0];
echo $displayTitle[1];
$displayIntro = mysqli_query($connection,"SELECT * FROM Introduction");
$displayTitle = mysqli_fetch_row($displayIntro);
echo "<html>";
echo "<body>";
echo "<h1>" . $displayTitle['Title'] . "</h1>";
echo "<p>" . $displayTitle['Description'] . "</p>";
echo "</body>";
echo "</html>";
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 have a table which contains the following columns..
And I stored the data of that table in a session variable
$sql = "SELECT * from `basic_info` where Username='".$user."' and Password='".$pass."'";
$sqlresult = mysqli_query($con,$sql) or die(mysqli_error($con));
$rowCount = mysqli_num_rows($sqlresult);
$currentData = mysqli_fetch_array($sqlresult);
if($rowCount > 0){
$_SESSION['currentUser'] = $currentData;
}
But whenever i echo the values of the array using below code:
<?php
echo "Welcome {$_SESSION['currentUser'][1]}";
echo "<div name='infoDisplay'>";
echo "<table>";
echo "<tr>";
echo "<td><b>Family Name</b><td>";
echo "<td><b>First Name</b><td>";
echo "<td><b>Middle Name</b><td>";
echo "<td><b>Birthday</b><td>";
echo "<td><b>Contact Number</b><td>";
echo "<td><b>Address</b><td>";
echo "<td><b>Username</b><td>";
echo "<td><b>Password</b><td>";
echo "<td><b>Permission Level</b><td>";
echo "<td></td>";
echo "</tr>";
echo "</table>";
echo "</div>";
echo "Array count: " . count($_SESSION['currentUser']) . "<br/>";
echo "<tr>";
for($rowcount=0; $rowcount<=count($_SESSION['currentUser']);$rowcount++){
echo "<td>". $_SESSION['currentUser'][$rowcount] . "</td>";
}
echo "</tr>";
?>
the array count is doubled. I only have 9 columns in my database but the result is returning 18 counts. Which why I'm also getting below error:
Please let me know your inputs on how can I easily resolve the issue. Thank you very much in advance! Have a great day!
As mentioned in the comments, mysqli_fetch_array returns an array with two elements for each column selected in the query: one element with a numeric key, and one element whose key is the column name. So if you select 9 columns, the array will have numeric keys from 0 to 8, and named keys FamilyName, FirstName, and so on.
The problem you're running into count($_SESSION['currentUser']) counts both of these elements, so it will return 18, not 9. Your for loop then uses this as the limit, so it will try to echo $_SESSION['currentUser'][9] through $_SESSION['currentUser'][17], which don't exist.
The solution to this is to use mysql_fetch_row() instead of mysql_fetch_array(). Or use mysql_fetch_assoc() and use a foreach loop instead of a for loop, which is my normal preference.
FROM THE MANUAL:
mysqli_fetch_array() is an extended version of the mysqli_fetch_row() function. In addition to storing the data in the numeric indices of the result array, the mysqli_fetch_array() function can also store the data in associative indices, using the field names of the result set as keys.
So for each row you return lets say you did select fname,lname from ...
You would get an array for each row like this:-
$row would be
[0 => 'fred', 'fname' => 'fred', 1 => 'Bloggs', 'lname' => 'Bloggs']
This is the doubling up of columns you are talking about
If you use
mysqli_fetch_array($sqlresult, MYSQLI_ASSOC);
or
mysqli_fetch_assoc($sqlresult);
You will only get the Associative array of column names like :
['fname' => 'fred', 'lname' => 'Bloggs']
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);
All I need to do is echo array pocket $row[0].
while ($row = mysqli_result::fetch_array($result)) {
echo '<br>' . $row[1];
}
echo $row[0];
I have googled, but results haven't helped. I know I'm missing something stupid, but I'd appreciate the help!
I'm trying to echo the database column, 'ID'. An example of the data I want to echo would be '3'.
Copy $row[0] as you go.
$lastZero = "";
while ($row = mysqli_result::fetch_array($result)) {
echo '<br>' . $row[1];
$lastZero = $row[0];
}
echo $lastZero;
For debugging the whole row, write this:
var_dump($row)
To output the column ID, write this:
while ($row = mysqli_result::fetch_array($result)) {
var_dump($row); // This will output all values for each row as they're looped through
echo '<br>' . $row['ID'];
}
var_dump($row); // dumps the last row of the returned results, or false if no rows.
echo $row['ID']; // echos the ID of the last row. However if there are no rows, this will not work.
Be aware, as #Daedalus pointed out, $row is an array, so the var_dump line is really just for debugging. Because an array has multiple values, PHP doesn't know how you want to display it. You'll need to program how each element of $row is displayed.
Hi below code prints results from first saved to last saved. Is there a way to get last saved on the top row once the data is displayed? (All records in the reverse order.) thanks.
while ($recz = mysql_fetch_array($runz))
{
echo "<tr class='taB'>";
echo "<td>".$recz["chkNum"]."</td>";
echo "<td>".$recz["InvoNum"]."</td>";
echo "<td>".$recz["InvoVal"]."</td>";
echo "<td>".$recz["InvoDate"]."</td>";
echo "<td>".$recz["type"]."</td>";
echo "<td class='ta'>".$recz["statu"]."</td>";
echo "</tr>";
}
You will need to change your query. Use ORDER BY ... DESC, where the ... is the column name that determines the order of the results.
In your HTML output, it looks like you have a column named InvoDate. Perhaps that's the column you need. If so, your query would end with ORDER BY InvoDate DESC.