How can I set variable php and mysql code - php

I want to set a variable "name",
I tried: if(isset($_POST['name'])), But not result.
I was also looking for other options but could not get results.
I have a PHP code:
<?php
$servername = "localhost";
$username = "..";
$password = "..";
$dbname = "..";
$conn = new mysqli($servername, $username, $password, $dbname);
$response = array();
$posts = array();
$sql = "SELECT name, addres, status, date FROM silknet WHERE name='test'";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result)) {
$name=$row['name'];
$addres=$row['addres'];
$status=$row['status'];
$date=$row['date'];
$response[] = array('name'=> $name, 'addres'=> $addres, 'status'=>$status,
'date'=>$date);
}
echo json_encode($response);
?>

Add to the top:
$name = $_POST['name'];
And edit this line as:
$sql = "SELECT name, addres, status, date FROM silknet WHERE name='$name'";

Related

How to change the row to cloumn in php with mysql

Good Morning ,I find many example but still cannot display i want output,PLS Help Me and Thank
.Now output like this:output
.I want output like this :output
<?php
$servername = "localhost";$username = 'root';$password = "";$database = "ocall";
$mysqli = new mysqli("localhost", $username, $password, $database);$query = "SELECT * FROM timetable";
echo '<table border="1" ><tr><td><b> <font face="Arial">Day</font></td><td><b><font face="Arial">Date</font></td></tr>';
if ($result = $mysqli->query($query)) {while ($row = $result->fetch_assoc()) {$field1name = $row["Day"];$field2name = $row["Date"];
echo '
<tr><td>'.$field1name.'</td><td>'.$field2name.'</td></tr>';}$result->free();}
?>
One way can be by storing values in separate arrays. Please try the following code.
<?php
$servername = "localhost";
$username = 'root';
$password = "";
$database = "ocall";
$conn = new mysqli("localhost", $username, $password, $database);
if(!$conn){echo "Error";}
$query = "SELECT * FROM `timetable`";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($result)){
$daytime[] = $row['Day'] ;
$dates[] = $row['Date'];
}
echo '<table border=1><tr><td>Day</td>';
foreach($daytime as $d)
{echo '<td>'.$d.'</td>';
}
echo '</tr><tr><td>Dates</td>';
foreach($dates as $d)
{echo '<td>'.$d.'</td>';
}
echo '</tr></table>'
?>
Hope this works for you.

Return list of strings with mysql query in PHP

I'm trying to make a simple query with PHP that returns a list of Strings with all the names of the users, but I can figure out how to do do it. The only thing i thought wa this:
<?php
$host_name = "hostname";
$database = "database";
$user_name = "username";
$password = "pass";
$connect = mysqli_connect($host_name, $user_name, $password, $database);
$select = "SELECT username from userstasker";
$result = $connect->query($select);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
echo json_encode($rows);
$connect->close();
?>
But this returns a JSon, ant idea how to do it?
<?php
$host_name = "hostname";
$database = "database";
$user_name = "username";
$password = "pass";
$connect = mysqli_connect($host_name, $user_name, $password, $database);
$select = "SELECT username from userstasker";
$result = $connect->query($select);
$usernames = array();
while($r = mysqli_fetch_assoc($result)) {
$usernames[] = $r["username"];
}
$connect->close();
// Do something with $usernames
?>

how to paginate data fetched from databse

lets say if there are 13 records the latest 5 or from 9-13 in the first page,
from 4-8 in second page and 1-3 in the third page
i've tried this but its for the first page only
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mysqli_login";
// Create connection
$connection= new mysqli($servername, $username, $password, $dbname);
$query = mysqli_query($connection, "SELECT name,submittedby,trn_date FROM new_record ORDER BY id DESC LIMIT 5")or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($query)) {
$fileName = $row['name'];
$fileContents = file_get_contents("txt/$fileName");
$poster = $row['submittedby'];
$date = $row['trn_date'];
echo ("posted by :$poster | posted date : $date");
echo ("$fileContents");
}
?>
Your code should be like this
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create connection
$connection= new mysqli($servername, $username, $password, $dbname);
$current_page = 1; // 1=>refer to the first page, 2=> second page and so on..
if(!empty($_GET['page_no']){
$current_page = $_GET['page_no'];
}
$to = "5"; // it is no of record which you wants to show on each page, you can change it's value as per your need.
$from = ($current_page - 1) * $to;
$query = mysqli_query($connection, "SELECT name,submittedby,trn_date FROM new_record ORDER BY id DESC LIMIT $from , $to")or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($query)) {
$fileName = $row['name'];
$fileContents = file_get_contents("txt/$fileName");
$poster = $row['submittedby'];
$date = $row['trn_date'];
echo ("posted by :$poster | posted date : $date");
echo ("$fileContents");
}
?>
your url should be something like http://localhost/projects/?page_no=1
replace "http://localhost/projects/" with your actual url
Hope this will help!!
Here's an article that discusses this topic. You need to offset your query based on the current page. So it will be LIMIT 5 OFFSET <amount based on the page>.

