Download a file in PHP - php

I'm trying to download a PowerPoint file. I'm saving its path in column slide in a table called lesson. Whenever I try to download it, it downloads the whole table.
All I want is the column slide, how can I do that?
// connect to the database
$link = mysql_connect('localhost','root','');
if (!$link) {
die('Could not connect :' . mysql_error());
}
$Selected= mysql_select_db("elearningg", $link);
if (!$Selected) {
die("Could not connect: " . mysql_error());
}
// query the server for the file
$L_ID = $_GET['id'];
$query = "SELECT * FROM lesson WHERE LID = '$L_ID'";
$result = mysql_query($query) or die(mysql_error());
// define results into variables
$name=mysql_result($result,0,"Lname");
$content=mysql_result($result,0,"slide");
header("Content-disposition: attachment; filename=$name");
echo $content;
mysql_close();

If all you want is the column slide, you have to select only that column in your select clause. Right now you're selecting all of the table's columns by using SELECT *.
Try this:
$query = "SELECT slide FROM lesson WHERE LID = '$L_ID'";
That should only return the column slide from table lesson.

Related

Fetching SQL Current date for Graph lib php from timestamp

So i have setup the code to display graph from php sql. But i cant seem to order it by todays date from a timestamp in my table. Here is my code:
<?php
include("phpgraphlib.php");
$graph=new PHPGraphLib(550,450);
$link = mysql_connect('xxx', 'xxx', 'xxx')
or die('Could not connect: ' . mysql_error());
mysql_select_db('test') or die('Could not select database');
$dataArray=array();
//get data from database
// $sql="SELECT rating, COUNT(*) AS 'datentry' FROM main_table GROUP BY rating";
$sql="SELECT rating, COUNT(*) AS 'datentry' FROM main_table GROUP BY rating";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$salesgroup=$row["rating"];
$count=$row["datentry"];
//add to data areray
$dataArray[$salesgroup]=$count;
}
}
//configure graph
$graph->addData($dataArray);
$graph->setTitle("Daily");
$graph->setGradient("lime", "green");
$graph->setBarOutlineColor("black");
$graph->createGraph();
?>
What i want to achieve is to be able to display just todays data. This specific code displays all of the data in my table.

PHP - drop only one table from database

I've searched a long time and didn't found correct answer for this question that i want to delete only one table from database. so i have this:
<?php
$database_name = "XXXXXX";
if (!$link = mysql_connect('XXXXXX', 'XXXXXX', 'XXXXXX')) {
die("Could not connect: " . mysql_error());
}
$sql = "SHOW TABLES FROM $database_name";
if($result = mysql_query($sql)){
while($row = mysql_fetch_row($result)){
$found_tables[]=$row[0];
}
}
else{
die("Error, could not list tables. MySQL Error: " . mysql_error());
}
foreach($found_tables as $table_name){
$sql = "DROP TABLE $database_name.$table_name";
if($result = mysql_query($sql)){
echo "Success - table $table_name deleted.";
}
else{
echo "Error deleting $table_name. MySQL Error: " . mysql_error() . "";
}
}
?>
it will list all tables from database and delete all of theme but i want to delete them one by one. something like this:
delete.php?item=ONE_OF_TABLE
drop table by querystring with table name or ...
First take a look on mysql function deprecated
Then your SQL-Syntax has to be edited by a WHERE table_name LIKE 'ONE_OF_TABLE'
or replace ONE_OF_TABLE by $_GET['item']
The code posted does exactly what you want - it drops tables one by one in a cycle. If you mean - "Show tables to end user, THAN drop them one by one" - you should print the result of SHOW TABLES query and expect some other user interaction.

Display amount of registered users in MySQL database using PHP

