I have a drop down menu populated from a mysql database , is it possible to display two columns from the database but only when clicked ?
By this I mean, when you click you see a list of items on the left and a brief description on the right. The description column should only be visible when clicking on the dropdown.
Is this possible ?
My code is as follow, this queries the DB to check the current selection for the user.
I have tried adding a second column but it shows at all times which looks terrible.
<?php
require_once('db_connect.php');
$id = $_GET['id'];
$qry = "SELECT issue FROM issuetbl WHERE issueid =?";
$stmt = $mysqli->prepare($qry);
$stmt->bind_param('s', $id);
$stmt->execute();
$result = $stmt->get_result();
$client_result = $result->fetch_assoc();
$qry2 = "SELECT data FROM issdd";
$stmt = $mysqli->prepare($qry2);
$stmt->execute();
$resultdata = $stmt->get_result();
echo '<select name="enqiss">';
while($row = $resultdata->fetch_assoc()) {
if($client_result['issue']==$row['data']){
$selectCurrent='selected';
}else{
$selectCurrent='';
}
echo '<option value="'.$row['data'].'" '.$selectCurrent.'>'.$row['data'].'</option>';
}
echo '</select>';
?>
Many thanks
Related
I have database structure like this:
table name: tblpost and I want to show all post by user like show post by multiple user in only page name: allpost.php
And I want to get result like this:
So how to show data like that? Many thanks for all your answer.
Assuming you use PDO.
$stmt = $db->prepare("SELECT * from tblposts where post_by=:post_by");
$stmt->bindParam(':post_by',$user);
$stmt->execute();
//Fetches the posts for the certain user you want
$posts = $stmt->fetch(PDO::FETCH_ASSOC);
MySQLi:
$stmt = $mysqli->prepare("SELECT * from tblposts where post_by=?");
$stmt->bind_param('s',$user);
$stmt->execute();
$res = $stmt->get_result();
while ($row = mysqli_fetch_assoc($res)) {
echo $row['post_date'];
}
I have a Attendance table containing attendance of all the employee.
Currently i am working on report_generation page.
So, i want to pass emp_mail id through drop-down box to fetch records of that particular employee.
here is the database table containing records
Here is the code from the report generation page:
$dbh = getDBH();
$output = "";
$sql = "SELECT ATT.AttDate FROM attandance ATT";
$query = $dbh->prepare($sql);
$query->execute();
$results = $query->fetchAll(\PDO::FETCH_OBJ);
$arrDates = array();
foreach($results as $intKey=>$objAttDate)
{
$arrDates[] = $objAttDate->AttDate;
}
var_dump($arrDates);
I am trying to get country and other info from sql for specific id, only "id" displays correctly in both cases.
<?php
$sql = "SELECT * FROM `list` ORDER BY category ASC";
$result = mysql_query($sql);
$rows = mysql_fetch_assoc($result);
$id = $_GET['id'];
$country = $_GET['country'];
echo $id;
echo $country;
?>
and
<?php
$sql = "SELECT * FROM `list` ORDER BY category ASC";
$result = mysql_query($sql);
$id = $_GET['id'];
$country = $_GET['country'];
if (mysql_num_rows($result) > 0) {
while($rows = mysql_fetch_assoc($result)) {
echo $id;
echo $country;
}
}
?>
I think you have a column named id and another named country from which you want to collect the data in the country row of a certain record.
I have similar ideas to some of the comments and would like to highlight my thoughts in this answer.
$_GET superglobal is used to extract data form a URL parameter. If it is used to get the data from the record, you might want to use mysql_fetch_assoc($result)["country"]. For more information, please take a look at https://www.php.net/manual/en/function.mysql-fetch-assoc.php
and https://www.w3schools.com/php/php_superglobals_get.asp
To filter certain records according to some input id, you would want to use the WHERE clause. Link for more information:- https://www.w3schools.com/sql/sql_where.asp
The possible code might be:-
<?php
$id = $_GET["id"]
$sql = "SELECT * FROM `list` WHERE id='$id' ORDER BY category ASC";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) {
while($rows = mysql_fetch_assoc($result)) {
echo $rows['id'];
echo $rows['country'];
}
}
?>
PS: I have tried my best to answer the asked question provided the less information given. Hope it helps
I am trying to write a query within a While loop using a dynamic variable but can't seem to get it working correctly. I am new to PDO so I'm not 100% if this is even the correct way to go about doing this. The connection to the database ($db) works fine, and the query runs fine without the second $STH2 lines. Please help :)
<?php
//This will list player info so the user can get the correct player ID
$STH = $db->query('SELECT id, name, tag from wdlTeams');
//Setting the fetch mode
$STH->setFetchMode(PDO::FETCH_ASSOC);
//Create listbox object and populates options with team names
echo "<select name='teamID'>";
while($row = $STH->fetch()) {
$id = $row['id'];
$name = $row['name'];
$tag = $row['tag'];
$seasonId = $row['seasonId'];
$STH2 = $db->prepare('SELECT name from wdlSeasons where id=$seasonId');
$STH2->execute();
$seasonName = $STH2->fetchColumn();
echo "<option value='$id'>$tag - $name ($seasonName)</option>";
echo "<br />";
}
echo "</select>";
?>
I have also tried changing to
$STH2 = $db->prepare("SELECT 'name' from 'wdlSeasons' where id='$seasonId'");
but with no luck
Try this:
$STH2 = $db->prepare('SELECT name from wdlSeasons where id=:id');
$STH2->bindParam(':id', $seasonId);
$STH2->execute();
I want to search in a database table for a particular word and want to retrieve a text from other column in the same row and there may be more than one rows where that word may exist, so i want all those rows where that word is.
i am using this code
$found = 'world';
$sql = "SELECT file_id FROM hello WHERE $field = '$found'";
$result = mysql_query( $sql);
$row = mysql_fetch_array($result);
while($row)
{
echo $row['file_id'];
}
so my problem here is in database table i have only 5 rows and there are only 2 possible file_id to be printed after the code, but this while loop goes to infinite.
Thanks in advance
The quick fix is this:
$found = 'world';
$sql = "SELECT file_id FROM hello WHERE field = '$found'";
$result = mysql_query( $sql);
while($row = mysql_fetch_array($result)) {
echo $row['file_id'];
}
But there are quire a lot other things to be corrected:
use the mysqli extension, the mysql extension is deprecated
you should use prepared statements to prevent sql injections
you must add error checking and handling
...
if you are starting a project use PDO
$db new PDO('mysql:host=localhost dbname=foo', 'user', 'password');
$sql = 'SELECT file_id FROM hello WHERE field = ? ';
$stmt = $db-prepare($sql);
$stmt->bindValue(1, $word);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $item){
echo $item['key'];
}