Display output of MySQL query on button badge - php

I am new to php mysql & have this query where i want to display the output of mysql query on button badge. I have tried the following code
Kindly help .
<?php
$con=mysqli_connect("localhost","root","") or die("Failed to connect with database!!!!");
mysqli_select_db($con,"sales");
$sth = mysqli_query($con,"select sum(sal) as Salary from `sales` ");
$result = mysqli_query($con,$sth);
$row = mysqli_fetch_assoc($con,$result);
echo $row;
?>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>
<body>
<button type="button" class="btn btn-primary">Primary <span class="badge"><?php echo $row; ?></span></button>
</body>
</html>

<?php echo ''.$row['Salary'].''; ?>
As long as you have verified the settings and the query is correct the above line should output what you are looking for.
EDIT
<?php
$con=mysqli_connect("localhost","root","") or die("Failed to connect with database!!!!");
mysqli_select_db($con,"sales");
$sth = "select sum(sal) as Salary from `sales` ";
$result = mysqli_query($con,$sth);
$row = mysqli_fetch_assoc($result);
Try That

Related

Retrieving and displaying logged in info in PHP

I'm trying to retrieve my logged in user data to no avail. Please check my
enter code here private function getUserData($user_name)
{
// if database connection opened
if ($this->databaseConnection()) {
// database query, getting all the info of the selected user
$query_user = $this->db_connection->prepare("SELECT * FROM users WHERE user_name='$_SESSION['user_name']'");
$query_user->bindValue(':user_name', $user_name, PDO::PARAM_STR);
$query_user->execute();
// get result row (as an object)
return $query_user->fetchObject();
} else {
return false;
}
}
Got the way to move about it ,Thanks #ADyson for the response and follow up
code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>YOUR ORDER , We will contact you in a few !!!!</title>
<link href="assets/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/css/main.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<?php include 'header.php'; ?>
<section>
<div class="container">
<strong class="title">MY ORDERS</strong>
</div>
<div class="profile-box box-left">
<?php
require('db.php');
// SQL query
$strSQL = "SELECT user_name, phone, firstname, lastname, service, referal,user_registration_datetime FROM users WHERE user_name = '".$_SESSION['user_name']."'";
// Execute the query (the recordset $rs contains the result)
$rs = mysqli_query($myConnection, $strSQL);
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysqli_fetch_array
while($row = mysqli_fetch_array($rs)) {
echo WORDING_PROFILE_PICTURE . '<br/>' . $login->user_gravatar_image_tag;
echo "<div class='info'> <strong>NAME:</strong> <span>".$row['firstname'].", ".$row['lastname']."</span></div>";
echo "<div class='info'><strong>phone No:</strong> <span>".$row['phone']."</span></div>";
echo "<div class='info'><strong>SERVICE:</strong> <span>".$row['service']."</span></div>";
echo "<div class='info'><strong>REFERAL:</strong> <span>".$row['referal']."</span></div>";
echo "<div class='info'><strong>DATE QUERIED:</strong> <span>".$row['user_registration_datetime']."</span></div>";
}
// Close the database connection
mysqli_close($myConnection);
?>
<div class="options">
<a class="btn btn-primary" href="editprofile.php">Edit Profile</a>
<a class="btn btn-success" href="changepassword.php">Change Password</a>
</div>
</div>
</section>
<script src="assets/js/jquery-3.1.1.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
<script src="assets/js/main.js"></script>
</body>
</html>

How to fetch draggable events area from the database in fullCalendar?

