I'm a complete newbie when it comes to HTML, I've never needed to do any webpage stuff before so apologies if I'm being a simpleton.
I have a high score table for an iOS app, and I want to display it on the website for the app as well. The leaderboard is working fine in the app using PHP and mySQL, but when I try to access the database using PHP inside an HTML file it doesn't seem to pay any attention to the logic of the PHP (i.e. if I have an if/else statement, it will run both even cases).
Here is a simplified version of my code, if connection to the database is successful then the first column in the table header will say "success", otherwise it will say "failure".
<body>
<table class="altrowstable" id="alternatecolor">
<?php
if (!mysql_connect($db_host, $db_user, $db_pwd))
{
<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
}
else
{
<tr>
<th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
}
?>
</table>
</body>
Can anyone see what has been done incorrectly here? I assumed that the if/else would work as normal, but instead I get both rows inserted into the table, "failure" and "success".
Edit:
I have also tried using echo ""; etc, but not sure which way is correct as they both give me the same error where it ignores the PHP.
Change your code to
<table class="altrowstable" id="alternatecolor">
<?php
if (!mysql_connect($db_host, $db_user, $db_pwd))
{
?>
<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
<?php
}
else
{
?>
<tr>
<th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
<?php
}
?>
</table>
I think that you need to put your HTML tags in the echo command like this
echo '<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th> <th>Draws</th><th>Losses</th>
</tr>';
this should work
Check extension of your file. It need to be filename.php
You need a server to launch php code
You can use echo heredoc like here
<body>
<table class="altrowstable" id="alternatecolor">
<?php
if (!mysql_connect($db_host, $db_user, $db_pwd))
{
echo <<<_END
<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
_END
}
else
{
echo <<<_END
<tr>
<th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
_END
}
?>
</table>
</body>
That seems to be working
Between <?php and ?> is just PHP code. It is treating you HTML as PHP.
Either
Use echo
put ?> before the HTML and <?php> after (i.e. stop and start PHP processing)
try this:
<body>
<table class="altrowstable" id="alternatecolor">
<?php
if (!mysql_connect($db_host, $db_user, $db_pwd))
{
?>
<tr>
<th>Failure</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
<?php
}
else
{
?>
<tr>
<th>Success</th><th>Player</th><th>Points</th><th>Played</th><th>Wins</th><th>Draws</th><th>Losses</th>
</tr>
<?php } ?>
</table>
</body>
Related
I want to get data from my sql database into a table with html.
I have writed 1st part of code ended with ?>
and started with html table and end with html
My code getting only Empty table without getting data from my Database, here the Code, i dont know how to get data with that
<td>
<?php echo $result['nazwiskoimie'] ?>
</td>
Here's the full code :
<?php
$con=mysqli_connect("localhost","root","","listap");
if (mysqli_connect_errno()) {
echo "Blad przy polaczeniu z baza: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM 'lista'");
?>
<html>
<table border='1'>
<tr>
</tr>
<tr>
<td> <?php echo $result['nazwiskoimie'] ?> </td>
//in this part have problem to get my data from database
</html>
Here's also a screenshot of a result: Final After a Save my file
Please try this:
$result = mysqli_query($con,"SELECT * FROM lista");;
?>
<html>
<table border='1'>
<?php
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['nazwiskoimie']?></td>
</tr>
<?php
}
} ?>
</table>
</html>
I want to retrieve data from MySql table. I am using Xampp, phpMyAdmin etc... I followed this tutorial step by step: https://www.youtube.com/watch?v=IHdd02IK2Jg
But I get this error:
Notice: Undefined variable: records in C:\XAMPP\htdocs\display_prsc.php on line 29
And a warning:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in C:\XAMPP\htdocs\display_prsc.php on line 29
It just displays the column names when i run it.
<? php
//make connection
mysql_connect('localhost','root','');
//select_db
mysql_select_db('mynewdb');
$sql="SELECT * FROM new";
$records=mysql_query($sql);
?>
<html>
<head>
<title>Program Scores Table</title>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Year</th>
<th>Criteria</th>
<th>Score</th>
<tr>
<?php
while ($new=mysql_fetch_assoc($records)){
echo"<tr>";
echo"<td>".$new['py']."</td>";
echo"<td>".$new['pcrit']."</td>";
echo"<td>".$new['psco']."</td>";
echo"</tr>";
}
?>
</table>
</body>
</head>
</html>
You have a few errors in your code:
1. Wrong php opening tag
You have to remove the space in your php opening tag, e.g.
<?php
2. Weird html
Almost your entire html is wrong! I would recommend you to read a book or watch a basic html tutorial
So your entire code should look something like this:
<?php
//make connection
mysql_connect('localhost', 'root', '');
//select_db
mysql_select_db('mynewdb');
$sql = "SELECT * FROM new";
$records = mysql_query($sql);
?>
<html>
<head>
<title>Program Scores Table</title>
</head> <!-- head tag closed here -->
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Year</th>
<th>Criteria</th>
<th>Score</th>
</tr> <!-- forgot / to close the tag -->
<?php
while ($new = mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>" . $new['py'] . "</td>";
echo "<td>" . $new['pcrit'] . "</td>";
echo "<td>" . $new['psco'] . "</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
Side Notes:
1. What can be improved?
I would do some basic error checking e.g. if the connection failed and so on
Use mysqli_* with prepared statements, or PDO with prepared statements, they are much safer! (We live in 2015)
(Also if the guy in your tutorial uses mysql_*, change the tutorial!)
So that you don't only have your old code working, here your improved code:
<?php
//Database stuff
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "mynewdb";
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("SELECT * FROM new");
$stmt->execute();
//Close Connection
$dbh = null;
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
<html>
<head>
<title>Program Scores Table</title>
</head>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Year</th>
<th>Criteria</th>
<th>Score</th>
</tr>
<?php
while ($new = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<tr>";
echo "<td>" . $new['py'] . "</td>";
echo "<td>" . $new['pcrit'] . "</td>";
echo "<td>" . $new['psco'] . "</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
2. Coding side notes
Add error reporting to the top of your file(s) which will help find errors.
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
?>
Error reporting should only be done in staging, and never production.
Note the space in your first php tag.
You have <? php , it should be <?php
And since it seems you are starting to learn, avoid learning things deprecated. Don't use MySQL, use PDO or Mysqli.
try
<?php
//make connection
$con= mysql_connect('localhost','root','');
//select_db
$select= mysql_select_db('mynewdb',$con);
$sql="SELECT * FROM new";
$records=mysql_query($sql);
?>
<html>
<head>
<title>Program Scores Table</title>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Year</th>
<th>Criteria</th>
<th>Score</th>
<tr>
<?php
while ($new=mysql_fetch_array($records)){
echo"<tr>";
echo"<td>".$new['py']."</td>";
echo"<td>".$new['pcrit']."</td>";
echo"<td>".$new['psco']."</td>";
echo"</tr>";
}
?>
</table>
</body>
</head>
</html>
I recommend you using Prepared statements for MySQL instead of clean MySQL, it's not secure at all, and will give you problems later on.
A lille guide here.
http://www.w3schools.com/pHp/php_mysql_prepared_statements.asp
There are multiple errors in your code. But for the error, it is due to mysql_query() returns FALSE at line:
$records=mysql_query($sql);
Then, the line while ($new=mysql_fetch_assoc($records)){ cannot get the value of $records, thus produce the errors.
Other errors are:
HTML
missing DOCTYPE
missing meta charset
a missing </tr>
a misplaced </head>
be modern, use CSS to style your HTML instead of using HTML tag attributes
PHP
didn't check the return value of functions before using them
use of deprecated mysql_* functions; use PDO / MySQLi instead
extra space at <? php
Im very new to PHP and was trying to get record information to display on a php page using the MAMP server environment.
The attributes are productID, productName, productDescription and productPrice.
I've been able to get really basic PHP to run fine but every time I open this php file, nothing displays, I was wandering if it might be to do with the location I placed the php file. It is currently in htdocs. would appreciate any help.
Thanks
<?php
//connection to db
mysql_connect('localhost', 'root', 'root');
//choosing db
mysql_select_db(primrose);
$sql= "SELECT * FROM products";
$records mysql_query(sql);
?>
<html>
<head>
<title>Product Data</title>
</head>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<?php
//loops over each record and for each a new record is set into the variable products
while(products=mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$products['productName']."</td>";
echo "<td>".$products['productDescription']."</td>";
echo "</tr>";
} //end while
?>
</table>
</body>
</html>
This is because (I think) this line is bad:
mysql_select_db(primrose);
Add quotes around the name of db:
mysql_select_db("primrose");
Also this line:
$records mysql_query(sql);
change to
$records = mysql_query($sql);
and this:
while(products=mysql_fetch_assoc($records)){
to
while($products=mysql_fetch_assoc($records)){
NOTE 1:
Do not use mysql functions since, they are deprecatid. Use mysqli or PDO instead.
NOTE 2:
Let's turn on your error reporting with these two rows in the top of your PHP file:
error_reporting(E_ALL);
ini_set('display_errors', 1);
You have a lot of syntax errors. Let's use an IDE to identify them.
So your final code like this:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//connection to db
mysql_connect('localhost', 'root', 'root');
//choosing db
mysql_select_db("primrose");
$sql= "SELECT * FROM products";
$records = mysql_query($sql);
?>
<html>
<head>
<title>Product Data</title>
</head>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<?php
//loops over each record and for each a new record is set into the variable products
while($products=mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$products['productName']."</td>";
echo "<td>".$products['productDescription']."</td>";
echo "</tr>";
} //end while
?>
</table>
</body>
</html>
Try setting error_reporting(E_ALL); to on just after to open'ed PHP.
If not, make sure that error reporting is turned on in your php.ini
http://php.net/manual/en/errorfunc.configuration.php
This is my first posting in SO, my apologize if I opened an existing question. As I couldn't find the result in Google. Sorry to said but I'm still fresh in PHP PDO and in learning stage.
Back to my question, currently I'm building a customer visit logs from my wife but I'm stuck with the result. I have two table which one stores the customer information and another table store the visit details. I uploaded the test table at here: SQL Fiddle
And below is my current coding and I'm using PHP PDO while
<?php
require_once 'dbconnect.php';
$p_id = $_GET['name'];
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = "SELECT customer.fname, customer.lname, customer.gender, services.treatment, services.date
FROM customer LEFT JOIN services
ON customer.id = services.customer_id
WHERE customer.slug LIKE :id";
$q = $conn->prepare($sql);
$q->execute(array('id' => $p_id));
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<div id="container">
<h1>Customer Record</h1>
Name: <br />
Gender: <br />
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Customer Name</th>
<th>Customer Gender</th>
<th>Treatment</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo htmlspecialchars($r['fname']), ' ', htmlspecialchars($r['lname'])?></td>
<td><?php echo htmlspecialchars($r['gender']); ?></td>
<td><?php echo htmlspecialchars($r['treatment']); ?></td>
<td><?php echo htmlspecialchars($r['date']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
Seach Again
</body>
</div>
</html>
And I achieve as what SQL Fiddle result, but what I wanted is, the name and gender is not keep repeating.
I attached together with the screenshot:
Screenshot
What I want is as per the screenshot image, Name: John Doe and Gender: Male, should be on top and not keep on repeating while the table below show all the visit details. I tried to modified the code but it seems it don't really work out.
Please advise me as I'm really out of idea how to achieve what I want.
Thank you so much.
Since you do a LEFT JOIN in your SQL query, you know ahead of time that all of the fname, lname and gender values returned by $q->fetch() are going to be for the same customer.slug, right? So you can count on that.
My suggestion would be to instead use the fetchAll() function to get an array of all records for customer.slug, and then render that in your view. For example (haven't tested this) you could add the following after $q->setFetchMode(PDO::FETCH_ASSOC); ...
$cs = $q->fetchAll(); // customer services join
Then, in your <html> view, you could do something like the following:
<h1>Customer Record</h1>
Name: <?php echo htmlspecialchars($cs[0]['fname'].' '.$cs[0]['lname']); ?> <br />
Gender: <?php echo htmlspecialchars($cs[0]['gender']); ?> <br />
<table>
<thead>
<tr>
<th>Treatment</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php foreach($cs as $r): ?>
<tr>
<td><?php echo htmlspecialchars($r['treatment']); ?></td>
<td><?php echo htmlspecialchars($r['date']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
Of course, it might also be a good idea to check to see that any records were returned by your query and display a "not found" message if not. After all, $cs[0] might be empty, giving you a PHP error.
I have a problem with flush() and a HTML table using "Internet Explorer".
I edited the following code to display a "real time" echo in a table with flush(), I put the flush() call after the </table> tag because in "Internet Explorer" it doesn't work, but I don't know why only the first echo of my cycle goes into the table, the others go outside. Any help?
<html lang="en">
<head>
</head>
<table border='1'>
<tr>
<td>
<?php
$total = 10;
for($i=1; $i<=$total; $i++){
echo "sometext";
//other stuff
?>
</td>
</tr>
</table>
<?php
echo str_repeat(' ',1024*64);
flush();
sleep(1);
}
?>
</body>
</html>
Put simply, you've closed the table inside the loop rather than outside of the loop. So after the first iteration, your table tags don't match up. Move </table> beyond your closing loop curly bracket }.
Concerning the Internet Explorer problem. You may want to look at this: http://php.net/manual/en/function.flush.php
It talks about some versions of Internet Explorer requiring a certain amount of bytes before it will begin to flush data. You may also want to try calling ob_flush(); above your flush(); statement.
The browser might be waiting for the closing because it does not know the content of all cells, and thus also the width of the columns. Try to hint it with fixed values for the td element, for example:
<td style="width: 200px">
You are closing your table inside of your loop.
<html lang="en">
<head>
</head>
<body>
<table border='1'>
<tr>
<td>
<?php
$total = 10;
for($i=1; $i<=$total; $i++){
echo "sometext";
//other stuff
echo str_repeat(' ',1024*64);
flush();
sleep(1);
}
?>
</td>
</tr>
</table>
</body>
</html>
Proof: