I am trying to display data from mysql using php. Please find the code which I have used. I am only getting the field names in the output. The referred table in the query containing more than 50 rows.
Please help to solve my issue...
<?php
$con=mysqli_connect("localhost","root","password","ayrilmana");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT txn_amount,donated_by FROM tempe_txn");
echo "<table border='1'>
<tr>
<th>txn_amount</th>
<th>donated_by</th>
</tr>";
if($result === FALSE){
die(mysql_error());
}
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['txn_amount'] . "</td>";
echo "<td>" . $row['donated_by'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Related
I am working on a website whereby a load of advertisers are stored in the DB and then displayed to the user by there logo. I know storing directly in to the DB for images is not the done thing, however, I am starting out this way, to get the website running and then will refactor to move to a much more suitable approach.
Currently, I have the following PHP code:
<?php
session_start();
require_once "config.php";
// Create connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT * FROM advertisers";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>advertiser_Name</th>";
echo "<th>advertiser_URL</th>";
echo "<th>advertiser_Category</th>";
echo "<th>advertiser_logo</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['advertiser_id'] . "</td>";
echo "<td>" . $row['advertiser_Name'] . "</td>";
echo "<td>" . $row['advertiser_URL'] . "</td>";
echo "<td>" . $row['advertiser_Category'] . "</td>";
echo "<td>" . $row['<img src="data:image/jpeg;base64,'.base64_encode($row['advertiser_logo']).'"/>'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
However, the images are displayed when called from the DB but they are displayed in the warning message rather than in the table?
<?php
session_start();
require_once "config.php";
// Create connection
if ($link === false)
{
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT * FROM advertisers";
if ($result = mysqli_query($link, $sql))
{
if (mysqli_num_rows($result) > 0)
{
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>advertiser_Name</th>";
echo "<th>advertiser_URL</th>";
echo "<th>advertiser_Category</th>";
echo "<th>advertiser_logo</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['advertiser_id'] . "</td>";
echo "<td>" . $row['advertiser_Name'] . "</td>";
echo "<td>" . $row['advertiser_URL'] . "</td>";
echo "<td>" . $row['advertiser_Category'] . "</td>";
echo "<td><img src='data:image/jpeg;base64," . base64_encode($row['advertiser_logo']) . "'/></td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
}
else
{
echo "No records matching your query were found.";
}
}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
The fact that is showing the image in the warning is because you're using a tag with the source as an array key which is not correct.
The array keys, so what is inside the square bracket, is the reference to the array position. If you're familiar with C for example is the 0, 1, ecc.. and not the value itself.
Yes as #NigelRen mentioned this row $row['<img src="data:image/jpeg; looks very bad.
I think you should use:
echo "<td><img src='data:image/jpeg;base64," . base64_encode($row['advertiser_logo']) . "'/></td>";
//replace the following with your details. Dbname is your username by default.
$con = mysqli_connect("info20003db.eng.unimelb.edu.au","geehwank","2058","geehwank");
// Check connection
if (mysqli_connect_errno()) {
echo "Could not connect to MySQL for the following reason: " . mysqli_connect_error();
}
/* this lists the name and release date of all action movies */
echo "<table border='1'>";
$result = mysqli_query($con,"SELECT name, release_date FROM Movie WHERE genre = 'action'");
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td><td>" . $row['release_date'] . "</td>";
echo "</tr>";
}
mysqli_close($con);
This code is from my uni
and it reads in tables from mysql
and displays them in a table
im a php noob
ive been trying to add headnings to the table called
name, releasedate
how can i do this in code??
can anyone help??
Here you go:
echo "<table border='1'><thead><tr><th>Name</th><th>Release Date</th></tr></thead><tbody>";
$result = mysqli_query($con,"SELECT name, release_date FROM Movie WHERE genre = 'action'");
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td><td>" . $row['release_date'] . "</td>";
echo "</tr>";}
mysqli_close($con);
?>
</tbody></table>
Try as below :
<html>
<head><title>Lab F</title></head>
<body>
<h1> Lab F - SELECT Example</h1>
<p>
This PHP code runs an SQL query, and displays the answers in an HTML table.
</p>
<p>
<br>
<?php
//replace the following with your details. Dbname is your username by default.
$con = mysqli_connect("info20003db.eng.unimelb.edu.au","geehwank","2058","geehwank");
// Check connection
if (mysqli_connect_errno()) {
echo "Could not connect to MySQL for the following reason: " . mysqli_connect_error();
}
/* this lists the name and release date of all action movies */
echo "<table border='1'>";
$result = mysqli_query($con,"SELECT name, release_date FROM Movie WHERE genre = 'action'");
echo "<tr><td>Name</td><td>Release Date</td></tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td><td>" . $row['release_date'] . "</td>";
echo "</tr>";}
mysqli_close($con);
?>
</table>
</body>
</html>
I'm actually a little bit confused with all these AJAX techniques. The aim is to display the new rows on my site when new rows are inserted into my database, but i dont know how. The following code has been inserted into my Joomla site threw an extension:
<!-- You can place html anywhere within the source tags -->
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
$today = date("d/m/Y");
$sql = "SELECT * FROM queue WHERE";
$sql =$sql . " Date>='$today' ORDER BY Qnumber Desc LIMIT 3";
// echo $sql;
$con=mysqli_connect("localhost","root","db","pass");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Αριθμός Εξυπηρέτησης</th>
<th>Πελάτες σε Αναμονή</th>
<th>Ώρα</th>
<th>Ημερομηνία</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Qnumber'] . "</td>";
echo "<td>" . $row['WaitingCustomers'] . "</td>";
echo "<td>" . $row['Time'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I have a database IN MYSQL from where I wish to retrieve data and print it, how do I do it?
The columns in it are title and blogs. I wish to print the title content as the heading and the blog content in a paragraph.
To retrieve the data , use "select"
select title,blogs from table name
http://dev.mysql.com/doc/refman/5.6/en/select.html
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Use following links
http://www.w3schools.com/php/php_mysql_select.asp
Ok. Here.
<?php
$con=mysqli_connect("hostname","username","password","databasename");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT blogs, title FROM tableName");
while($row = mysqli_fetch_array($result)) {
echo "<h1>".$row['title']."</h1>";
echo "<p>".$row['blogs']."</p>";
}
mysqli_close($con);
?>
I need to filter a session table, normally this is how I show the table list.
<?php
$con=mysqli_connect("localhost","root","","esc");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM sponsor ;");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>PIN</th>
<th>Author</th>
<th>Author ID</th>
<th>Entry Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['sid'] . "</td>";
echo "<td>" . $row['spin'] . "</td>";
echo "<td>" . $row['spuname'] . "</td>";
echo "<td>" . $row['suid'] . "</td>";
echo "<td>" . $row['sdate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I was wondering if it's possible on this part...
$result = mysqli_query($con,"SELECT * FROM sponsor ;");
to add Where spuname = 'spuname'
I guess the way I'm doing it is wrong.
What I wanted to do is to show filtered tables, so the one that has the session will see only his logs.
mysqli_query("SELECT * FROM sponsor Where spuname = 'spuname'",$con);
try putting $con after your query
You can use where clause in your query. it would be
$result = mysqli_query($con,"SELECT * FROM sponsor Where spuname = 'spuname'");