QUERY works in phpyMyAdmin but not in PHP - php

My Query don't work in PHP but when I test it in my database it works well.
Code:
<?php
//Database connectie
$link = mysqli_connect("*", "*", "*", "*") or die("Error ". mysqli_error($link));
ini_set('memory_limit', '1024M'); // or you could use 1G
//Query //
$query = "SET SQL_BIG_SELECTS = 1;";
$query = "SELECT * FROM datakram, datakram2, datakram3, datakram4 WHERE datakram.ISIN = datakram2.ISIN AND datakram2.ISIN = datakram3.ISIN AND datakram3.ISIN = datakram4.ISIN";
$result = mysqli_query($link, $query);
?>
<table>
<thead>
<tr class="warning">
<th>Naam</th>
<th>ISIN</th>
<th>Jaar</th>
<th>Closely Held Shares</th>
<th>Outstanding Sahres</th>
<th>Market capitalistion</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row['company-name'];?></td>
<td><?php echo $row['ISIN'];?></td>
<td>2017</td>
<td><?php echo $row['2017'];?></td>
<td><?php echo $row['2017-shares'];?></td>
<td><?php echo $row['2017-market'];?></td>
</tr>
<?php
}
?>
Knows somebody why? I have tried: $result = mysqli_multi_query($link, $query); But that didn't work.
I have fix it, don't tell me how ;) but it works. Here if somebody need it:
//Database connectie
$link = mysqli_connect("*", "*", "*", "*") or die("Error ". mysqli_error($link));
ini_set('memory_limit', '1024M'); // or you could use 1G
//Query //
$query = "SET SQL_BIG_SELECTS = 1;";
$query .= "SELECT * FROM datakram, datakram2, datakram3, datakram4 WHERE datakram.ISIN = datakram2.ISIN AND datakram2.ISIN = datakram3.ISIN AND datakram3.ISIN = datakram4.ISIN";
if (mysqli_multi_query($link, $query)) {
do {
?>
<table>
<thead>
<tr class="warning">
<th>Naam</th>
<th>ISIN</th>
<th>Jaar</th>
<th>Closely Held Shares</th>
<th>Outstanding Sahres</th>
<th>Market capitalistion</th>
</tr>
</thead>
<tbody>
<?php
if ($result = mysqli_store_result($link)) {
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row['company-name'];?></td>
<td><?php echo $row['ISIN'];?></td>
<td>2017</td>
<td><?php echo $row['2017'];?></td>
<td><?php echo $row['2017-shares'];?></td>
<td><?php echo $row['2017-market'];?></td>
</tr>
<?php
}
}
} while (mysqli_next_result($link));
}
?>

Related

Add profile avatar to the leaderboard

I recently made a leaderboard and I would like to make some changes.
This is the code :
<table>
<tr>
<td></td>
<td>Name</td>
<td>Likes</td>
</tr>
<?php
$con=mysqli_connect("localhost", "user", "pass", "database");
$sql = ("SELECT username, likes FROM users ORDER BY likes DESC LIMIT 5");
$result = mysqli_query($con, $sql);
$rank = 1;
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>{$rank}</td>
<td>{$row['username']}</td>
<td>{$row['likes']}</td>
</tr>";
$rank++;
}
}
?>
</table>
Now, what I would like to do is add instead of 'username' the avatar of each user with 'alt' for username. E.g :
<img src="<?php echo $example->avater;?>" alt="<?php echo $example->username;?>" />
This is the location of avatar in mysql !
I also have a css code created for the avatar to appear in a round circle.That means we will use
<div class="pro_usrs_container">
I tried but I don't understand how to make the codes so that I can integrate them in <td> instead of <td> $ row ['username'] </td>
Thank you very much !
Select avater in your Query and use it in your table.
<table>
<tr>
<td></td>
<td>Name</td>
<td>Likes</td>
</tr>
<?php
$con=mysqli_connect("localhost", "user", "pass", "database");
$sql = ("SELECT username, avater, likes FROM users ORDER BY likes DESC LIMIT 5");
$result = mysqli_query($con, $sql);
$rank = 1;
if (mysqli_num_rows($result)) :
while ($row = mysqli_fetch_assoc($result)) : ?>
<tr>
<td><?php echo $rank; ?></td>
<td><img src="<?php echo $row['avater'];?>" alt="<?php echo $row['avater'];?>" /></td>
<td><?php echo $row['likes']; ?></td>
</tr>
<?php $rank++;
endwhile;
endif;
?>
</table>
Based on the requirement above:
<table>
<tr>
<td></td>
<td>Name</td>
<td>Likes</td>
</tr>
<?php
$con=mysqli_connect("localhost", "user", "pass", "database");
$sql = ("SELECT avater, username, likes FROM users ORDER BY likes DESC LIMIT 5");
$result = mysqli_query($con, $sql);
$rank = 1;
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>{$rank}</td>
<td><img src="{$row['avater']}" alt="{$row['username']}" /></td>
<td>{$row['likes']}</td>
</tr>";
$rank++;
}
}
?>

