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
Related
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.
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.";
}
I am new to php. I want to fetch a particular data from mysql and display it in the label.I have tried a simple php coding.But it displays the fetched data two times(actually I have created 2 columns such as name and age in a table called test).Please help me.Here is the coding:
displayform.php
<body>
<form method="post" name="display" action="display.php" >
Enter the name you like to display the data from MySQL:<br>
<input type="text" name="name" />
<input type="submit" name="Submit" value="display" /> </form>
</body>
</html>
display.php
<?php
mysql_connect("localhost", "root", "") or die("Connection Failed");
mysql_select_db("acp")or die("Connection Failed");
$name = $_POST['name'];
$query = "select age from test where name = '$name'";
$result = mysql_query($query);
while ($line = mysql_fetch_array($result))
{
echo $line['age'];
echo "<br>\n";
}
?>
The datas in table is
name=janani
age=25
The output is displayed as
25
25
I am certain that you have two rows bearing the same name and/or age.
In order to show only one result, what you need to do is:
Use either DISTINCT with GROUP BY, or LIMIT 1.
I.e.:
$query = "select DISTINCT age from test where name = '$name' GROUP BY name";
or
$query = "select age from test where name = '$name' LIMIT 1";
Sidenote: I suggest you use mysqli with prepared statements though, since your code is open to SQL injection.
<?php
$DB_HOST = "xxx"; // Replace
$DB_NAME = "xxx"; // with
$DB_USER = "xxx"; // your
$DB_PASS = "xxx"; // credentials
$conn = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($conn->connect_errno > 0) {
die('Connection failed [' . $conn->connect_error . ']');
}
if($statement=$conn->prepare("select age from test where name = ? LIMIT 1")){
// or this one
// if($statement=$conn->prepare("select distinct age from test where name = ? group by name")){
$name = "janani";
$statement-> bind_param("s", $name);
// Execute
$statement-> execute();
// Bind results
$statement-> bind_result($age);
// Fetch value
while ( $statement-> fetch() ) {
echo $age . "<br>";
}
// Close statement
$statement-> close();
}
// Close entire connection
$conn-> close();
$line = mysql_fetch_array($result) ; //remove while loop
echo $line['age'][0];
try this ans this is work for one data fetch from table .. And may possible your result show two time because $name match two times in table so it fetch two record
You are hard coding 'age' in the array reference so it will echo that element only. Loop over array by index and you will get the name also.
I have successfully got my php code to connect to my database and retreieve data and display it. I am now trying to take this data and display it in a dropdown menu, however when I do this the dropdown menu displays the correct number of options that correlates with the data in the database(ie. it gives three options if there is 3 values in the db). But it doesn't display the text all the options are blank. Any ideas as to why it is not displaying the text?
<?php
$dbhost= 'Host IP';
$dbuser ='My Username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$sql='SELECT event_id FROM events';
mysql_select_db('db_name');
$retval= mysql_query($sql, $conn);
echo'<select name=dropdown value=', '>Dropdown</option>';
while($r= mysql_fetch_array($retval))
{
echo "<option value={$r["event_id"]}>{$r["events"]}</option>";
}
echo "</select>";
?>
http://i.imgur.com/30Uq0Ok.png
The link is what the menu currently returns
Thanks
You did not select events from database. You need to select all(*) or required columns by its name in the query .
Change your query:
$sql='SELECT event_id FROM events';
to :
$sql='SELECT * FROM events';
You also need to take option value inside quotes.
Change this line inside loop:
echo "<option value={$r["event_id"]}>{$r["events"]}</option>";
to:
echo "<option value=\"{$r['event_id']}\">{$r['events']}</option>";
try it like this (change your query to select *)
echo '<option value='.$r["event_id"].'>'.$r["events"].'</option>';
You are just selecting event_id from the table. So, $r["events"] is always empty.
Select the title field, too.
<?php
$dbhost= 'Host IP';
$dbuser ='My Username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$sql='SELECT event_id,event_title FROM events';
mysql_select_db('db_name');
$retval= mysql_query($sql, $conn);
echo'<select name=dropdown value=', '>Dropdown</option>';
while($r= mysql_fetch_array($retval))
{
echo "<option value={$r["event_id"]}>{$r["event_title"]}</option>";
}
echo "</select>";
?>
I assumed, that the field name is "event_title".
Ok so I don't know what the hell is going on I'm just trying to list all the data in a table and it's just not working I have tried a few different ways and I'm not getting any error messages and I have been over the syntax many times.
Here is the code:
// Connect to database
$dbc = mysql_connect("localhost", "root");
if (!$dbc)
die("Could not connect" . mysql_error());
// Select database
$dbc_dbselect = mysql_select_db( "contactmanager", $dbc );
if (!$dbc_contactmanager)
die("Could not connect: " . mysql_error());
// Query database
$query = "SELECT * FROM contacts ORDER by name";
$result = mysql_query($query);
// start a table tag in the HTML
echo '<table>';
// Create a loop to loop through results
while($row = mysql_fetch_array($result)){
// Print the results
echo '<tr>'.'<td>'.$row['Name'].'</td>'.'<td>'.$row['Address'].'</td>'.'<td>'.$row['Phone'].'</td>'.'<td>'.$row['Mobile'].'</td>'.'<td>'.$row['Mobile'].'</td>'.'</tr>';
}
echo "</table>"; //Close the table in HTML
mysql_close($dbc);
And this is the output result in firefox:
'; //Create a loop to loop through results while($row = mysql_fetch_array($result)){ echo ''.''.$row['Name'].''.''.$row['Address'].''.''.$row['Phone'].''.''.$row['Mobile'].''.''.$row['Mobile'].''.''; //$row['index'] the index here is a field name } echo ""; //Close the table in HTML mysql_close($dbc); ?>
Database and table
I think you are missing the third parameter i.e: password
$dbc = mysql_connect("localhost", "root", "");
^----Here
it should be:
mysql_select_db( "contactmanager", $dbc );
Provide password as a third parameter whatever it is set on your system in double quotes
$dbc = mysql_connect("localhost", "root", "<DBMS_PASSWORD>");
if not set , leave double quotes empty ("").