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']; ?>
Related
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've searched hard to find a way to print the query of the code above into a HTML part, but i don't find anything. I saw that is possible to present the result by a HTML table using the fetch_assoc() of php. Below is the code, and on a global view the code is fine, because i test it on a full php page. But i need a solution to put it in HTML. Am i trying an impossible thing?
<?php
require_once('connconf.php');
$conn = mysqli_connect($server, $user, $pw, $bdname) or die ('Connection Error');
$sqlquery = "select ID_Vote from Votes where ID_Player = '1'";
if($result = mysqli_query($conn, $sqlquery)) {
$rowcount = mysqli_num_rows($result);
echo $rowcount; //this is the value i want to publish on a HTML <label>
}
?>
If you want to output the results in a table you could do like this:
print "<table>";
while($row = mysqli_fetch_assoc($result)) {
print "<tr><td>".$row['ID_Vote']."</td></tr>";
}
print "</table>";
The same goes if you just want to print the amount of rows:
print "<table>";
print "<tr><td>".$rowcount."</td></tr>";
print "</table>";
Another way of doing it could be to end php:
<?
// Some code
?>
<table>
<tr>
<td><?=$rowcount;?></td>
</tr>
</table>
<?
// More php
?>
You can have a read here
Guys i found the solution to my problem, all the query was ok like i said. But the extension of my webpage was .HTML and when i change it to .php it worked. Now i got the website running with HTML and php code using a .php extension instead .html
Thanks for your attention.
I know there's a lot about debugging PHP but I just cant seem to figure out why this bit of code doesn't work:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
// CONNECT TO MYSQL
$con = mysqli_connect("127.0.0.1","root","MySQLrootpass","cards");
if (mysqli_connect_errno()){
echo "<div style='text-align: center;'>";
echo "<h1>Could not connect to server, please try again later.</h1>";
echo "Failed to connect to MySQL: " . mysqli_connect_error();
echo "</div>";
}
$data = mysqli_query($con,"SELECT * FROM cards");
while($row = mysqli_fetch_array($data)){ ?>
<div id="card<?php echo $row["id"]; ?>" class="cardPost" style="background-image: url('Sprites/card.png');" onClick="sideData('card','<?php echo $row["id"]; ?>')">
<span class="cardTitle"><?php echo $row["name"]; ?></span>
<div class="elements">
<img class="elmPic" src="Sprites/elms/<?php echo $row["element"]; ?>Elm.png">
</div>
</div>
<?php}
// DISCONNECT FORM MYSQL
mysqli_close($con);
?>
I'm new to the whole PHP display error thing, so I don't know why there not displaying.
After running it through a syntax checker, it said that the } at the end of the while loop was unexpected, I don't understand why, because this code worked fine until I added the id and onClick attribute to the <div>.
Now when I remove them it still doesn't work.
Also, I know it is this while loop, because when I comment it, all works well.
I also have another on the page just like this one (with the id and onClick) that works just fine.
One more thing, when I remove a semicolon in this second while loop (which starts off hidden), all works well still.
I know this is a very specific situation, but any help would be very appreciated.
I'm new to the whole php display error thing, so I don't know why there not displaying
ini_set and error_reporting are functions: like any other functions, they are executed when your script runs. If your script has a parse error, it will never run in the first place, so your functions won't have a chance to do anything.
You can change these settings in your php.ini file instead, which will allow them to take effect before the script executes (and then you'll be able to see the error messages).
display_errors = on
error_reporting = E_ALL
After running it through a syntax checker, it said that the } at the end of the while loop was unexpected, I don't understand why
There should be whitespace between <?php and }.
As traq said, the problem is withespace between <?php and } (line 22). check this out:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
// CONNECT TO MYSQL
$con = mysqli_connect("127.0.0.1","root","","ads");
if (mysqli_connect_errno()){
echo "<div style='text-align: center;'>";
echo "<h1>Could not connect to server, please try again later.</h1>";
echo "Failed to connect to MySQL: " . mysqli_connect_error();
echo "</div>";
}
$data = mysqli_query($con,"SELECT * FROM cards");
while($row = mysqli_fetch_array($data)){ ?>
<div id="card<?php echo $row["id"]; ?>" class="cardPost" style="background-image: url('Sprites/card.png');" onClick="sideData('card','<?php echo $row["id"]; ?>')">
<span class="cardTitle"><?php echo $row["name"]; ?></span>
<div class="elements">
<img class="elmPic" src="Sprites/elms/<?php echo $row["element"]; ?>Elm.png">
</div>
</div>
<?php }
// DISCONNECT FORM MYSQL
mysqli_close($con);
?>
Try
ini_set('display_errors','1'); // value in quotes
For special people. Visit this link .
http://php.net/manual/ru/function.ini-set.php
If not works then.
<?php
phpinfo();
/*
find where is php.ini
then tell admin to change if you dont have permissions.
*/
?>
Also you can try error_get_last
i don't know a lot of php but, i have the following code, it will load all images from the top of the table to the bottom, can i invert this process? load from de bottom to top? I need the ordest lines to be loaded first...
<?php
mysql_connect("localhost","root","");
mysql_select_db("bravo");
$res=mysql_query("select * from coisas");
?>
<div>
<?php
while ($row=mysql_fetch_array($res)) {
echo "<img src=\"{$row['imagem']}\">";
}
?>
</div>
If your table have one identifier column you can do that (assuming id is the identifier column name):
<?php
mysql_connect("localhost","root","");
mysql_select_db("bravo");
$res=mysql_query("SELECT * FROM coisas ORDER BY id DESC");
?>
<div>
<?php
while ($row=mysql_fetch_array($res)) {
echo "<img src=\"{$row['imagem']}\">";
}
?>
</div>
As Mark Baker correctly say, let SQL do it for you with a ORDER BY.
If you still want to do this in PHP, use this snippet of code:
<?php
$html = '';
while ($row=mysql_fetch_array($res)) {
html = "<img src=\"{$row['imagem']}\">".$html;
}
echo $html;
?>
This will concatenate your current (of the loop) fetched array entry with previous one. So, the first will be concatenated before the last (read it as inversed order).
Moreover, don't use msql_* as those functions are deprecated. Use msqli_* or PDO instead
PS.: This code is your code revisited, if there is any error (in fetching array, connection or so) please correct it your own: I only give you a pointer
I'm attempting to extract some data from a database and echo each result. The code below is code that I took from a textbook and then tried to modify to fit my own website that is hosted locally. I cannot see where I'm going wrong, no error messages are shown, just a blank screen when I run the scrip.
<?php #script 9.4 view top 5 recipients
// This script exctracts data from db and then displays each record in a table
DEFINE('SYSPATH','FOO');
require '../application/config/database.php';
require 'mysqli_connect.php';
$q = "SELECT alert_recipient as NAME
FROM alert
LIMIT 5;
";
$r = mysqli_query($dbc,$q);
// $dbc database connection comes from required mysqli_connect.php
if($r)
{
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo $row['name'];
}
}
else {
echo "<p>ERROR</p>".mysqli_error($dbc);
}
?>
The code looks okay except for your echo $row['name'];, note that you are selecting NAME, uppercase.
Change your echo statement to be:
echo $row['NAME'];
because field names quoted within $row array are case sensitive.
(Can't comment yet)
Maybe the script works but there is no results to display. Check your database.