TWO MySQL commands (SELECT to View), but only one is showing output

I have a table and each , I want to select a data from the same table in my database.
For example, first <td> is first name, then the second <td> is phone number.
I got the command, but only the first command is showing output.
This is my php codes to open and connect to the database :
<?php
include("./inc/db_connect.php");
$conn = OpenCon();
?>
This is the php codes for the table including <th> and <td> :
<div class="layer55">
<h3>
<table class="flat-table">
<tbody>
<tr>
<th>
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if ($result = $conn->query($sql)) {
if ($result->num_rows > 0) {
echo "Name";
}
}
?>
</th>
<th>
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if ($result = $conn->query($sql)) {
if ($result->num_rows > 0) {
echo "Phone Number";
}
}
?>
</th>
</tr>
<tr>
<td>
<?php
$sql = "SELECT first_name FROM sharp_emp WHERE employee_id = 'AA170336'";
while ($row = $result->fetch_array()) {
echo "" . $row['first_name'] . "";
}
?>
</td>
<td>
<?php
$sql = "SELECT phone FROM sharp_emp WHERE employee_id = 'AA170336'";
while ($row = $result->fetch_array()) {
echo "" . $row['phone'] . "";
}
?>
</td>
</tr>
</tbody>
</table>
</h3>
</div>
This is the php codes for db_connect.php :
<?php
function OpenCon()
{
$dbhost = "localhost";
$dbuser = // Hidden;
$dbpass = // Hidden;
$db = "sharp_db";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
return $conn;
}
function CloseCon($conn)
{
$conn -> close();
}
?>
The expected output :
|----------|----------|
|Name |Phone Number|
|----------|----------|
|John |179898765 |
The current output :
|----------|----------|
|Name |Phone Number|
|----------|----------|
|John |Null (empty) |
You are running the same query multiple times, overwriting the $result variable for no reason, having useless $sql for the later 2 fetch without using them, and fetching a single $result twice by mistake.
So there are multiple concept problem with your code. I think your current code is something equivalant to this:
<div class="layer55">
<h3>
<table class="flat-table">
<tbody>
<tr>
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if ($result = $conn->query($sql)) {
if ($result->num_rows > 0) {
?>
<th>Name</th>
<th>Phone Number</th>
<?php } else { ?>
<th></th>
<th></th>
<?php } ?>
<?php } ?>
</tr>
<tr>
<?php if ($row = $result->fetch_array()) { ?>
<td><?php echo "" . $row['first_name'] . ""; ?></td>
<td><?php echo "" . $row['phone'] . ""; ?></td>
<?php } else { ?>
<td></td>
<td></td>
<?php } ?>
</tr>
</tbody>
</table>
</h3>
</div>
But frankly, it makes no sense to me to print an empty table when there is no result. So what you need is probably something like this.
<?php
$sql = "SELECT * FROM sharp_emp WHERE employee_id = 'AA170336'";
if (
($result = $conn->query($sql))
&& ($result->num_rows > 0)
&& ($row = $result->fetch_array())
):
?>
<div class="layer55">
<h3>
<table class="flat-table">
<tbody>
<tr>
<th>Name</th>
<th>Phone Number</th>
</tr>
<tr>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['phone']; ?></td>
</tr>
</tbody>
</table>
</h3>
</div>
<?php endif; ?>

Adding delete button into the table