How to retrieve images from database in php

I have a database which has a table called 'propImages' and there are two columns.- 'pid' and 'location'.
And i have data in the database where multiple images can contained by single pid.
image contains database data
now i want to retrieve images from database according to given pid. there can be more than one image.
All i know it there should be an iteration to retrieve images.
I want to display images in HTML .
can you please show me the way to do it in php?
Thanks in advance guys
This may help you
<?php
include 'inc/database.php';
$conn = new mysqli($servername, $username, $password, $database);
$propid = $_GET['propid'];
$sql = "SELECT * FROM propImages WHERE propid='" . $propid . "';";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<img src=" . $row['image'] . ">";
}
}
else {
echo "No results";
}
?>
in the inc/database.php :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "database";
?>
To see how it works try visiting : file.php?propid=22
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "databasename";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
//create sql
$sql = "SELECT * FROM `propImages` where pid='$YOUR_PID'";
$result = mysqli_query($con, $sql);
$row = mysqli_num_rows($result);
//retrive data print here
if($row > 0){
while($col = mysqli_fetch_assoc($result))
{
echo $col['location'];
}
} else {
echo 'no result found.';
}
?>
wish it helps

how to replace current_date() to any date type by user

how to replace current_date() to any date type by user
example:
www.example.com/2025-01-04
or any date
<?php
$servername = "localhost";
$username = "11_11";
$password = "1Eh]V";
$dbname = "1_1";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT domain FROM insights_base WHERE domain_1 = current_date() ";
$result = $conn->query($sql);
$data = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
}
$conn->close();
$smarty = new Smarty;
$smarty->assign('data', $data);
$smarty->display(APP_THEME . '/dom.tpl');
Try this:
<?php
$servername = "localhost";
$username = "11_11";
$password = "1Eh]V";
$dbname = "1_1";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_GET['date'])
and preg_match('/^\d{4}(-\d{2}){2}$/', $_GET['date']) // primitive validation
) {
$date = $_GET['date'];
} else {
$date = date('Y-m-d');
}
$sql = "SELECT domain FROM insights_base WHERE domain_1 = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $date);
$stmt->execute();
$stmt->bind_result($domain);
$data = array();
while ($stmt->fetch()) {
$data[] = array('domain' => $domain);
}
$conn->close();
$smarty = new Smarty;
$smarty->assign('data', $data);
$smarty->display(APP_THEME . '/dom.tpl');
I would recommend using a prepared statement here (assuming you are using mysqli).
A) Because you can dynamically assign either current_date() or variable using user input value to a placeholder.
B) The prepared statement would make this operation more secure since you are escaping all user input.
if (!empty($_POST['date'])) {
$user_date = $_POST['date'];
} else {
$user_date = "current_date()";
}
$sql = "SELECT domain FROM insights_base WHERE domain_1 = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $user_date);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$data = [];
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
}
Depending on how you are getting the date from user, you may need to validate it to make sure that it is in the proper date/time format for mysql.

Categories