I want to display the current amount of users registered in my database (it's called dalton) / the users are stored in a table in that database called simpleauth_players. It stores their name, hash, registerdate, logindate, and lastip.
I want to somehow use a PHP code that (logs me into the database) and displays the current amount of names in the database. So I can display a message like "Hey, there is currently 1,894 registered players!" inside of my HTML/PHP page. I'm kinda a novice it would be awesome if somebody could share the code and instructions.
My code:
$connection = mysql_connect('host', 'username', 'password');
mysql_select_db('database');
$query = "SELECT * FROM simpleauth_players";
$result = mysql_query($query);
$registered = "SELECT COUNT(*) FROM dalton.tables WHERE simpleauth_players = 'name' and TABLE_TYPE='BASE TABLE';
echo "$registered";
mysql_close();
This is the code I used to display the amount of registered players (AKA rows) in the simpleauth_players table.
<?php
$link = mysql_connect("localhost", "username", "password");
mysql_select_db("dalton", $link);
if ($_GET['task'] == 'total') {
$get_db = 'simpleauth_players';
$result = mysql_query("SELECT * FROM $get_db", $link);
echo '{"task":"total","amount":"';
echo mysql_num_rows($result);
echo '"}';
}
?>
select count(*) as total_player from simpleauth_players
OR
$sql = "select * from simpleauth_players";
$result = mysqli_query($con,$sql);
$count = mysqli_num_rows();
echo "Total ".$count." Players";
Try this one assumed that your column name is language
SELECT COUNT(*) FROM simpleauth_players WHERE language = "PHP"
or if you want to get count by each language type you can use this
SELECT COUNT(DISTINCT user_id) AS Count,language FROM simpleauth_players GROUP BY language
As per your original post/question Since you have not provided us with the MySQL API you're using to connect with, here's an mysqli_ version, using MySQL's aggregate COUNT() function, which will count the number of given rows in a table:
$connection = mysqli_connect('host', 'username', 'password', 'database');
$result = mysqli_query($connection, "SELECT COUNT(*) as count
FROM simpleauth_players"
);
while ($row = mysqli_fetch_array($result)) {
$var = $row['count'];
echo "There are currently " .$var. " users.";
}
Edit: if using mysql_
$connection = mysql_connect('host', 'username', 'password');
if (!$connection) {
die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('database', $connection);
if (!$db_selected) {
die ('Can\'t use database : ' . mysql_error());
}
$result = mysql_query("SELECT COUNT(*) as count
FROM simpleauth_players", $connection);
while ($row = mysql_fetch_array($result)) {
$var = $row['count'];
echo "There are currently " .$var. " users.";
}

Display Multiple IDs columns from table

At the moment I am displaying only the first ID sorted ascending.
I have to display multiple ID results, from the same table. How would I accomplish this task?
<?php
$dbhost ='localhost';
$dbuser =‘user’; $dbpass =‘pass’;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$conn){
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM table_name ORDER BY id ASC LIMIT 0, 1';
mysql_select_db('database_name’);
$retval = mysql_query($sql, $conn);
if(!$retval){
die('Could not get data: ' . mysql_error());
}
?>
<?php while($row = mysql_fetch_assoc($retval))
{echo "{$row[‘full_name’]}”.”
{$row[‘telephone']}"."
{$row[‘email’]}”;}
mysql_close($conn);
?>
All you have to do is change the LIMIT 1 to whatever number you want, or just remove it. Removing it would look like:
$sql = 'SELECT * FROM table_name ORDER BY id ASC'
why do you have a limit if you want all the ids to be displayed?
you can use a while loop in your script
for eg:
while($row = mysql_fetch_assoc($test)){
<div id="'.$row['id'].'">'.$row['id']'.</div>;
}
which will output:
<div id="my id 1">my data 1</div>
<div id="my id 2">my data 2</div>
<div id="my id 3">my data 3</div>
.
.
.
and so on
your modified code that outputs the data in different divs
<?php
$db = mysqli_connect("localhost", "user", "pass" ,"database name") or die("Could not connect database");//keep it in one line and use (mysqli) instead of (mysql) because newer mysql versions donot support (mysql)
$retreive=mysqli_query($db,'SELECT * FROM table_name ORDER BY id');//if youre connection is in another file you need to include the connection variable before the selection line.In this case $db is the connection variable
while($row=mysqli_fetch_array($retrieve))
{ the following code creates a div for each result
echo '<div class="'.$row['fullname'].'">'.$row['fullname'].'</div>';.
echo '<div class="'.$row['telephone'].'">'.$row['telephone'].'</div>';
echo '<div class="'.$row['email'].'">'.$row['email'].'</div>';
}
?>
tried the above code on my test server and it works

How to select database and use JOIN with MySQL PHP database

I'm trying to run this function but can't get it to work, what am I doing wrong (output says my fetch array arg is invalid)?
$link = mysql_connect('localhost', 'over_app', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT Pic.PicID FROM Pics.Pic Pic LEFT JOIN SeenPics.Seen Seen ON Pic.PicID = Seen.PicID");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
while($row = mysql_fetch_array($result)) { echo "Your Pic ID: $row['PicID']"; }
Note The Pic table is in the Pics database and the Seen table is in the SeenPics database
mysql_query returns a boolean false is the query fails .. do this
$result = mysql_query("SELECT Pic.PicID FROM Pic LEFT JOIN Seen ON Pic.PicID = Seen.PicID");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
while($row = mysql_fetch_array($result)) {
echo "Your Pic ID: $row['PicID']";
}
Don't use mysql_* See this comment
Update
Now that we have the full facts - ie your attempting a join across multiple databases ... the problem is that your query isnt complete ... try this :
SELECT Pic.PicID FROM Pics.Pic Pic LEFT JOIN SeenPics.Seen Seen ON Pic.PicID = Seen.PicID

Categories