I've been working on developing fullCalendar drag and drop events with a resource column. For now, I've hard coded the draggable events area; now trying to fetch it from the database.
The database previously had two tables - Resources and Events. The events after being dropped on the calendar, gets updated in the events table. The resource column is being fetched from the database and for adding new resources, I've built a rooms button which saves the new resources in the resource table.
Till now, I only had five draggable events in the main file, but now I'm working on fetching those from database as well. So, I created one more table in the database named draggableevents. The table contains two columns id and EventName.
Here's the code:
draggableevents.php
<?php
require "connection.php";
$conn = DB::databaseConnection();
$conn->beginTransaction();
$sql = "Select * FROM DraggableEvents";
$stmt = $conn->prepare($sql);
if ($stmt->execute()) {
while($result = $stmt->fetch(PDO::FETCH_ASSOC));
return $result;
} else {
return null;
}
?>
form.php
<head>
<link href='https://unpkg.com/#fullcalendar/core#4.4.0/main.min.css' rel='stylesheet' />
<link href='https://unpkg.com/#fullcalendar/daygrid#4.4.0/main.min.css' rel='stylesheet' />
<link href='https://unpkg.com/#fullcalendar/timegrid#4.4.0/main.min.css' rel='stylesheet' />
<script src='https://unpkg.com/#fullcalendar/core#4.4.0/main.min.js'></script>
<script src='https://unpkg.com/#fullcalendar/interaction#4.4.0/main.min.js'></script>
<script src='https://unpkg.com/#fullcalendar/daygrid#4.4.0/main.min.js'></script>
<script src='https://unpkg.com/#fullcalendar/timegrid#4.4.0/main.min.js'></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<link href='https://unpkg.com/#fullcalendar/timeline#4.4.0/main.min.css' rel='stylesheet' />
<link href='https://unpkg.com/#fullcalendar/resource-timeline#4.4.0/main.min.css' rel='stylesheet' />
<script src='https://unpkg.com/#fullcalendar/timeline#4.4.0/main.min.js'></script>
<script src='https://unpkg.com/#fullcalendar/resource-common#4.4.0/main.min.js'></script>
<script src='https://unpkg.com/#fullcalendar/resource-timeline#4.4.0/main.min.js'></script>
<link rel="stylesheet" href="css/main.css" media="all">
<link href="main.css" rel="stylesheet">
<script src='main.js'></script>
</head>
<?php
require 'draggableevents.php';
?>
<div id='external-events'>
<p>
<strong>Draggable Events</strong>
</p>
<div class='fc-event'><?php $result['EventName']; ?></div>
<p>
<input type='checkbox' id='drop-remove' />
<label for='drop-remove'>remove after drop</label>
</p>
</div>
<div id='calendar-container'>
<div id='calendar'></div>
</div>
The above code results in a blank draggable events area. The draggableevents.php file doesn't seem to be loaded on refreshing the page. I don't see it in the network panel and hence no error related to it.
There are some obvious logical issues, none of which have anything much to do with fullCalendar:
1) I have mentioned this a number of times before in your previous questions: return does not return a value when you aren't inside a function. Where do you imagine you are returning that to, exactly? require doesn't have any way to
2) even if that worked, you'd never return any events, because your while loop is closed and doesn't do anything.
3) $result would be out of scope outside your while loop anyway.
4) You never execute your query
5) You didn't echo the event name.
You need to get all your database results into an array, and then loop through that array to generate as many fc-event divs as there are entries in the array.
Here's one way to do it - I've put the functionality of draggableevents.php into a function which you can call when you need it.
draggableevents.php
<?php
require "connection.php";
function getDraggableEvents() {
$conn = DB::databaseConnection();
$sql = "Select * FROM DraggableEvents";
$stmt = $conn->prepare($sql);
$stmt->execute();
$results = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$results[] = $row;
}
return $results;
}
?>
form.php
<html>
<head>
<link href='https://unpkg.com/#fullcalendar/core#4.4.0/main.min.css' rel='stylesheet' />
<link href='https://unpkg.com/#fullcalendar/daygrid#4.4.0/main.min.css' rel='stylesheet' />
<link href='https://unpkg.com/#fullcalendar/timegrid#4.4.0/main.min.css' rel='stylesheet' />
<script src='https://unpkg.com/#fullcalendar/core#4.4.0/main.min.js'></script>
<script src='https://unpkg.com/#fullcalendar/interaction#4.4.0/main.min.js'></script>
<script src='https://unpkg.com/#fullcalendar/daygrid#4.4.0/main.min.js'></script>
<script src='https://unpkg.com/#fullcalendar/timegrid#4.4.0/main.min.js'></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<link href='https://unpkg.com/#fullcalendar/timeline#4.4.0/main.min.css' rel='stylesheet' />
<link href='https://unpkg.com/#fullcalendar/resource-timeline#4.4.0/main.min.css' rel='stylesheet' />
<script src='https://unpkg.com/#fullcalendar/timeline#4.4.0/main.min.js'></script>
<script src='https://unpkg.com/#fullcalendar/resource-common#4.4.0/main.min.js'></script>
<script src='https://unpkg.com/#fullcalendar/resource-timeline#4.4.0/main.min.js'></script>
<link rel="stylesheet" href="css/main.css" media="all">
<link href="main.css" rel="stylesheet">
<script src='main.js'></script>
</head>
<body>
<div id='external-events'>
<p>
<strong>Draggable Events</strong>
</p>
<?php
require 'draggableevents.php';
$events = getDraggableEvents();
foreach ($events as $event)
{
?>
<div class='fc-event'><?php echo $event['EventName']; ?></div>
<?php
}
?>
<p>
<input type='checkbox' id='drop-remove' />
<label for='drop-remove'>remove after drop</label>
</p>
</div>
<div id='calendar-container'>
<div id='calendar'></div>
</div>
</body>
</html>

mysql query on php doesn't work

