I'm working on one site where I am trying to connect to an SQL database and post a SQL field inside a <div>. The <div> is inside a while loop.
I think I have connected successfully to the database. But, I'm unable to show id field in the header. I am newbie to PHP & SQL and can't figure it out. Here is my code below:
<?php
$db_host = "localhost";
$db_username = "user192";
$db_pass = "xxxx";
$db_name = "sound-library";
#mysql_connect("$db_host","$db_username","$db_pass") or die ("couldnt connect to sql");
#mysql_select_db ("$db_name") or die ("cant find database");
$query = "select * from sound-library";
$queryResult=mysql_query($query);
$numrows=mysql_num_rows($queryResult);
?>
while($row = mysql_fetch_assoc($queryResult)) {
<div class="audio-module-parent">
<div class="audio-module-header">
<?php
<h1> <?php echo $row['id']?> </h1>
</div>
<div class="audio-module-preview"></div>
<div class="audio-module-download">Download</div>
<div class="audio-module-tutorial">Watch Tutorial</div>
</div>
?>
You are placing code outside the <?php ?> e.g here
?>
while($row = mysql_fetch_assoc($queryResult)) {
and then putting HTML inside <?php ?> e.g here
<?php
<h1> <?php echo $row['id']?> </h1>
</div>
It should be
<?php
error_reporting(E_ALL); //Enable Error Reporting
ini_set('display_errors',1); //change value to 0 to disable the error views
$db_host = "localhost";
$db_username = "user192";
$db_pass = "xxxx";
$db_name = "sound-library";
#mysql_connect("$db_host","$db_username","$db_pass") or die ("couldnt connect to sql");
#mysql_select_db ("$db_name") or die ("cant find database");
$query = "select * from sound-library";
$queryResult=mysql_query($query) or die(mysql_error());
$numrows=mysql_num_rows($queryResult);
while($row = mysql_fetch_assoc($queryResult)) {
?>
<div class="audio-module-parent">
<div class="audio-module-header">
<h1> <?php echo $row['id']?> </h1>
</div>
<div class="audio-module-preview"></div>
<div class="audio-module-download">Download</div>
<div class="audio-module-tutorial">Watch Tutorial</div>
</div>
<?php } ?>
- <h1> <?echo $row['id'];?> </h1> missing semi colon (;) here in this line.
- Missing } end of while loop
Use this connection. Write your password in 'xxxx' part & check it. comment your mysql_connect & mysql_select_db
$con=mysql_connect("localhost","user192","xxxx") or die ("couldnt connect to sql");
$db=mysql_select_db ("sound-library",$con) or die ("cant find database");
<?
while($row = mysql_fetch_assoc($queryResult))
{?>
<div class="audio-module-parent">
<div class="audio-module-header">
<h1> <?echo $row['id'];?> </h1>
</div>
<div class="audio-module-preview">
</div>
<div class="audio-module-download">Download</div>
<div class="audio-module-tutorial">Watch Tutorial</div>
</div>
<?}?>
Related
Working on a project to display a list of cars
$username="username";
$password="password";
$database="listofcars";
$mysqli = new mysqli("localhost", $username, $password, $database);
#mysql_select_db($database) or die( "Unable to select database");
$query2="SELECT * FROM cars";
$result=$mysqli->query($query2);
$num=$mysqli->mysqli_num_rows($result);
$mysqli->close();
echo "
<div class="item">
<div class="container">
<div class="imgcontainer"><img alt="Cars for sale" src="$carimage" width="380" height="380" /></div>
<div class="details">
<a href="$internallink" target="_blank">
<h3 class="title"> $carname <br />
<span> $cartype </span></h3>
<p>
$cardesc
</p>
<div class="button"><span data-hover="Order Car">Order Car</span></div>
</a>
</div>
</div>
</div>
I need it to loop the results
database name: listofcars
tablename: cars
Field names
carsid
carname
carimage
cartype
internallink
cardesc
Try this to see if it works.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "listofcars";
// Create connection
$mysqli = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$mysqli) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM cars";
$result = mysqli_query($mysqli, $sql);
?>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<div class="item">
<div class="container">
<div class="imgcontainer">
<img alt="Cars for sale" src="<?php echo $row['carimage']; ?>" width="380" height="380">
</div>
<div class="details">
<a href="<?php echo $row['internallink']; ?>" target="_blank">
<h3 class="title"><?php echo $row['carname']; ?><br><span><?php echo $row['cartype']; ?></span></h3>
<p><?php echo $row['cardesc']; ?></p>
<div class="button">
<span data-hover="Order Car">Order Car</span>
</div>
</a>
</div>
</div>
</div>
<?php } ?>
mysqli_close($mysqli);
You need to leave the connection open until you are done and loop through it after running the query.
Something like
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "listofcars";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Query
$sql = "SELECT * FROM cars";
$result = mysqli_query ($conn, $sql);
while($row = mysqli_fetch_array($result)) {
//Whatever you want to do for each row...
echo "
<div class='item'>
<div class='container'>
<div class='imgcontainer'><img alt='Cars for sale' src='".$row[carimage]."' width='380' height='380' /></div>
<div class='details'>
<a href='".$row[internallink]."' target='_blank'>
<h3 class='title'> ".$row[carname]." <br />
<span> ".$row[cartype]." </span></h3>
<p>
".$row[cardesc]."
</p>
<div class='button'><span data-hover='Order Car'>Order Car</span></div>
</a>
</div>
</div>
</div>";
}
$conn->close();
I have to pull out the value from the database table call count.php which I already got the value 49. The problem is how to insert into the html
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hopeplace";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT COUNT(*) AS TOTAL_APPLICANT FROM applicant";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo $row["TOTAL_APPLICANT"];
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
code for the container for the applicant pending
<div class="content">
<div class="row">
<div class="col-xs-5">
<div class="icon-big icon-danger text-center">
<i class="ti-user"></i>
</div>
</div>
<div class="col-xs-7">
<div class="numbers">
<p>Applicant Pending</p>
<p>*this is the place where value need to be put*</p>
</div>
</div>
</div>
<div class="footer">
<hr />
</div>
</div>
You can assign value to one variable and echo that variable inside html
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hopeplace";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$count = 0;
$sql = "SELECT COUNT(*) AS TOTAL_APPLICANT FROM applicant";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$count = $row["TOTAL_APPLICANT"] ;
}
}
mysqli_close($conn);
?>
code for the container for the applicant pending will be like
<div class="content">
<div class="row">
<div class="col-xs-5">
<div class="icon-big icon-danger text-center">
<i class="ti-user"></i>
</div>
</div>
<div class="col-xs-7">
<div class="numbers">
<p>Applicant Pending</p>
<p>
<?php echo $count; ?>
</p>
</div>
</div>
</div>
<div class="footer">
<hr />
</div>
</div>
save both html & php in same file or ensure that your html code is saved as a .php file. So, your script will be - <p><?php echo $total; ?></p> or <p><?= $total ?></p>
I am trying to repeat a div the same div multiple times with as many rows as i have in the database table. a non working example i made of what i want is below.
<?php for ($i=0; $i < 3; $i++) {
?><!-- switch the three with the number of rows in the table-->
I would want to loop the container and switch the database query with the value of i.
<div id = "bodyContainer">
<form class="review">
<div class="reviewContainer">
<div class = "images">
<?php
$link = mysqli_connect("localhost", "root", "root","DJP");
if (mysqli_connect_error()) {
die("Could not connect to database");
}
$query = "SELECT * FROM reviews WHERE id = '<?php echo $i ?><!--switch the value of i here-->'";
$result=mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
echo '<img height = 90% width = 100% src="data:image/jpeg;base64,'.$row[3].' " />';
?>
</div>
<!--<div class="title">
<?php
//echo $row[1];
?>
</div>-->
<div class = "comment">
<?php
echo $row[2];
?>
</div>
<div class = "rating" style = "float: right;">
<?php
echo $row[4];
?>
<br/>
<br/>
stars
</div>
</div><!--end review-->
</form>
</div><!--end body container-->
<?php;}?><!-- end the for loop so all the divs can be re-done with a new entry for the query. I would move the $link out of loop also.-->
Just fetch all the rows once and then go through them as you print the data:
$link = mysqli_connect("localhost", "root", "root","DJP");
if (mysqli_connect_error()) {
die("Could not connect to database");
}
$query = "SELECT * FROM reviews";
$result=mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)){ ?>
<div id = "bodyContainer">
<form class="review">
<div class="reviewContainer">
<div class = "images">
<?php
echo '<img height = 90% width = 100% src="data:image/jpeg;base64,'.$row[3].' " />';
?>
</div>
<!--<div class="title">
<?php
//echo $row[1];
?>
</div>-->
<div class = "comment">
<?php
echo $row[2];
?>
</div>
<div class = "rating" style = "float: right;">
<?php
echo $row[4];
?>
<br/>
<br/>
stars
</div>
</div><!--end review-->
</form>
</div><!--end body container-->
<?php } ?>
So I've written out simple code that SHOULD display every comment in the comment table (there is only 1 comment at the moment). The issue is - it doesn't.
What the code SHOULD do: display every comment in the comment table.
What it DOESN'T do: doesn't display ANY comments.
The code below doesn't work:
<!-- Posted Comments -->
<?php
$result = mysqli_query("SELECT * FROM comment WHERE adventureID = 2");
while($row = mysqli_fetch_assoc($result)) { ?>
<div class="media">
<a class="pull-left" href="#">
<img class="media-object" src="http://placehold.it/64x64" alt="">
</a>
<div class="media-body">
<h4 class="media-heading"><?php echo $row['userID']; ?>
<small><?php echo $row['dateTime']; ?></small>
</h4>
<?php echo $row['commentText']; ?>
</div>
</div>
<?php } ?>
You forgot to include the connection variable in mysqli_query:
$result = mysqli_query($con, "SELECT * FROM comment WHERE adventureID = 2");
Where $con is the connection variable. Refer this Link
If this is the only piece of code you have I was wondering if you have made a connection with the database in which the comments are stored in.
Making a connection could look a bit like this:
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$db= 'test';
// Create connection
$connect = new mysqli($servername, $username, $password, $db);
// Check connection
if ($connect->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
i tried the empty selector in jquery but it doesnt work. the content is still being displayed. i am retrieving some rows from the SQL database.. if there is no database then i dont want to display that div.
<div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
</div>
Is that your whole code? Are you working with ajax? If you're using pure PHP without ajax just set the output div into your "if"-conditions?
If you want to use jQuery, try:
if ( ($("div.scrollableArea p").text()).length > 0 )
{
$("div.scrollableArea p").show();
}
else
{
$("div.scrollableArea p").hide();
}
But you can do it without jQuery, I guess.
You need to put the HTML inside your php if clause.
Simple example:
<div id="scrollingText">
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysqli_connect_errno($con)) {
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
<?php } else { ?>
<div class="errordiv">Display this on error</div>
<?php } ?>
</div>
thank you guys with ur help i made some change. the below code did it:
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysql_num_rows($result) > 0)
{
?><div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?> </p>
</marquee>
</div>
</div>
</div>
<?php } ?>