I am currently trying to echo data which I have selected from a database using the following code:
(DB Connection line)
$sqlQ = 'SELECT w_Continent.ID, w_Continent.NAME as continent_name, w_Country.Name, w_Country.Continent FROM w_Continent JOIN w_Country ON w_Continent.ID = w_Country.Continent';
//Execute SQL Query
$stmtRow = $db->query($sqlQ);
<?php foreach($stmtRow->fetchAll() as $cont):;?>
<?php echo $cont['continent_name']; ?>
<?php endforeach; ?>
However, when checking the browser, this does not have the continents listed.
If you want your data listed you have to add this line to your code, after echoing "continent_name".
<?php echo "<br/>"; ?>
Or you can add "\n" and view listed text in "View Source" page of your browser.
<?php echo "\n"; ?>
Related
I have a PHP program that is supposed to print customer purchase history after selecting the customer name from a dropdown menu. However, after selecting a customer, the unordered list that is supposed to be displayed, is empty.
Here is my code
<!DOCTYPE html>
<html>
<body>
<script src="customerselect.js"></script>
<?php
include "connecttodb.php";
include "getcustomer.php";
?>
<h1>Customers</h1>
<br>
Customers
Products
<br>
<hr>
<hr>
Select the customer whom you'd like to see what items they've purchased:
<form action="" method = "post">
<select name="pickacustomer" id="pickacustomer">
<option value="1">Select Here</option>
<?php
include "getcustomername.php";
?>
</select>
</form>
<hr>
<?php
if (isset($_POST['pickacustomer'])){
include "connecttodb.php";
include "getcustomerinfo.php"; #This is the line that I expect to print my customer purchase history
}
?>
<hr>
<br>
<br>
<h2>List of all Customers</h2>
</body>
</html>
getcustomerinfo.php
<?php
$connection = mysqli_connect("localhost", "root", "xxx", "xxx") or die("Not connected");
$whichCustomer = $_POST["pickacustomer"];
$query = "
SELECT c.firstname
, c.lastname
, p.description
, u.quantitybought
FROM purchased u
JOIN customer c
ON u.customerid = c.customerid
JOIN products p
ON u.productid = p.productid
WHERE c.firstname = ". $whichCustomer.";";
$result = mysqli_query($connection, $query);
if (!$result) {
die("databases query failed - getcustomerinfo.php.");
}
echo "<ul>";
while ($row = mysqli_fetch_assoc($result)){
echo "<li>" . $row["firstname"] . $row["lastname"]. $row["description"]. $row["quantitybought"]. "</li>";
}
echo "</ul>";
mysqli_free_result($result);
?>
customerselect.js
window.onload=function(){
prepareListener();
}
function prepareListener(){
var droppy;
droppy = document.getElementById("pickacustomer");
droppy.addEventListener("change",getCustomer);
}
function getCustomer(){
this.form.submit();
}
I know that the query in getcustomerinfo.php works because if I enter an actual customer name, it'll display it. However, when I try to dynamically assign a name to it, the program fails to display the proper information. Please excuse how poorly this code is written as I'm just starting to learn PHP.
Thanks to user3783243 reminding me to double check the POST field, I realized that I was receiving the customerid rather than the customer.firstname in the POST field. So in getcustomerinfo.php, the query should be edited like so: ...WHERE customer.customerid... rather than ...WHERE customer.firstname...
I want to create a PDF using HTML, PHP, and MySQL and I want a new page every time a new row is fetched and then generate a combined PDF for all the pages created.
How can I achieve this?
<?php
$count=0;
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con, "electricity");
$result = mysqli_query($con, "SELECT acct, name, address, amount FROM newtable");
while($row = mysqli_fetch_array($result))
{
?>
<html>
<body><pre>
श्री: <?php echo $row['name'];?><br>
पता: <?php echo $row['address'];?>
ACCNO :- <b><?php echo $row['acct'];?></b>
Amount : <b><?php $val=$row['amount']+25; echo $val;?></b>
</pre>
</body>
</html>
<?php
}
mysqli_close($con);
?>
I used FPDF many times and it allows you to do whatever you want. Have more info here for doc & download link : http://www.fpdf.org/?lang=en
I have a code that should check the Database following to find all the people that the current logged in user has followed then it will search the second database posts to find any posts by the people they are following but it currently only shows one post. I know its not very efficient compared to JOIN but I don't fully understand how that works.
Here is a Screenshot of the Database Following it has the user who sent the following request and the user they are following what is it should do is use the session id for the logged in user and find all matching results for the user in the following column and it should take all the results from the column following and search the database post in the column code and echo all the results as the newsfeed.
<?php
$con=mysqli_connect("HOST","USERNAME","PASSWORD","DB");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$myid=$_SESSION['user']['id'];
$result = mysqli_query($con,"SELECT * FROM following WHERE follower='$myid'");
while($row = mysqli_fetch_array($result)) {
$following=$row['following'];
?>
<?php
$con=mysqli_connect("HOST","USERNAME","PASSWORD","DB");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM posts");
while($row = mysqli_fetch_array($result)) {
if ($following==$row['code']){
?>
<div class="content">
<section class="intro">
<h2><?php echo $row['name']; ?> <?php echo $row['lastname']; ?></h2>
<div class="text">
<?php echo $row['post']; ?>
</div>
<?php
}
else{
}
}
mysqli_close($con);
?>
<?php
}
mysqli_close($con);
?>
Because my previouse answer didnt help What do you want to output??
If your database has 3 followers does it have to output this in html?
But this Code will Output All users in your following list Like this..
$result = mysqli_query($con,"SELECT * FROM posts");
echo "<div class='content'> <section class='intro'>";
echo "<h2><a href='/profile/?id=".$row['code']."'>";
while($row = mysqli_fetch_array($result)) {
if ($following==$row['code']){
echo $row['name']." ".$row['lastname']."</a></h2>";
echo $row['post'];
}
echo "<div class='text'>";
echo "</div>";
}
This Code is Outputting this but the Bold text in this example is a link that goes to
/profile/?id=CODEHERE
Steve Jobs <-- is a link (goes to /profile/?id=CODEHERE
The Post text
Johny John
The Post text
Miracle Tru
The Post text
I'm trying to display information from a table in my database in a loop, but for certain information, I'm referencing other tables. When I try to get data from other tables, any data following will disappear. here is the code I am using:
`
//Below is the SQL query
$listing = mysql_query("SELECT * FROM Musicians");
//This is displaying the results of the SQL query
while($row = mysql_fetch_array($listing))
{
?>
...html here...
<? echo $row['name']; ?>
<? echo $row['Town']; ?>
<?
$CountyRef = $row['CountyId'];
$county = mysql_query("SELECT * FROM County WHERE CouInt='$CountyRef'");
while($row = mysql_fetch_array($county))
{
echo $row['CouName'];
}
?>
<?php echo $row['instrument']; ?>
<?php echo $row['style']; ?>`
My problem is that everything after the second while loop is not displaying. Anyone have any suggestions?
Thanks
Second loop should say $row2. $row is being overwritten. Both variables should be named different from each other.
You can acomplish that with a one single query:
SELECT *,
(SELECT CouName FROM County WHERE CouInt=mus.CountyId) as Country
FROM Musicians mus;
You final code should looks like:
<?php
$listing = mysql_query("SELECT *,
(SELECT CouName FROM County WHERE CouInt=mus.CountyId) as Country
FROM Musicians mus;");
//This is displaying the results of the SQL query
while($row = mysql_fetch_assoc($listing))
{
echo $row['name'];
echo $row['Town'];
echo $row['Country']; //Thats all folks xD
echo $row['instrument'];
echo $row['style'];
} ?>
Saludos ;)
And that?:
while($row2 = mysql_fetch_array($county)) {
echo $row2['CouName'];
}
I am trying to position the data pulled from my mysql database using the following php code on my webpage;
<?php
require "connect.php";
$query = "select * from item LIMIT 0, 1";
$result = #mysql_query($query, $connection)
or die ("Unable to perform query<br>$query");
?>
<?php
while($row= mysql_fetch_array($result))
{
?
//call each item from the database, and nest in div or html table
<?=$row['item']?>
<?=$row['description']?>
<?=$row['brand']?>
When I view the webpage it creates the row for each item however each field is empty?
The conection to the database is fine as I ran another piece of code which simply displayed all the info in 1 block. However this is not what I want I want to be able to position each item where I want it on the site.
I wrote this when I had php version 4.1 running on IIS I am now running the latest version of php 5 and after doing some reading it says there are chnages in the syntax including global variables disabled by default so not sure if this is the issue?
try:
<?php echo $row['item'] ?>
<?php echo $row['description'] ?>
<?php echo $row['brand'] ?>
I believe the <?= ?> syntax is normally disabled by default.
The following will also show a dump of you $row array, so you can make sure that it actually contains data
<?php print_r($row); ?>
Good luck!
Sorttag maybe no enabled, try :
<?php echo $row['item']; ?>
<?php echo $row['description']; ?>
<?php echo $row['brand']; ?>