Mysqli query are not fetching database values - php

I am trying with php 7.0 as everyone recommend me here to start with it as 5.6 has been depreveted . But after writing its code i am unable to fetch the values from database. what wrong i am doing here. thank you
//This is my aboutus page//
<div class="col-lg-10">
<?php include('config.php');
$query = mysqli_query('select * from about ');
while($row = mysqli_fetch_array($query)){
?>
<h5>
<?php echo $row['about_desc'] ;?>
<br>
<br>
<?php echo $row['about_desc1']; ?>
<br>
<br>
<?php echo $row['about_desc2'] ;?>
<br>
<br>
<?php echo $row['about_desc3'] ;?>
<br>
<br>
<?php echo $row['about_desc4']; ?>
</h5>
<hr class="featurrate-divider">
<?php
}
?>
</div>
</div>
<div id="tab-1">
<?php include('config.php ');
$query = mysqli_query('select * from news LIMIT 0,2');
while($row = mysqli_fetch_array($query)){
?>
<div class="news">
<h4><u><?php echo $row['news_name']; ?></u></h4>
<h5><font color="black"><?php echo $row['dat']; ?></font></h5>
<p><?php echo $row['news_desc']; ?></p>
se mer >>
</div>
<hr class="featurrate-divider">
<?php
}
?>
</div>
//This is my config.php file//
<?php
$connection = mysqli_connect('localhost','root','','dandelion') or die(mysqli_error($connection));
?>
Looking for good suggestion as a learner. thank you

Your About us page should be like this:
<body>
<div class="col-lg-10">
<?php include('config.php');
$query = mysqli_query($connection, 'select * from about ');
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
?>
<h5>
<?php echo $row['about_desc'] ;?>
<br>
<br>
<?php echo $row['about_desc1']; ?>
<br>
<br>
<?php echo $row['about_desc2'] ;?>
<br>
<br>
<?php echo $row['about_desc3'] ;?>
<br>
<br>
<?php echo $row['about_desc4']; ?>
</h5>
<hr class="featurrate-divider">
<?php
}
?>
</div>
</div>
<div id="tab-1">
<?php include('config.php ');
$query = mysqli_query($connection, 'select * from news LIMIT 0,2');
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
?>
<div class="news">
<h4><u><?php echo $row['news_name']; ?></u></h4>
<h5><font color="black"><?php echo $row['dat']; ?></font></h5>
<p><?php echo $row['news_desc']; ?></p>
se mer >>
</div>
<hr class="featurrate-divider">
<?php
}
?>
</div>
</body>

if your database connection is sucessfully work and problem with fetching data than use number.it may be useful to fetch the data.
<?php echo $row[o]; ?>
<?php echo $row[1]; ?>
instead of
<?php echo $row['about_desc1']; ?>

Related

Generate HTML code with PHP automatically, after data pull in MySql