code:
<?php
include "init.php";
include_once('tbs_class.php');
$TBS = new clsTinyButStrong;
$TBS->LoadTemplate('/home/b2bmomo/www/templates/templateb2bmomo.htm');
$TBS->Show();
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
$query = "SELECT * FROM information_schema.tables";
$risultati=$db->mysqli_query($query);
while ( $row = $risultati->fetch_row() ){
$table = $row[0];
echo '<h3>'.$table.'</h3>';
}
}
?>
This is the query of my index should print me all the tables in my db which I connect in init.php that comes included , the part is loaded into a html template tiny butstrong , this is the structure of html
code:
<html>
<head>
<title>B2BMOMO</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="/include/css/bootflat.min.css">
<link rel="stylesheet" href="/include/css/app.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/s/bs/pdfmake-0.1.18,dt-1.10.10,af-2.1.0,b-1.1.0,b-colvis-1.1.0,b-html5-1.1.0,b-print-1.1.0,cr-1.3.0,fc-3.2.0,fh-3.1.0,kt-2.1.0,r-2.0.0,rr-1.1.0,sc-1.4.0,se-1.1.0/datatables.min.css"/>
</head>
<body>
<div align="center">ELENCA TABELLE PRESENTI NEL DB</div>
<form method="post" action="index.php" style="text-align: center;">
<INPUT type="submit" value="Invia">
<br><br>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="/include/js/bootstrap-contextmenu.js"></script>
<script type="text/javascript" src="/include/js/bootflat.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/s/bs/pdfmake-0.1.18,dt-1.10.10,af-2.1.0,b-1.1.0,b-colvis-1.1.0,b-html5-1.1.0,b-print-1.1.0,cr-1.3.0,fc-3.2.0,fh-3.1.0,kt-2.1.0,r-2.0.0,rr-1.1.0,sc-1.4.0,se-1.1.0/datatables.min.js"></script>
</body>
</html>
Now , I wish that clicking the button in the index is given me to output the list of tables in the db , but at present the query was
code:
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
$query = "SELECT * FROM information_schema.tables";
$risultati=$db->mysqli_query($query);
while ( $row = $risultati->fetch_row() ){
$table = $row[0];
echo '<h3>'.$table.'</h3>';
}
}
I will not print anything, does anyone have any idea ? thank you
First of all, unless you're using a custom class, I think that you confuse procedural and objected oriented style.
This doesn't work:
$db->mysqli_query( $query );
This works:
$db->query( $query );
In addition, SELECT * FROM information_schema.tables doesn't show all the tables in our database. It display instead all the rows in table TABLES from database information_schema (it's not the same: see more about information_schema tables).
To display a list of all tables from your current database, use this query:
"SHOW TABLES"

Custom PHP Function works except on one specific page

Can someone please help me understand why the below function works in option.php but does not work in main.php? From what I'm seeing, it looks as if the query is not going through when the function is called on main.php; however, it clearly goes through on option.php.
custom_functions.php
function fetch_options(){
global $connection;
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)){
echo "<option value=\"{$row['a']}\">{$row['b']}</option>";
}
mysqli_free_result($result);
mysqli_close($connection);
}
option.php
<?php
require_once('lib/db.php');
require_once('lib/custom_functions.php');
echo "<select><option value=\"0\">Unknown</option>";
fetch_options();
echo "</select>";
?>
main.php
<?php
require_once('lib/db.php');
require_once('lib/custom_functions.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Main</title>
<link href="css/bootstrap.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<?php include('lib/nav.php'); ?>
<div class="container-fluid">
<div class="row">
<div class="col-lg-2" style="padding:none;background-color:#ccc;min-height: calc(100vh - 52px);">
</div>
<div class="col-lg-10" style="padding:none;" id="page2">
<select class="form-control" id="e_CR" name="craft">
<option value="0">Unknown</option>
<?php fetch_options(); ?>
</select>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="js/validate.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/jfunctions.js"></script>
</body>
</html>
The included nav.php file contained a mysqli_close call and terminated the connection before the fetch_options() function was run.

Parse mysql results in PHP and return to Flot

I am pulling data from a MySQL database and plotting with Flot. My problem is in our DB design.
I have 1 table that contains the following columns:
ID (auto incremented) (key)
Timestamp
SensorData (looks like: 34534.3 32452.4 23454.6 6523.45 67546.4 2233)
I would like to parse out my sensor data so I can create Flot plots with individual sensor data. Right now, I am able to plot the ID vs Timestamp and that works. Here is my code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Flot Examples</title>
<link href="layout.css" rel="stylesheet" type="text/css">
<!--[if lte IE 8]><script language="javascript" type="text/javascript" src="js/flot/excanvas.min.js"></script><![endif]-->
<script language="javascript" type="text/javascript" src="js/flot/jquery.js"></script>
<script language="javascript" type="text/javascript" src="js/flot/jquery.flot.js"></script>
</head>
<body>
<h1>Title</h1>
<div id="placeholder" style="width:600px;height:300px;"></div>
<?php
$server = "www.freesql.org";
$user="username";
$password="password";
$database = "database";
$connection = mysql_connect($server,$user,$password);
$db = mysql_select_db($database,$connection);
$query = "SELECT Id, localtme FROM varia";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
$dataset1[] = array($row['Id'],$row['localtme']);
}
?>
<script type="text/javascript">
$(function () {
var dataset1 = <?php echo json_encode($dataset1); ?>;
$.plot($("#placeholder"), [ dataset1 ]);
});
</script>
</body>
</html>

Categories