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'");
Related
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 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>";
I'm actually a little bit confused with all these AJAX techniques. The aim is to display the new rows on my site when new rows are inserted into my database, but i dont know how. The following code has been inserted into my Joomla site threw an extension:
<!-- You can place html anywhere within the source tags -->
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
$today = date("d/m/Y");
$sql = "SELECT * FROM queue WHERE";
$sql =$sql . " Date>='$today' ORDER BY Qnumber Desc LIMIT 3";
// echo $sql;
$con=mysqli_connect("localhost","root","db","pass");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Αριθμός Εξυπηρέτησης</th>
<th>Πελάτες σε Αναμονή</th>
<th>Ώρα</th>
<th>Ημερομηνία</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Qnumber'] . "</td>";
echo "<td>" . $row['WaitingCustomers'] . "</td>";
echo "<td>" . $row['Time'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I want to create a page where I have to show records of an specific id but it is showing me this error
Notice: Undefined variable: mysqli in line 82.
Fatal error: Call to a member function query() on a non-object in line 82.
this is PHP code of program.
$con=mysqli_connect("XXXX","XXXX","XXXX","XXXX");
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$id = $_GET['id'];
$sql = "SELECT * FROM details WHERE cat_id = $id";
$result = mysqli_query($con,$sql); //line #82
$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){
$id = $row['cat_id'];
echo "<tr>";
echo "<td><a href='detail.php?id=$id' >" . $row['cat_name'] . "</a></td>";
echo "</tr>";
}
echo "<table>";
echo "<tr>";
echo "<th>name</th>";
echo "<th>address</th>";
echo "<th>phone</th>";
echo "<th>uan</th>";
echo "<th>location</th>";
echo "</tr>";
mysqli_close($con);
You are not loop through record correctly like this:
$id = $_GET['id'];
$result = mysqli_query($con,"SELECT * FROM details WHERE cat_id = $id");
echo "<table>
<tr>
<th>name</th>
<th>address</th>
<th>phone</th>
<th>uan</th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['uan'] . "</td>";
echo "</tr>";
}
$con=mysqli_connect("XXXX","XXXX","XXXX","XXXX");if (mysqli_connect_errno())
{echo "Failed to connect to MySQL: " . mysqli_connect_error();}$id =$_GET['id'];
$sql = "SELECT * FROM details WHERE cat_id=$id";$result=mysqli_query($con,$sql); //line #82$row = mysqli_fetch_array($result);echo "";echo "";echo "name";echo"address";echo"phone";echo"uan";echo"location";echo"";while($row=mysqli_fetch_array($result))
{$id = $row['cat_id'];echo ""; echo "" . $row['cat_name'] . ""; echo "";
}
mysqli_close($con);
I am trying to display data from mysql using php. Please find the code which I have used. I am only getting the field names in the output. The referred table in the query containing more than 50 rows.
Please help to solve my issue...
<?php
$con=mysqli_connect("localhost","root","password","ayrilmana");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT txn_amount,donated_by FROM tempe_txn");
echo "<table border='1'>
<tr>
<th>txn_amount</th>
<th>donated_by</th>
</tr>";
if($result === FALSE){
die(mysql_error());
}
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['txn_amount'] . "</td>";
echo "<td>" . $row['donated_by'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>