I have written a really simple php page that populate a database.
I now want to fetch this data in another php page and that is ok.
What I would like to achive is:
when I add another row into the database, I would like the html to create a new card, and not add the information in the same card.
I am not sure I understand how this can be achived.
Do I have to use php templates like smarty or anybody can point me how could I proceed?
This is how it look when I add second raw:
While what i want to achive should look like
Here is the HTML code I use with the PHP code:
<section class="tm-section-3 tm-section-mb" id="tm-section-3">
<div class="row">
<div class="col-md-6 tm-mb-sm-4 tm-2col-l">
<div class="image">
<img src="img/tm-img-1.jpg" class="img-fluid" />
</div>
<div class="tm-box-3">
<h2>
<?php if (mysqli_num_rows($result) > 0) {
?>
<table>
<?php
include_once '../scripts/connection.php';
$result = mysqli_query($link,"SELECT
domain,subdomain,topic,topictitle,topictext FROM newpage");
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["domain"]; ?></td>
</tr>
<tr>
<td><?php echo $row["subdomain"]; ?></td>
</tr>
<tr>
<td><?php echo $row["topic"]; ?></td>
</tr>
<tr>
<td><h4><?php echo $row["topictitle"]; ?></h4></td>
</tr>
<tr>
<td><h5><?php echo $row["topictext"]; ?></h5></td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
}
else{
echo "No result found";
}
?>
</h2>
<p>
</p>
<div class="text-center">
Details
</div>
</div>
</div>
</div>
</section>
This is how i send the code to the db:
<?php
include("connection.php");
$domain = mysqli_real_escape_string($link, $_POST['domain']);
$subdomain = mysqli_real_escape_string($link, $_POST['subdomain']);
$topic = mysqli_real_escape_string($link, $_POST['topic']);
$topictitle = mysqli_real_escape_string($link, $_POST['topictitle']);
$topictext = mysqli_real_escape_string($link, $_POST['topictext']);
$sql = "INSERT INTO newpage (domain,subdomain,topic,topictitle,topictext) VALUES ('$domain','$subdomain','$topic','$topictitle','$topictext')";
$result = mysqli_query($link, $sql);
// if query fails stop script and echo error
if( $result === false)
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
exit;
}
$sql = "INSERT INTO menu (item) VALUES ('$domain')";
$result = mysqli_query($link, $sql);
// if query fails stop script and echo error
if( $result === false)
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
exit;
}
header("location:../scripts/add-new-page-script-end.php");
exit;
echo "You'll never see this";
?>
Here the code that works even the style is bad. But logically is correct:
<div class="col-md-6 tm-mb-sm-4 tm-2col-l">
<?php
include_once '../scripts/connection.php'; $result = mysqli_query($link,"SELECT domain,subdomain,topic,topictitle,topictext FROM newpage"); foreach($result as $row){
?>
<div class="image">
<img src="img/tm-img-1.jpg" class="img-fluid" />
</div>
<div class="tm-box-3">
<h1><?php echo $row['domain']; ?></h1>
<h2><?php echo $row['subdomain']; ?></h2>
<h3><span><?php echo $row['topic']; ?></span></h3>
<h4> <span><?php echo $row['topictitle']; ?></span></h4>
<p><?php echo $row['topictext']; ?></p>
<div class="text-center">
Details
</div>
</div>
<?php
}
?>
</div>
It currently looks like you have something like this:
<div class="card">
<img src="..." />
<?php
foreach($result as $row){
?>
<h1><?php echo $row['domain-name']; ?></h1>
<h2><?php echo $row['sub-domain-name']; ?></h2>
<span><?php echo $row['topic-text-title']; ?></span>
<p><?php echo $row['text-of-topic']; ?></p>
<?php
}
?>
<button>Details</button>
</div>
If you instead put the foreach loop outside of the card div then it will make a new card for each result, something like this:
<?php
foreach($result as $row){
?>
<div class="card">
<img src="..." />
<h1><?php echo $row['domain-name']; ?></h1>
<h2><?php echo $row['sub-domain-name']; ?></h2>
<span><?php echo $row['topic-text-title']; ?></span>
<p><?php echo $row['text-of-topic']; ?></p>
<button>Details</button>
</div>
<?php
}
?>
Assuming that your are not using any framework.
In raw php context you could do something like this:
<div>
<?php foreach($arr as $item): ?>
<div>
<img src="...">
<h1><?php echo $item->domain_name; ?></h1>
<h2><?php echo $item->subdomain_name; ?></h2>
<h3><?php echo $item->topic; ?></h3>
<h4><?php echo $item->topic_text_title; ?></h4>
<h5><?php echo $item->text_pf_topic; ?></h5>
</div>
<?php endforeach; ?>
</div>

PHP - Query and loop to show different data

I'm struggling to show different news in my page. I want to show each of the news in 1 div and the limit is 4 news. Example, i have 4 different news and i want of every each of it to display separately in divs. Im new to html and php. Can someone give me ideas what the best loop and queries for this?
to easily understand what i want to do here is the picture.
here is what i im doing. i think im lost.
here is my php code for the news..
<div class="content">
<div class="container">
<div class="row">
<div class="col-xs-12 col-md-9">
<div class="fix leftbar floatleft">
<div class="fix left_sidebar">
<div class="news">
<h2><i class="fa fa-newspaper-o"></i> Latest News</h2>
<hr class="carved">
<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)) {
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>
<hr>
<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>
<hr>
<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>
<hr>
<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>
View More News
</div>
</div>
</div>
</div>
Simply like your first loop... with better format
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<div class="fix single_news">
<?php echo $title; ?>
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<hr class="carved"/>
<?php
}
?>
Your loop is taking all article, so you need only one loop like this.
Inside you just need to format only one article, all others will take the same format.
You are looping the output over and over again, you only need the single loop to get the data from the database.
Note this is untested:
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
echo '<div class="fix single_news">';
echo '<div class="single_image">';
echo '<img src="img/coveredcourt.jpg" alt="court">';
echo '</div>';
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<hr>
<?php
}
?>
</div>
View More News

