I'm following along with the example from w3schools to extract some rows from a mysql db and return the results in a table format, but I'm only getting blank rows (although the correct number of them, oddly enough).
Here is what I have in my php script:
<!DOCTYPE html>
<html>
<head>
<style>
table
{
width: 100%;
border-collapse: collapse;
}
table, td, th
{
border: 1px solid black;
padding: 5px;
}
th
{
text-align: left;
}
</style>
</head>
<body>
<?php
$q = $_GET['q'];
$con = mysqli_connect('localhost', 'root', '123', 'drugsatfda');
if (!$con) {die('Could not connect: ' . mysqli_error($con));}
mysqli_select_db($con, "drugsatfda");
$sql="SELECT * FROM products WHERE DrugName LIKE '"."%".$q."%"."'";
$result = mysqli_query($con,$sql);
$count = 0;
echo "<table>
<tr>
<th>Application Number</th>
<th>Product Number</th>
<th>Form</th>
<th>Strength</th>
<th>Reference Drug</th>
<th>Drug Name</th>
<th>Active Ingredient</th>
</tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC) && $count < 10) {
echo "<tr>";
echo "<td>" . $row['ApplNo'] . "</td>";
echo "<td>" . $row['ProductNo'] . "</td>";
echo "<td>" . $row['Form'] . "</td>";
echo "<td>" . $row['Strength'] . "</td>";
echo "<td>" . $row['ReferenceDrug'] . "</td>";
echo "<td>" . $row['DrugName'] . "</td>";
echo "<td>" . $row['ActiveIngredient'] . "</td>";
echo "</tr>";
$count++;
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Can someone please help me see where I'm going wrong?
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC) && $count < 10) {
When doing this, you get two conditions, so $row = mysqli_fetch_array($result, MYSQLI_ASSOC) just becomes an expression returning a boolean true, so while $count is less than 10, you get while (true && true).
The solution is to break; when the loop reaches 10 instead.
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
if ($count >= 10) // If we reach 10 iterations, break the loop
break;
echo "<tr>";
echo "<td>" . $row['ApplNo'] . "</td>";
/* and so on */
echo "</tr>";
$count++;
}
You can verify this by doing
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC) && $count < 10) {
var_dump($row);
}
Which will output bool(true); for each iteration, until there are no more rows to fetch (when mysqli_fetch_array() returns null), or when $count is 10 or greater - whichever comes first.
The easier approach
An alternative, is simply just to fetch 10 rows. You can add a LIMIT clause to your SQL, like
$sql="SELECT * FROM products WHERE DrugName LIKE '%".$q."%' LIMIT 10";
This will fetch only 10 rows, meaning that you can just loop through it normally, without having to count
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['ApplNo'] . "</td>";
/* and so on */
echo "</tr>";
}
It should also be noted that your code is currently wide open to SQL injections, and you should use prepared statements to guard yourself against this.
References
http://php.net/manual/en/mysqli.prepare.php
How can I prevent SQL injection in PHP?
Please try again...
<!DOCTYPE html>
<html>
<head>
<style>
table
{
width: 100%;
border-collapse: collapse;
}
table, td, th
{
border: 1px solid black;
padding: 5px;
}
th
{
text-align: left;
}
</style>
</head>
<body>
<?php
$q = $_GET['q'];
$con = mysqli_connect('localhost', 'root', '123', 'drugsatfda');
if (!$con) {die('Could not connect: ' . mysqli_error($con));}
mysqli_select_db($con, "drugsatfda");
$sql="SELECT * FROM products WHERE DrugName LIKE '"."%".$q."%"."'";
$result = mysqli_query($con,$sql);
$count = 0;
echo "<table>
<tr>
<th>Application Number</th>
<th>Product Number</th>
<th>Form</th>
<th>Strength</th>
<th>Reference Drug</th>
<th>Drug Name</th>
<th>Active Ingredient</th>
</tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
if($count >=10)
break;
echo "<tr>";
echo "<td>" . $row['ApplNo'] . "</td>";
echo "<td>" . $row['ProductNo'] . "</td>";
echo "<td>" . $row['Form'] . "</td>";
echo "<td>" . $row['Strength'] . "</td>";
echo "<td>" . $row['ReferenceDrug'] . "</td>";
echo "<td>" . $row['DrugName'] . "</td>";
echo "<td>" . $row['ActiveIngredient'] . "</td>";
echo "</tr>";
$count++;
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Related
I outputed the data from my database in a HTML Table and styled it with CSS. Everything is fine, but there is an awkward White space above the outputed Table. (see picture). What's wrong with my code?
Result:
<html>
<head>
<title>Create new user</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<div>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "users";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select * from employee";
$result = $conn->query($sql);
echo "<table>";
echo "<th>Identifier</th><th>Name</th><th>Lastname</th>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["vorname"] . "</td>";
echo "<td>" . $row["nachname"] . "</td>";
echo "</tr>";
echo "</br>";
}
}
else {
echo "0 results";
}
echo "</table>";
$conn->close();
?>
</div>
</body>
</html>
Expected result : no white space above the Table
Many Thanks
Why are you adding a <br>
echo "</br>";
in your while loop.
Try this -
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["vorname"] . "</td>";
echo "<td>" . $row["nachname"] . "</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
If this does not fix, try inspecting and see what is adding space. Also check for the padding and margins in the computed view in chrome inspect view.
If nothing works, try setting top:0 for the div and may be for table :)
So I basically been coding this quick stat shower for my game server, but I want it to ID numbers.
<html>
<head>
<meta charset="utf-8">
<title>ExileMod Stats</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
$con=mysqli_connect("******","******","******","******"); //server address, username, password, dbname
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//check order ascending or descending
if (isset($_GET["order"])) {
$sort = $_GET["order"];
if ($_GET["order"] == "asc"){
$sort = "desc";
$order = "asc";
}
else{
$sort = "asc";
$order = "desc";
}
}
//check filter
if (isset($_GET["name"])) {
$name = $_GET["name"];
}
else {
$name = "name";
}
$list=array('name', 'money', 'score', 'kills', 'deaths', 'uniform', 'vest', 'last_updated_at');
if (in_array($name,$list))
{
//variable ok
}
else
{
$name = "name";
}
$query = mysqli_query($con,"SELECT * FROM account a INNER JOIN player p ON a.uid = p.account_uid ORDER BY a.$name $order");
?><!--//echo "<table border='1'>-->
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
<table class="table">
<tr>
<?php echo "<th>Player Name</th>";?>
<?php echo "<th>Money</th>";?>
<?php echo "<th>Score</th>";?>
<?php echo "<th>Kills</th>";?>
<?php echo "<th>Deaths</th>";?>
<?php echo "<th>Uniform</th>";?>
<?php echo "<th>Vest</th>";?>
<?php echo "<th>Last Updated</th>";?>
</tr>
<!--//";-->
<?php
while($row = mysqli_fetch_array($query))
{
?><tr class="danger"><?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['money'] . "</td>";
echo "<td>" . $row['score'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . $row['uniform'] . "</td>";
echo "<td>" . $row['vest'] . "</td>";
echo "<td>" . $row['last_updated_at'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
© HeroesOfGaming 2016 - 2017
</div>
</body>
</html>
So what I am trying to do is put numbers so it counts 1 2 3 4 5 6 7 to however many players are selected from the database? Hopefully this makes sense.
You can do like this
<?php
$i = 0; // <--- Added this
while($row = mysqli_fetch_array($query))
{
$i++; // <--- Added this
?><tr class="danger"><?php
echo "<td>". $i . "</td>"; // <--- Added this
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['money'] . "</td>";
echo "<td>" . $row['score'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . $row['uniform'] . "</td>";
echo "<td>" . $row['vest'] . "</td>";
echo "<td>" . $row['last_updated_at'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I have a table that shows me data from a call flow.
I need the Data from this table to be manipulated per row in such a way that all the values of my table, which are being looked up from my DB, (which are now in code) will be translated into a text value. Let me show U and explain:
My Table:
<?php
include_once "Connect2Martijn1.php";
?>
<link rel="stylesheet" href="CSSMartijn1.css">
</link>
<head>
<meta charset="iso-8859-1">
<meta name="description"content="VoizXL ">
<meta name="keywords"content="VoizXL ">
<meta name="author"content="Kenn Lo-A-Tjong">
</meta>
<title>Call Flow</title>
</head>
<fieldset>
<article class="rondehoeken">
<header>
<div class="streep1"></div>
<div class="streep2"></div>
<div class="streep3"></div>
<div class="streep4"></div>
<div class="streep5"></div>
<h1 id="artikel-titel" >Call Flow</h1>
</header>
<div id="artikel-container">
<table class="table 1">
<thead>
<title>Call Flow</title>
<meta charset = "UTF-8" />
<style type = "text/css">
table, td, th {
border: 1px solid black;
}
</style>
</thead>
<tbody>
<?php
$con=mysqli_connect("localhost","root","","voizxl_wachtrij");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Callflow");
echo "<table border='0'>
<tr>
<th>Nummer</th>
<th>Naam</th>
<th>Status</th>
<th>Time</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . $row['statusAnswered'] . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Example of (how I want to be) Translating the Data:
<?php
if ($row['statusAnswered'] ="NULL")
{
echo "Not Answered!";
}
else
{
echo "Answered!";
}
?>
What I want to Achieve is for eg. that the value in this table from $row['statusAnswered'] will be displayed in text as "Answered or Not Answered" if the Value of this row in the DB is NULL or Not...
How do I do this?
Right now I can only achieve to have 1 echo under the table saying Answered :S
No Idea how to put it per $row.
while($row = mysqli_fetch_array($result))
{
if (!isset($row['statusAnswered']))
{
$answered = "Not Answered!";
}
else
{
$answered = "Answered!";
}
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . $answered . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
if ($row['statusAnswered'] =="NULL"||$row['statusAnswered'] =="Null" || $row['statusAnswered'] =="null" || $row['statusAnswered'] =="")
{
echo "<td>Not Answered!</td>";
}
else
{
echo "<td>Answered!</td>";
}
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<table border='0'>
<tr>
<th>Nummer</th>
<th>Naam</th>
<th>Status</th>
<th>Time</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['calleridnum'] . "</td>";
echo "<td>" . $row['calleridname'] . "</td>";
echo "<td>" . ($row['statusAnswered']=='NULL'?'Not Answered!':'Answered!') . "</td>";
echo "<td>" . $row['statusCalling'] . "</td>";
echo "</tr>";
}
echo "</table>";
You could use;
$checkstatus = if($row['statusAnswered'] = NULL) { echo "Not Answered!"; } else {echo "Answered!";};
then call that by replacing;
echo "<td>" . $row['statusAnswered'] . "</td>";
with;
echo "<td>{$checkstatus}</td>"
Im building a web site that will return results from mysql and display them on a web page.
I have no problem with storing the data in mysql or retrieving the data
and displaying it on the web page.
When the data is returned to the page I want to store it in a table and run a loop so that for each record there will be a number incremented down the side like a list number.
Any help would be appreciated.
I keep running into errors with my code:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("forloops", $con);
$result = mysql_query("SELECT * FROM userinfo ORDER BY age DESC");
echo "<table border='1'>
<tr>
<th>Position</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>";
$counter = 0;
$position= 0;
$row = mysql_fetch_array($result);
if($counter <=10){
echo "<tr>";
echo "<td>" . $position . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "</tr>";
$counter++;
}
echo "</table>";
mysql_close($con);
?>
The problem with your current code is that you're only fetching the first row from the query, take a look at how I've down it below:
echo "<table border='1'>
<tr>
<th>Position</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>";
$counter = 0;
$position= 0;
while ($row = mysql_fetch_array($result)) { // This is new code
if($counter <=10){
echo "<tr>";
echo "<td>" . $position . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "</tr>";
$counter++;
}
} // This is new code
echo "</table>";
mysql_close($con);
?>
If you notice, I've created a loop around the echoing of the table's rows <tr> and cells <td>.
This while loop will go through each row returned from the query.
If you have more then one result from database, you should use:
while ($row = mysql_fetch_array($result)) {
//process one row
}
Using your way, $row is always the same.
And secondly, if i saw right, you dont increase $position variable.
I am learning PHP as I code. From an example on w3schools it showed using PHP and msql to display database results on a html table. My questions is, I now have too many rows and I could not make them have mismatch colors between rows. I've tried adding style span and font color after <td but it doesn't take it. the entire PHP just don't work if I do so.
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
The output of the code above will be:
Firstname Lastname
Glenn Quagmire
Peter Griffin
http://www.w3schools.com/php/php_mysql_select.asp
Not entirely sure what you mean by color mismatch. Assuming that you mean alternating row colors I'd do the following:
$odd = false;
while (...)
{
echo '<tr class="'.($odd ? "odd" : "even").'">';
...
echo "</tr>";
$odd = !$odd;
}
Now you have the tr element being of class odd or even alternatingly, and may specify some additional background color for one of them in your CSS, e.g.:
tr.odd { background-color: rgba(0, 0, 0, 0.05); }
Replace this:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
with this:
$i = 0;
while($row = mysql_fetch_array($result))
{
echo "<tr ". ($i % 2 == 0 ? 'style="background-color:grey;"' : '' .">";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
$i++;
}
Every other row will have grey color.
Maybe you can use this approach with jquery
<script src="text/javascript">
$('#table tbody tr:odd').addClass('odd');
$('#table tbody tr:even').addClass('even');
</script>
and then add the styles
.odd { background-color: #color }
.even { background-color: #color }
$class = "even";
while($row = mysql_fetch_array($result))
{
if($class == "even")
{
echo "<tr class='$class'>";
$class = "odd";
}
else
{
echo "<tr class='$class'>";
$class = "even";
}
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
CSS
tr.even
{
background-color:blue;//Pick your own color
}
tr.odd
{
background-color:green;
}
Here is a list of color names. If you want a more detailed color choices, click here.
Use
$flag = 0;
while($row = mysql_fetch_array($result))
{
if ($flag%2 == 1)
echo "<tr bgcolor=#123345>";
else
echo echo "<tr bgcolor=#643235>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
$flag = $flag +1;
}