Here is my code, what I want to do is display the first names of my friends in another page as Hyperlinks and when a link is clicked I want to display the data.
<?php
$con=mysqli_connect("localhost","root","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM friends");
while($row = mysqli_fetch_array($result))
{
echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}
mysqli_close($con);
?>
<?php
$con=mysqli_connect("localhost","root","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM friends");
while($row = mysqli_fetch_array($result))
{?>php
<a href=<?php "echo "<p>" . $row['FirstName'] . "</p>";"?>> `your page name`</a><?php
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}
mysqli_close($con);
?>
Your file from questions change:
while($row = mysqli_fetch_array($result))
{
echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}
to
while($row = mysqli_fetch_array($result))
{
echo ' . $row['FirstName'] . "";
}
show.php
$con=mysqli_connect("localhost","root","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_GET['UserId'])){
if(is_numeric($_GET['UserId'])){
$UserId = (int)$_GET['UserId'];
$result = mysqli_query($con,"SELECT * FROM friends WHERE UserId= $UserId");
if(count($result)){
while($row = mysqli_fetch_array($result))
{
echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}}}}
Important: I'm not sure what is UserId column name, so change it.
Also, please check for syntax errors.
Try connection it with an incremental ID out of the database. So every row in your friends table must have a unique ID. In that way you can connect it and get the row data from your friends table.
So your friends table must look like this:
friend_id (unique id)
firstname
lastname
email
address
And your code can be something like (this is not save from any attacks):
<?php
$con = mysqli_connect("localhost","root","abc123","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_GET['friendid'])) {
$result = mysqli_query($con,"SELECT * FROM friends");
while($row = mysqli_fetch_array($result)) {
echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
echo "<p>Details</p>";
}
} else {
$result = mysqli_query($con,"SELECT * FROM friends WHERE friend_id = '".$_GET['friendid']."'");
$row = mysqli_fetch_assoc($result);
echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}
mysqli_close($con);
?>
Related
I am using php and mysql to display all the user information of different users and i have a button which gets the id which will be redirected to another page and the id will be displayed in the url. What i am trying to do now is display the user information with the selected id on the redirected page
$sql = "SELECT * FROM users";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
echo "<div class='users-data'>";
echo "<p>" . $row['username'] . "</p>";
echo "<p>" . $row['full_name'] . "</p>";
echo "<p>" . $row['age'] . "</p>";
echo "<p>" . $row['gender'] . "</p>";
echo "<p>" . $row['email'] . "</p>";
echo "<p>" . $row['medical_condition'] . "</p>";
echo "<img src=images/".$row['image'] ."/>";
echo '<td><a href="view-user-information.php?id='.$row['id'].'"><button>View Details</button></td>';
echo "</div>";
}
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
Use this code;
if(isset($_GET['u'])){
$id = $_GET['u'];
$sql = "SELECT * FROM `users` WHERE `id` = '$id'";
$result = mysqli_query ($link,$sql);
$row = mysqli_fetch_assoc($result);
echo "<div class='users-data'>";
echo "<p>" . $row['username'] . "</p>";
echo "<p>" . $row['full_name'] . "</p>";
echo "<p>" . $row['age'] . "</p>";
echo "<p>" . $row['gender'] . "</p>";
echo "<p>" . $row['email'] . "</p>";
echo "<p>" . $row['medical_condition'] . "</p>";
echo "<img src=images/".$row['image'] ."/>"
}
else
{
//you can redirect it to back to the previous page if no id exist in url;
header("LOCATION:index.php"); // change the index.php to your privious page url
}
I apologize before I speak English very little I want to write all of the data in textboxes, but I could not do it
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "vipser";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = ("SELECT * FROM `a_info` WHERE `pax`='" . $_GET['pax'] . "' and `nereden`='" . $_GET['durum'] . "' and `nereye`='" . $_GET['gdurum'] . "'");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
while ($row = $result->fetch_array()) {
echo "<tr>";
echo "<td>" . $row['pax'] . "</td>";
echo "<td>" . $row['a_cinsi'] . "</td>";
echo "<td>" . $row['nereden'] . "</td>";
echo "<td>" . $row['nereye'] . "</td>";
echo "<td>" . $row['saat'] . "</td>";
echo "<td>" . $row['km'] . "</td>";
echo "<td>" . $row['fiyat'] . "</td>";
echo '<td><img src="' . $row["a_resmi"] . '" width="75" height="75"/></td>';
echo "</tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
$conn->close(); ?>
Store the data of the loop in a variable instead of echoing and then assign the variable into textbox's value. Done this here:
<?php
$str = "";
if ($result->num_rows > 0) {
while($row = $result->fetch_array()) {
$str .= . $row['pax'] . " " . $row['a_cinsi'] . " " . $row['nereden'] . " " . $row['nereye'] . " " . $row['saat'] . " " . $row['km'] . " " . $row['fiyat'] . " ";
}
}
?>
<input type="text" value="<?= $str ?>" />
You can put this input inside while if you want different textboxes for each row.
I have two tables that looks like this:
Put it in images since i dont know how to draw a table here.
My problem is that i can't seem to make a query or anything in my php that will allow me to load the report and the 5 images, so that i can display the images where i want to on the page. As i do it now it loads the report five times and one image in each report.
The edited code after #Terminus suggestions
$sql = "
SELECT *
FROM fangstrapporter AS f, rapportbilleder AS r
WHERE f.id=".htmlspecialchars($_GET["id"])." AND r.id=f.id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<b>Overskrift:</b> " . $row["overskrift"] . "<br><br>" .
"<b>Sted:</b> " . $row["sted"] . "<br><br>" .
"<b>Fangstdato:</b> " . $row["fangstdato"] . "<br><br>" .
"<b>Agn:</b> " . $row["agn"] . "<br><br>" .
"<b>Kategori:</b> " . $row["kategori"] . "<br><br>" .
"<b>Art:</b> " . $row["art"] . "<br><br>" .
"<b>Vægt:</b> " . $row["vaegt"] . "<br><br>" .
"<b>Længde:</b> " . $row["laengde"] . "<br><br>";
do {
echo "<a href='" . $row["image_path"] . "'><img src='" . $row["image_thumb_path"] . "'></a><br><br>";
} while($row = $result->fetch_assoc());
echo $row["beskrivelse"]."<br>";
} else {
echo "0 results";
}
Can anyone help me do this? I have been searching on google for four days now, without any success.
Do as #Kenney suggested and remove the part where you echo the report from the loop.
$sql = "
SELECT *
FROM fangstrapporter AS f, rapportbilleder AS r
WHERE f.id=".htmlspecialchars($_GET["id"])." AND r.id=f.id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<b>Overskrift:</b> " . $row["overskrift"] . "<br><br>" .
"<b>Sted:</b> " . $row["sted"] . "<br><br>" .
"<b>Fangstdato:</b> " . $row["fangstdato"] . "<br><br>" .
"<b>Agn:</b> " . $row["agn"] . "<br><br>" .
"<b>Kategori:</b> " . $row["kategori"] . "<br><br>" .
"<b>Art:</b> " . $row["art"] . "<br><br>" .
"<b>Vægt:</b> " . $row["vaegt"] . "<br><br>" .
"<b>Længde:</b> " . $row["laengde"] . "<br><br>";
$beskrivelse = $row["beskrivelse"];
do {
echo "<a href='" . $row["image_path"] . "'><img src='" . $row["image_thumb_path"] . "'></a><br><br>";
} while($row = $result->fetch_assoc());
echo $beskrivelse . "<br>";
} else {
echo "0 results";
}
I am trying to display a link in a php document. the data is stored in mysql. I have stored the url in the field course_url.
i can get the page to show the hyperlink asd a plain text but want it to show ashyperlink with "Click Here" anchor text. The coding i got so far is:
<?php
$con=mysqli_connect("localhost","root","","mentertraining");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT `coursedates`.`coursedate_id`,`coursedates`.`course_id`,`coursedates`.`date1`,`courses`.`course_title`,`courses`.`course_url`,`courses`.`no_of_days` FROM coursedates\n"
. "LEFT JOIN `mentertraining`.`courses` ON `coursedates`.`course_id` = `courses`.`course_id` LIMIT 0, 30 ";
$result = mysqli_query($con,$query);
echo "<table border='1'><tr><th>Course Title</th><th>Course Date</th><th>No of Days</th><th>Course URL</th></tr>";
while($row = mysqli_fetch_assoc($result))
{
$date = new DateTime($row['date1']);
$row['date1'] = $date->format('d/m/Y');
echo "<tr>";
echo "<td>" . $row['course_title'] . "</td>";
echo "<td>" . $row['date1'] . "</td>";
echo "<td>" . $row['no_of_days'] . "</td>";
echo "<td>""<a href=" . $row['course_url'] . >"'Click Her'"</a>""</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Your echo is malformed on this line:
echo "<td>""<a href=" . $row['course_url'] . >"'Click Her'"</a>""</td>";
It should be:
echo "<td><a href='" . $row['course_url'] . "'>Click Here</a></td>";
You have used the wrong "' quotations
while($row = mysqli_fetch_assoc($result))
{
$date = new DateTime($row['date1']);
$row['date1'] = $date->format('d/m/Y');
echo "<tr>";
echo "<td>" . $row['course_title'] . "</td>";
echo "<td>" . $row['date1'] . "</td>";
echo "<td>" . $row['no_of_days'] . "</td>";
echo "<td><a href='" . $row['course_url'] ."'>Click Her</a></td>";
echo "</tr>";
}
I want to print mysql_query result in a table. I know how to do it but I am just confused. I tried this.
<?php
mysql_connect("localhost","root","") or die("Could not Connect.");
mysql_select_db("check") or die("Could not Select DB");
$table = "cc";
$i = 1;
$query = "select * from $table";
$sql = mysql_query($query);
if($sql){
echo "<table border='5'><tr>";
while($i<=2 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=4 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=6 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=8 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
echo "</tr></table>";
}
?>
As you can see it is written again and again with a slight change of 2,4,6,8 in the while loop. It works but the problem is I cant rewrite it again and again as when the website will go live it will have more than 1000 entries. Could You guys help me out by suggesting another way to do this?
""** I need it to be like these dots (dots represent records in the database) **"""
. . . .
. . . .
. . . .
THANKS in Advance.
Ramzy
<?php
mysql_connect("localhost","root","") or die("Could not Connect.");
mysql_select_db("check") or die("Could not Select DB");
$table = "cc";
$query = "select * from $table";
$sql = mysql_query($query);
if($sql){
echo "<table border='5'><tr>";
while($row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
echo "</tr></table>";
}
?>
while($row = mysql_fetch_array($sql)) {
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
I don't really see what's the problem here.
By the way you should never call you're array like this $row[id] but you should quote the key instead $row['id']; Because if a constant id exists it will screw up your code and also for performance reason.
Just use
$limit = 1000;//place any value you need here to limit the number of rows displayed
while ($i<=$limit && $row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
++$i;
}
Also, that limit is unnecessary if all you want is to flush every record to the output. You could just do
while ($row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
And it will stop as soon as there are no more records.
To print all database rows into an HTML-table, use:
echo '<table>';
$i = 0; // $i is just for numbering the output, not really useful
while($row = mysql_fetch_array($sql))
{
echo '<tr><td>' . $i . ' - ' . $row['id'] . ' : ' . $row['name'] . '</td></tr>';
$i++;
}
echo '</tr></table>';
here is a general function I use:
function query_result_to_html_table($res, $table_id = NULL, $table_class = NULL, $display_null = true)
{
$table = array();
while ($tmp = mysql_fetch_assoc($res))
array_push($table, $tmp);
if (!count($table))
return false;
echo "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" "
. ($table_id ? "id=\"$table_id\" " : "")
. ($table_class ? "class=\"$table_class\" " : "") . ">";
echo "<tr>";
foreach (array_keys($table[0]) as $field_name) {
echo "<th>$field_name";
}
foreach ($table as $row) {
echo "<tr>";
foreach ($row as $col => $value) {
echo "<td>";
if ($value === NULL)
echo "NULL";
else
echo $value;
}
echo "\n";
}
echo "</table>\n";
return true;
}
I Got The Answer.. I wanted it to be like this. I made this and It Actually Works.
<?php
$i = 1;
mysql_connect("localhost" , "root" , "") or die('Could not Connect.');
mysql_select_db("db") or die('Could not select DB.');
$query = "select * from `check`";
$sql = mysql_query($query) or die(mysql_error());
echo "<table border='5' width='50%'><tr><th>Name</th><th>Gender</th></tr></table><table border='5' width='50%'><tr>";
if($i<3){
echo "<td align='center'>".$row['name']."</td>";
echo "<td align='center'>".$row['gender']."</td>";
++$i;
} else {
echo "<td align='center'>".$row['name']."</td><td align='center'>".$row['gender']."</td>";
echo "</tr>";
$i = 1;
echo "<tr>";
}
}
echo "</table>";
?>
</div>
Thank You Guys For Your Support