PHP how to store words of a sentence into a string without explode

I'm making a php tag script for my website.
I have completed the code, but I'm afraid my code is too long.
Can my code be more simple and short?
<?php
include("admin/apps/site-settings.php"); // database connection
$albumq = mysql_query("select * from albums order by rand() limit 20");
while($album = mysql_fetch_array($albumq)){
$name_a = str_replace("&apos;s","",$album['name']);
$name_b = str_replace(""","",$name_a);
$name_c = str_word_count($name_b, 1);
?>
<?php if(!empty($name_c[0])){?>
<a href="search-<?php echo $name_c[0];?>.html">
<div class="tag">
<?php echo $name_c[0];?>
</div>
</a>
<?php }?>
<?php if(!empty($name_c[1])){?>
<a href="search-<?php echo $name_c[1];?>.html">
<div class="tag">
<?php echo $name_c[1];?>
</div>
</a>
<?php }?>
<?php if(!empty($name_c[2])){?>
<a href="search-<?php echo $name_c[2];?>.html">
<div class="tag">
<?php echo $name_c[2];?>
</div>
</a>
<?php }?>
<?php if(!empty($name_c[3])){?>
<a href="search-<?php echo $name_c[3];?>.html">
<div class="tag">
<?php echo $name_c[3];?>
</div>
</a>
<?php }?>
<?php if(!empty($name_c[4])){?>
<a href="search-<?php echo $name_c[4];?>.html">
<div class="tag">
<?php echo $name_c[4];?>
</div>
</a>
<?php }?>
<?php if(!empty($name_c[5])){?>
<a href="search-<?php echo $name_c[5];?>.html">
<div class="tag">
<?php echo $name_c[5];?>
</div>
</a>
<?php }?>
Output in the website:
I'm running it on localhost so I can't provide a link at the moment.
<?php
include("admin/apps/site-settings.php"); // database connection
$albumq = mysql_query("select * from albums order by rand() limit 20");
while($album = mysql_fetch_array($albumq)){
$name_a = str_replace("&apos;s","",$album['name']);
$name_b = str_replace(""","",$name_a);
$name_c = str_word_count($name_b, 1);
foreach ($name_c as $value)
{
if (!empty($value))
{
echo "
<a href='search-{$value}.html'>
<div class='tag'>
{$value}
</div>
</a>";
}
}
}
?>
Use foreach loop for same , use like
if(count($name_c) > 0)
{
foreach($name_c as $name)
{
?>
<a href="search-<?php echo $name; ?>.html">
<div class="tag">
<?php echo $name; ?>
</div>
</a>
<?php
}
}

Bootstrap Data-toggle not working on PHP using a "while "

I have a problem whith my " Bootstrap Collapse" . My query display only the fist $ID_Producto of all my rows.
<?php
$Consulta_Productos = $Conexion ->query( "SELECT * FROM productos ORDER BY Producto_Categoria");
while($Fila_Productos = $Consulta_Productos->fetch_assoc())
{
$ID_Producto = $Fila_Productos['ID_Producto'];
?>
<span data-toggle="collapse" data-target="#demo">Show ID <?php echo $ID_Producto ?></span>
<div id="demo" class="collapse">
<?php echo $ID_Producto; ?>
</div>
<br>
<?php
}
?>
your data-target="#demo" and id="demo" are the same for all the products.
try this
<?php
$Consulta_Productos = $Conexion ->query( "SELECT * FROM productos ORDER BY Producto_Categoria");
while($Fila_Productos = $Consulta_Productos->fetch_assoc())
{
$ID_Producto = $Fila_Productos['ID_Producto'];
?>
<span data-toggle="collapse" data-target="#demo<?php echo $ID_Producto; ?>">Show ID <?php echo $ID_Producto ?></span>
<div id="demo<?php echo $ID_Producto; ?>" class="collapse">
<?php echo $ID_Producto; ?>
</div>
<br>
<?php
}
?>

