I have question about lookup data from a table,...
there is a table that save 4 fields from users : id, name, username, email
I have another table that save 10 more not required fields from users : phone, and...
now I want to lookup the Id from table 1 in table 2 and show the phone number in the table too.
Note : ID is a shared code between table 1 & 2
<?php
$con=mysqli_connect("localhost","blah","blah","blah");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Here is 2 tables
mysqli_query($con, "SET NAMES 'utf8'");
$result = mysqli_query($con,"SELECT * FROM q7fbn_users");
$result2 = mysqli_query($con,"SELECT * FROM q7fbn_user_profiles");
echo "<table border='1', table style='float:center' align='center'>
<tr>
// Here is the table 1 fields
<th>ID</th>
<th>Name</th>
<th>Username</th>
<th>E-mail</th>
// Here is the phone number that i want lookup from table 2
<th>Phone</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr align='center'>";
// Here is the table 1 fields
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
// Here is the phone number that i want lookup from table 2
echo "<td>" . $row['phone'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Your problem is that, that in your row there will be only the data from the user table, because you are fetching data from the $result only.
You need to join them:
$result = mysqli_query($con, "SELECT * FROM q7fbn_users LEFT JOIN q7fbn_user_profiles ON q7fbn_users.id = q7fbn_user_profiles.userId");
Of course you need to change the q7fbn_user_profiles.userId to your name of your field what conatins your user id in that table.
EDIT:
Based on OP table structure of the profile:
$con = mysqli_connect("localhost", "blah", "blah", "blah");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Here is 2 Database
mysqli_query($con, "SET NAMES 'utf8'");
$sql = "SELECT * FROM q7fbn_users";
$result = mysqli_query($con, $sql);
echo "
<table border='1', table style='float:center' align='center'>
<tr>
<!-- Here is the database 1 fields -->
<th>ID</th>
<th>Name</th>
<th>Username</th>
<th>E-mail</th>
<!-- Here is the phone number that i want lookup from database 1 -->
<th>Phone</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
//Now you can use the $row['id'] what is the id of the user
//so create another query to get the phonenumber.
$sql = "SELECT profile_value FROM q7fbn_user_profiles"
. " WHERE user_id = ". $row["id"] . ""
. " AND profile_key = 'profile.phone'";
$profileRes = mysql_query($con, $sql);
//Get the phonenumber from the table2
$phoneRow = mysqli_fetch_assoc($profileRes);
//Initialize the phonenumber, maybe there are not phonnumber for this user.
$phoneNumber = '';
if ($phoneRow) {
$phoneNumber = $phoneRow['profile_value'];
}
echo "<tr align='center'>";
// Here is the database 1 fields
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
// Here is the phone number that i want lookup from database 1
echo "<td>" . $phoneNumber . "</td>";
echo "</tr>";
}
echo "</table>";
Related
//replace the following with your details. Dbname is your username by default.
$con = mysqli_connect("info20003db.eng.unimelb.edu.au","geehwank","2058","geehwank");
// Check connection
if (mysqli_connect_errno()) {
echo "Could not connect to MySQL for the following reason: " . mysqli_connect_error();
}
/* this lists the name and release date of all action movies */
echo "<table border='1'>";
$result = mysqli_query($con,"SELECT name, release_date FROM Movie WHERE genre = 'action'");
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td><td>" . $row['release_date'] . "</td>";
echo "</tr>";
}
mysqli_close($con);
This code is from my uni
and it reads in tables from mysql
and displays them in a table
im a php noob
ive been trying to add headnings to the table called
name, releasedate
how can i do this in code??
can anyone help??
Here you go:
echo "<table border='1'><thead><tr><th>Name</th><th>Release Date</th></tr></thead><tbody>";
$result = mysqli_query($con,"SELECT name, release_date FROM Movie WHERE genre = 'action'");
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td><td>" . $row['release_date'] . "</td>";
echo "</tr>";}
mysqli_close($con);
?>
</tbody></table>
Try as below :
<html>
<head><title>Lab F</title></head>
<body>
<h1> Lab F - SELECT Example</h1>
<p>
This PHP code runs an SQL query, and displays the answers in an HTML table.
</p>
<p>
<br>
<?php
//replace the following with your details. Dbname is your username by default.
$con = mysqli_connect("info20003db.eng.unimelb.edu.au","geehwank","2058","geehwank");
// Check connection
if (mysqli_connect_errno()) {
echo "Could not connect to MySQL for the following reason: " . mysqli_connect_error();
}
/* this lists the name and release date of all action movies */
echo "<table border='1'>";
$result = mysqli_query($con,"SELECT name, release_date FROM Movie WHERE genre = 'action'");
echo "<tr><td>Name</td><td>Release Date</td></tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td><td>" . $row['release_date'] . "</td>";
echo "</tr>";}
mysqli_close($con);
?>
</table>
</body>
</html>
I have php generated table that displays data as follow;
ID Name
1 xxxx
2 xxxx
I would like to be able to click on ID number and display information associated with the ID on separate page
Ive got so far:
table.php
include("connection.php");
$con=mysql_select_db('fm', $con);
$query = "SELECT * FROM table ;
$result = mysql_query($query);
echo "<table>
<tr>
<th>ID</th>
<th>Location</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr><td><a href='send.php?=" . $row['id'] . "'>" . $row['id'] . "</a></td><td>" . $row['location'] . "</td></tr>";
}
echo "</table>";
mysql_close();
?>
info.php
include ("connection.php");
$con=mysql_select_db('fm', $con);
$id=$_GET['id'];
$query = "SELECT * FROM table WHERE id=". $id;
$result = mysql_query($query) or die (mysql_error());
echo "<table>
<tr>
<th>ID</th>
<th>Property</th>
<th>Location</th>
<tr>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['property'] . "</td></td>" . $row['location'] . "</td></tr>";
var_dump($row);
}
echo "</table>";
$result = mysql_query($sql);
mysql_close($con);
Any input will be appreciated
you have got a mistake here :
echo "<tr><td><a href='info.php?id=" . $row['id'] . "</td><td>" . $row['location'] . "</td></tr>";
you need to end the <a> element like this:
echo "<tr><td><a href='info.php?id=".$row['id']."'>".$row['id']."</a></td><td>" . $row['location'] . "</td></tr>";
Also you should use IF statement in the info.php because if you access it like :
info.php // without ?id=
you will have undefined variable $id. And its vulberable to mysql injection when you dont process the variable before using it in select.
You got mistake here in info.php in this line:
$result = mysql_query($query, $con);
it should look like this :
$result = mysql_query($query);
You got it right in table.php.
I would change the echo command like this
echo "<tr><td>{$row['id']}</td><td>{$row['property']}</td></td>{$row['location']}</td></tr>";
The double quotes will take care of the values of the variables.
Sorry if my english is not correct. I will try my best..
The question is how can I insert record from existing table with insert button. Everything work fine except variable $book. It records value 0 to database. Here is code..
//if the user press "INSERT BOOK" button there will be new record in database
<?php
$reader=$_SESSION['user_id'];
$book=$row['book_id'];
if (isset($_POST['update'])){
$sql = "INSERT INTO `read` (`read_id`, `book`, `reader`) VALUES (LAST_INSERT_ID(), '$book' '$reader')";
$result = mysql_query($sql) or die(mysql_error());
if ($result) {
echo " Yes, it works!";
}
else{
echo "noup!";
}
}
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
if(preg_match("/^[ a-zA-Z]+/", $_POST['name'])){
$name=$_POST['name'];
$result = mysql_query("SELECT * FROM book b JOIN `read` r ON (b.book_id = r.book) JOIN users u ON (r.reader = u.user_id) WHERE b.book_name LIKE '%" . $name . "%' or b.writer LIKE '%" . $name . "%' AND u.user_id !=". $_SESSION['user_id']);
$num_rows = mysql_num_rows($result);
if($num_rows>=1){
echo "<table id='table1'>
<tr>
<th>book_id</th>
<th>BOOKNAME</th>
<th>WRITER</th>
<th>PAGES</th>
</tr>";
while($row=mysql_fetch_array($result)){
echo "<form action=add_book.php method='post'>";
echo "<tr>";
echo "<td>" . $row['book_id'] . "</td>";
echo "<td>" . $row['book_name'] . "</td>";
echo "<td>" . $row['writer'] . "</td>";
echo "<td>" . $row['pages'] . "</td>";
//echo "<td>" . "<input type = 'hidden' name = 'hidden' value =" . $row['book_id'] . " </td>";
echo "<td>" . "<input class = 'field' type = 'submit' name = 'update' value = 'INSERT BOOK'>" . " </td>";
echo "</tr>";
echo "</form>";
}//WHILE LOOP
echo "</table>";
}
else{
echo "NO RESULTS";
}//else
}//if(preg_match..)
} //if(isset($_GET['go'])){
} //if(isset($_POST['submit'])){
?>
I want to show all data from my table.
But if I use/add ORDER BY id DESC or any code after $sql="SELECT * FROM $tbl_name, then last row is not showing.
<?php
include "db.php";
$tbl_name="report"; // Table name
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
$ro = mysql_fetch_array($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count>=1) {
echo "<table border='1' align='center' cellpadding='10'>
<tr>
<th>Reporter</th>
<th>Message</th>
<th>Reporter Ip Address</th>
<th>Action</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['from'] . "</td>";
echo "<td>" . $row['msg'] . "</td>";
echo "<td>" . $row['to'] . "</td>";
echo "<td class='middle'>" . $row['ip'] . "</td>";
echo "<td><a class=\"confirmation\" href=\"report_delete.php?id=" . $row['id'] . "\">Delete</a></td>";
echo "</tr>";
}
echo "</table>";
}
else {
print "<p align='center'>Nothing found.</p>";
}
?>
Of course when you used the DESC, it starts off the highest ID. Then the invocation of:
$ro = mysql_fetch_array($result); // this is the first row.
It fetches the first row.
Then your loop: while($row = mysql_fetch_array($result)) starts off with the second row.
So just remove this $ro = mysql_fetch_array($result); unneeded fetching line.
Obligatory Note:
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
Sample PDO Usage:
<?php
$db = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');
$query = $db->query('SELECT * FROM report ORDER BY id DESC');
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
if(count($rows) > 0) {
echo "
<table border='1' align='center' cellpadding='10'>
<tr>
<th>Reporter</th>
<th>Message</th>
<th>Reporter Ip Address</th>
<th>Action</th>
</tr>
";
foreach($rows as $row) {
echo "<tr>";
echo "<td>" . $row['from'] . "</td>";
echo "<td>" . $row['msg'] . "</td>";
echo "<td>" . $row['to'] . "</td>";
echo "<td class='middle'>" . $row['ip'] . "</td>";
echo "<td><a class=\"confirmation\" href=\"report_delete.php?id=" . $row['id'] . "\">Delete</a></td>";
echo "</tr>";
}
echo '</table>';
} else {
echo "<p align='center'>Nothing found.</p>";
}
?>
You have an extra mysql_fetch_array($result); before the loop.
You must generete the Sql query String correctly. Like this:
$sql = "SELECT * FROM ".$tbl_name." ORDER BY id DESC";
In Php there are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side.
More info -> http://php.net/manual/en/language.operators.string.php
Hope it helps.
I need to filter a session table, normally this is how I show the table list.
<?php
$con=mysqli_connect("localhost","root","","esc");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM sponsor ;");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>PIN</th>
<th>Author</th>
<th>Author ID</th>
<th>Entry Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['sid'] . "</td>";
echo "<td>" . $row['spin'] . "</td>";
echo "<td>" . $row['spuname'] . "</td>";
echo "<td>" . $row['suid'] . "</td>";
echo "<td>" . $row['sdate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I was wondering if it's possible on this part...
$result = mysqli_query($con,"SELECT * FROM sponsor ;");
to add Where spuname = 'spuname'
I guess the way I'm doing it is wrong.
What I wanted to do is to show filtered tables, so the one that has the session will see only his logs.
mysqli_query("SELECT * FROM sponsor Where spuname = 'spuname'",$con);
try putting $con after your query
You can use where clause in your query. it would be
$result = mysqli_query($con,"SELECT * FROM sponsor Where spuname = 'spuname'");