I have to add delete button into each section of the table.
<?php
$link = mysqli_connect("localhost", "root", "", "demo");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$name = mysqli_real_escape_string($link, $_POST['name']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$contact = mysqli_real_escape_string($link, $_POST['contact']);
$sql = "INSERT INTO persons (name, email, contact) VALUES ('$name', '$email', '$contact')";
if(mysqli_query($link, $sql)){
echo "<h2>Records added successfully.The new updated records are...</h2><br>";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$selectData = "select * from persons";
$res = mysqli_query($link, $selectData);
?>
<table border="2">
<tr>
<th>id</th>
<th>Name</th>
<th>Email</th>
<th>Contact</th>
</tr>
</table>
<?php
if(mysqli_num_rows($res)>0){
while($row = mysqli_fetch_assoc($res)){
?>
<tr><td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['contact'];?></td></tr>
<?php
}
}
?>
<tr><th>id</th>
<th>Name</th>
<th>Email</th>
<th>Contact</th>
<th>Delete</th>
</tr>
<?php
if(mysqli_num_rows($res)>0){
while($row = mysqli_fetch_assoc($res)){
?>
<tr><td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['contact'];?></td></tr>
<td><a onClick="delTopicContentRow('<?php echo $row['id'] ?>')"><img src="../admin_assets/images/delete.png"></a></td>
<?php
}
}
The delete javascript function
function delTopicContentRow(topic_content_id)
{
var confirm_delete = confirm("Do you want to delete topic content ?");
if (confirm_delete === true)
{
$(".deleteall"+topic_content_id).addClass("deletealloncomplete");
$.ajax({
type:'POST',
url:'delRow.php',
data:{topic_content_id:topic_content_id},
success:function(data)
{
if(data == 1)
{
$(".deletealloncomplete").fadeOut("slow");
setTimeout(function(){
window.location.reload(1);
}, 2000);
}
else
{
alert("Try again.!");
}
}
});
}
}
You should start with HTML basic http://www.w3schools.com/html/html_basic.asp
<tr><th>id</th>
<th>Name</th>
<th>Email</th>
<th>Contact</th>
<th>Delete</th>
</tr>
<?php
if(mysqli_num_rows($res)>0){
while($row = mysqli_fetch_assoc($res)){
?>
<tr><td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['contact'];?></td></tr>
<td>Delete item
</td>
<?php
}
}
//This last statement passes the id of the function to delete
function delete(){
if(isset($_GET['delete'])){
$id=$_GET['delete'];
$sql="DELETE FROM `trial` WHERE `id` ='$id'";
$stmt=$this->conn->prepare($sql);
$stmt->execute();
}
}
?>

Displaying data in tables depending on group

I have a question in relation to displaying PHP tables that should be straight forward but I cannot get my head around it at the moment so any help would be appreciated, basically what I want to do is display a team of players in a table, but display multiple tables of users with their team name display above it.
What I currently have : http://puu.sh/ilUJp/4a6ae5e47b.png
What I am looking to achieve : http://puu.sh/ilUJ8/7756033517.png
<div class="col-lg-6">
<h3>Team Name Goes Here </h3>
<?php
echo "<table class='table table-striped'>";
echo " <thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
";
while($row = mysqli_fetch_array($result)) {
$teamName = $row['teamName'];
$fName = $row['firstName'];
$surName = $row['surName'];
echo "
<tbody>
<tr>
<td>$teamName</td>
<td>$fName</td>
<td>$surName</td>
</tr>
</tbody>
";
}
echo "</table>";
?>
</div>
with my query :
$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID ";
I know the idea I need to do but cannot get it done, so any help would be appreciated.
Try this code
<?php $teemid=array();
while($row = mysqli_fetch_array($result)) {
if(!in_array($row['teamID'],$teemid)){
array_push($teemid,$row['teamID']);
if(!empty($teemid)){ ?>
</tbody>
</table>
</div>
<?php }
?>
<div class="col-lg-6">
<h3><?php echo $row['teamName']; ?></h3>
<table class='table table-striped'>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php } ?>
<tr>
<td><?php echo $row['teamName']; ?></td>
<td><?php echo $row['firstName']; ?></td>
<td><?php echo $row['surName']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
SQL Query Change as below
$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID ORDER BY u.teamID";
You can do this logic
$teams = "get all teams sql query";
while ($row = mysqli_fetch_array($teams)) {
$teamid = $row['teamid'];
$teamname = $row['teamname'];
$teammemberquery = "select all member in the where team = $teamid sql query";
echo "<table>";
while ($r = mysqli_fetch_array($teammemberquery)) {
$teamName = $r['teamName'];
$fName = $r['firstName'];
$surName = $r['surName'];
echo "
<tbody>
<tr>
<td>$teamName</td>
<td>$fName</td>
<td>$surName</td>
</tr>
</tbody>
";
}
echo "</table>";
}
Try as below (Please replace table column name as yours and mysql to mysqli):
<?php
$link = mysql_connect('localhost', 'root', 'root');
$db_selected = mysql_select_db('test', $link);
$sql = "SELECT t.team_id,t.team,u.fname,u.lname,u.email From users as u INNER JOIN team as t where u.team_id = t.team_id order by t.team_id ";
$result = mysql_query($sql);
?>
<html><head><title>team</title></head><body><div class="col-lg-6">
<?php
echo "<table>";
$teamName = "";
$i=0;
while($row = mysql_fetch_array($result))
{
if($teamName == "" || $teamName != $row['team'])
{
if($i!=0)
echo "</table>";
echo "<tr><td colspan='3'><h3>".$row['team']."</h3></td></tr>";
$teamName = $row['team'];
$i=0;
}
$fName = $row['fname'];
$surName = $row['lname'];
$email = $row['email'];
if($i==0)
{
echo "<table class='table table-striped'><tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
}
echo "<tr>
<td>$fName</td>
<td>$surName</td>
<td>$email</td>
</tr>";
$i++;
}
echo "</table>";
?>
</div></body></html>

How can I delete the selected record in mySQL from my HTML/PHP page?

My idea is to click 'Delete' link and it will pass the id to another PHP page (deleteSession.php), and then execute the query in deleteSession.php. but I couldn't seems to get the id from manageSession.php
In manageSession.php,
<table align='center' border='1' cellpadding='5' cellspacing='0'>
<tr>
<th>Session Id</th>
<th>Type</th>
<th>Date & Time</th>
<th>Venue</th>
<th>Pax</th>
<th>Delete</th>
<th>Edit</th>
</tr>
<?php
$sql = "SELECT booking_id, booking_types, dates_sessions, venue_available, room_count FROM bookings_available ORDER BY dates_sessions asc";
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
//mysqli_close($link);
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['booking_id']; ?></td>
<td><?php echo $row['booking_types']; ?></td>
<td><?php echo $row['dates_sessions']; ?></td>
<td><?php echo $row['venue_available']; ?></td>
<td><?php echo $row['room_count']; ?></td>
<td><input type="button" value="Delete"/></td>
<td><input type="button" value="Edit"/></td>
</tr>
<?php } ?>
</table>
In deleteSession.php,
<?php
include "dbFunctions.php";
include "manageSession.php";
//$sql = "SELECT booking_id, booking_types, dates_sessions, venue_available, room_count FROM bookings_available";
//$result = mysqli_query($link, $sql) or die(mysqli_error($link));
$bookingId = filter_input(INPUT_GET, 'booking_id');
$deleteQuery = "DELETE FROM bookings_available WHERE booking_id = '$bookingId'";
?>
I think in deleteSession.php file code should be as follows.
$bookingId = filter_input(INPUT_GET, 'id');
OR
$bookingId = $_GET['id'];
Because you are passing get parameter as follows.
deleteSession.php?id=
And also keep anchor as follows.
Delete
In the deleteSession.php you can try and replace:
$bookingId = filter_input(INPUT_GET, 'booking_id');
with the below code:
$bookingId = $_REQUEST['id'];
Finally at the last line you have to execute the query which is stored in $deleteQuery variable, which is not executed yet by using below code:
$qry = mysql_query("DELETE FROM bookings_available WHERE booking_id = '$bookingId'");
//will show you error if not able to delete
if(!$qry)
die("Error: ".mysql_error());
Added this at line 3 and it works:
mysqli_select_db($link ,$DB);
Because in the code I have not selected the mysql database and also the query was not executing as the first parameter $link was missing.

Categories