Showing fields from a query in php

I'm new in PHP and i'm having a trouble in showing the retrived data from a query. There area four area_name and many type_name, type_desc and type_price under that area_name
I wanted to display those data in a format like this:
area_name1
type_name1_1
type_desc1_1
type_price1_1
type_name1_2
type_desc1_2
type_price1_2
then
area_name2
type_name2_1
type_desc2_1
type_price2_1
type_name2_2
type_desc2_2
type_price2_2
But in my code, it is displayed like this:
area_name1
type_name1_1
type_desc1_1
type_price1_1
area_name1
type_name1_2
type_desc1_2
type_price1_2
then
area_name2
type_name2_1
type_desc2_1
type_price2_1
area_name2
type_name2_2
type_desc2_2
type_price2_2
Can anyone help me on how to show the area_name once then show the type_names under that specific area_name? any help/assistance will be greatly appreciated.
<?php
include 'config.php';
$query = "select a.area_name,t.type_no,t.type_name,t.type_desc,t.type_price,t.area_no, a.area_no from area as a INNER JOIN type as t where a.area_no=t.area_no";
$stmt= dbConnect()->prepare($query);
$stmt -> execute();
$text_num = $stmt -> rowCount();
?>
<div id="prices_content">
<div>
<?php if ($text_num>0) {?>
<?php while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {?>
<div id="content">
<br><br><br><center>
<b><h1><?php echo $row['area_name']; ?></h1></b><br>
<p><?php echo $row['type_name']; ?></p><br>
<p><?php echo $row['type_desc']; ?></p><br>
<p><?php echo $row['type_price']; ?></p></center><br>
</div>
<?php }?>
<?php }?>
</div>
</div>
In your while loop you're echoing $row['area_name'] every time. You need to declare a variable outside the loop to hold the area name and check it in each loop to see if it changed.
Like this:
$area_name = '';
<?php while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {?>
<div id="content">
<br><br><br><center>
<?php if($area_name != $row['area_name']){ ?>
<?php $area_name = $row['area_name']; ?>
<b><h1><?php echo $row['area_name']; ?></h1></b><br>
<?php } ?>
<p><?php echo $row['type_name']; ?></p><br>
<p><?php echo $row['type_desc']; ?></p><br>
<p><?php echo $row['type_price']; ?></p></center><br>
</div>
<?php } ?>
Try this Code,
<?php
include 'config.php';
$query = "select a.area_name,t.type_no,t.type_name,t.type_desc,t.type_price,t.area_no, a.area_no from area as a INNER JOIN type as t where a.area_no=t.area_no";
$stmt= dbConnect()->prepare($query);
$stmt -> execute();
$text_num = $stmt -> rowCount();
?>
<div id="prices_content">
<div>
<?php if ($text_num>0) {?>
<?php while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {?>
<div id="content">
<br><br><br><center>
<?if($area_name !== $row['area_name']){?>
<b><h1><?php echo $row['area_name']; ?></h1></b><br>
<?php }?>
<p><?php echo $row['type_name']; ?></p><br>
<p><?php echo $row['type_desc']; ?></p><br>
<p><?php echo $row['type_price']; ?></p></center><br>
</div>
<?php $area_name = $row['area_name'];}?>
<?php }?>
</div>
</div>
You are adding the area_name into the loop so it will be displayed
you should try this
<div id="prices_content">
<div>
<?php if ($text_num>0) {?>
<b><h1><?php echo $row['area_name']; ?></h1></b><br><!-- this outside of the loop-->
<?php while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {?>
<div id="content">
<br><br><br><center>
<p><?php echo $row['type_name']; ?></p><br>
<p><?php echo $row['type_desc']; ?></p><br>
<p><?php echo $row['type_price']; ?></p></center><br>
</div>
<?php }?>
<?php }?>
</div>

Categories