Display mysql_query result in PHP within MYSQL - php

I would like to know how can I display my MySQL result in PHP using tables. I have done a lot of research in this question. However I didn't find a way to solve my question. Here's my code.
<html>
<head>
</head>
<body>
<?php
$con = mysql_connect("localhost","root","123456");
if (!$con) {
die("Cannot Connect: " . mysql_error());
}
mysql_select_db("androidlogin",$con);
$sql = "select id, speed, distance FROM result";
$data = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>ID</th>
<th>Speed</th>
<th>Distance</th>
</tr>";
while($record = mysql_fetch_array($data)){
echo "<tr>";
echo "<td>" . $record['ID'] . "</td>";
echo "<td>" . $record['Speed'] . "</td>";
echo "<td>" . $record['Distance'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
It seems that it does not having any problems or I'm just too bad in it.
The output of the web: http://tinypic.com/r/2hdnypd/9
By the way, this code will be change a bit so that it will becomes leaderboard. And I have another problem, how should I do if I want to see where my record ranked, while the code $sql = "select users.username, result.speed, result.distance FROM result, users where result.id = users.id order by result.speed desc limit 10";?

Your db columns are id,speed and distance but while fetching records and displaying you are using it as ID, Speed and Distance.
So please change this part:
while($record = mysql_fetch_array($data)){
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['speed'] . "</td>";
echo "<td>" . $record['distance'] . "</td>";
echo "</tr>";
}

Related

Table not displaying any results in a array-query

I'm trying to display the results of a database on a webpage. Using this example, I have come up with this piece of php code below:
<?php
$con=mysqli_connect("217.199.187.70","cl52-domains","######","cl52-domains");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$results = mysqli_query($con, "SELECT * FROM domains");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Domain</th>
<th>Address</th>
<th>Price</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['domain_name'] . "</td>";
echo "<td>" . $row['domain_address'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
However, when I refresh my webpage, it shows the table header (no errors), but 0 results. Here is my table specifications:
Can someone tell me why I'm not getting any results?
Change
while($row = mysqli_fetch_array($result))
To
while($row = mysqli_fetch_array($results))
You probably made a typo in referencing the $results variable.

Displaying data from database in PHP, first and last name going into same column

I'm trying to display members from my database using PHP, however the last name goes in with the first name and I'm not quite sure why... Could anyone point me in the right direction?
<?php
$mysql_db_hostname = "localhost";
$mysql_db_user = "alex";
$mysql_db_password="";
$mysql_db_database="gym";
$con = mysql_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password) or die("Could not connect database");
mysql_select_db($mysql_db_database, $con) or die("Could not select database");
$query = mysql_query("select * from users WHERE Category='Member'");
echo "<table border=1>
<tr>
<th>Users ID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>";
while($row =mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>" . $row['user_id']."<br>" . "</td>";
echo "<td>" . $row['First_Name']."<br>" . "</td";
echo "<td>" . $row['Last_Name']."<br>" . "</td";
echo "</tr>";
}
echo "</table";
?>
You aren't closing the tags. Syntax highlighter would show the error.
while($row =mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>" . $row['user_id']."<br>" . "</td>";
echo "<td>" . $row['First_Name']."<br>" . "</td"; <-----------
echo "<td>" . $row['Last_Name']."<br>" . "</td"; <------------
echo "</tr>";
}
You are not closing your <td> tags properly
echo "<td>" . $row['First_Name']."<br>" . "</td>";
//^here
echo "<td>" . $row['Last_Name']."<br>" . "</td>";
//^and here
So the output is all one cell with both variables printed inside
You forgot to close the td on your first name field.
Just change
</td
to
</td>

Table ID in PHP

I'm trying to assign anID to the table I'm generating with PHP but it keeps returning an error. Here is the full code that works fine so far. All I want is to add an 'id' to the table so I can apply css styles to it in the relevant sheet
<?php
$con=mysqli_connect("localhost","<un>","<pw>","monitor");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM presnationalresults ORDER BY Percentage DESC");
echo "<table border='1'>
<tr>
<th>President</th>
<th>Party</th>
<th>Votes</th>
<th>Percentage</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['PresidentName'] . "</td>";
echo "<td>" . $row['PartyCode'] . "</td>";
echo "<td>" . $row['Votes'] . "</td>";
echo "<td>" . $row['Percentage'] . "</td>";
}
echo "</table>";
mysqli_close($con);
Any help? maybe I'm going about it the wrong way?
Please try below block of code . I have added table id and also close </tr> inside while loop
<?php
$con = mysqli_connect("localhost", "<un>", "<pw>", "monitor");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM presnationalresults ORDER BY Percentage DESC");
echo "<table border='1' id='table-id'>
<tr>
<th>President</th>
<th>Party</th>
<th>Votes</th>
<th>Percentage</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['PresidentName'] . "</td>";
echo "<td>" . $row['PartyCode'] . "</td>";
echo "<td>" . $row['Votes'] . "</td>";
echo "<td>" . $row['Percentage'] . "</td>";
echo "</tr>"; // you forget close tr
}
echo "</table>";
mysqli_close($con);

Add SQL data to URL with PHP

I have some basic code which selects data from a database and echo's it into a HTML table and also add's a link to the echo'd out data. This all works fine. What I want to do is add another piece of data from my database (product_id) to the URL. Here is the code:
<?php
$con=mysqli_connect("localhost","root","","db_test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tbl_products");
echo "<table border='1'>
<tr>
<th>Products:</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><a href='product.php?id='>" . $row['product_name'] . "</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I want to add product_id from my database at the end of
<a href='product.php?id='
so the product_id becomes the ID of the page. How would I do this? I have experimented but it has resulted in numerous errors. I am sure this is a simple syntax thing but it' s bugging me.
You are closing the tag A before placing your data into href.
I think it should work:
echo "<table border='1'>
<tr>
<th>Products:</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['product_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
Answer back if it doesn't work.
The problem is you should put the '> after " . $row['product_name'] . " so that the line would read:
echo "<td><a href='product.php?id=" . $row['product_name'] . "'>" . $row['product_name'] . "</a></td>";

How to capture a drop-down selection and insert into DB?

How can I insert a drop-down value into a DB? I have a form that selects a list of people. And with them i have their unique id showing. So populating isn't he problem, but inserting is... any ideas?
Code:
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ajax_demo", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<option value="1">John Doe</option>
You get the 1 via post, I don't see where's the problem with inserting that into the database.
Update after adding code to the question:
Please show how you generate the options, this code seems ok. One thing though: you should escape $q before passing it to the query like this: mysql_real_escape_string($q). It returns a string, so you can just concatenate it to the query.
Read about SQL injection attacks.

Categories