How to change the row to cloumn in php with mysql - php

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.

Related

How can I set variable php and mysql code

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'";

PHP mysqli query doesn't return any results

I'm using this code here
<?php
error_reporting(1);
$servername = '127.0.0.1';
$username = '';
$password = '';
$dbname = 'splafpoo_users';
$conn = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno()){
printf("<b>Connection failed:</b> %s\n", mysqli_connect_error());
exit;
}
$key = '';
if(isset($_POST['key'])){
$key = $_POST['key'];
}
$query = "SELECT * FROM users WHERE serial='$key'";
echo $query;
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
echo $row;
?>
Running the query SELECT * FROM users WHERE serial='test' in phpMyAdmin returns the desired result however when trying to display the result using the code above nothing is displayed and I cannot figure out how. How do I display the result?
You're gonna need a good old fashion while loop
while($row = $result->fetch_assoc()) {
echo $row['WHATEVERCOLUMNITISYOUWANT'];
}
also this is most definitely a duplicate.
Use var_dump($row) instead of echo $row or you use echo with a key:e.g. echo $row["user"]

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

extracting an array from a database in php

im currently trying to extract a table from my database (articles) and the table article and put it in an array but im not sure weather or not it wokred because i dont know how to print an array. i was following this link.
http://phpscriptarray.com/php-arrays-tutorials-tour/how-to-extract-mysql-database-data-into-php-array-variable.php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// create connection
$conn = new mysqli($servername, $username, $password);
// check connection
if ($conn->connect_error){
die("connection failed: " . $conn->connect_error);
}
// connect to DB
$db_selected = mysqli_select_db('article', $conn);
if (!$db_selected){
die("can't use article : " .mysqli_error());
}
// extract databases table to PHP array
$query = "SELECT * FROM `articles`";
$result = mysqli_query($query);
$number = mysql_numrows($result);
$article_array = array();
$x = 0
while($x < $number)
{
$row = mysqli_fetch_array($result);
$artic = $row['name'];
$amount = $row['quantity'];
$article_array[$artic] = $amount;
$x++;
}
echo count($article_array);
//echo "hello";
<?
even the echo hello wont work and im not sure if i was supposed to put a name and quantity in:
$artic = $row['name'];
$amount = $row['quantity'];
You are mixing object oriented with procedural style. Your query and loop should look like this:
$query = "SELECT * FROM `articles`";
$result = $conn->query($query);
$article_array = array();
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$artic = $row['name'];
$amount = $row['quantity'];
$article_array[$artic] = $amount;
}
http://php.net/manual/en/mysqli.query.php
Also your PHP closing tag is faulty - should be ?> or omitted.

PHP Database output. Arrays

In first case, i will get full array:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test.com";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM articles WHERE writer='$w_name' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row[header];
}
}
mysqli_close($conn);
?>
In the second case, i will get just 1st value from array:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test.com";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM articles WHERE writer='$w_name' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$q=$row[header];
}
}
mysqli_close($conn);
?>
<div>
<? echo $q ?>
</div>
What should I do, to make 2nd case work like 1st? I mean, how to put into $q, all values of array, not just the first.
You are not defining $q as an array at all.
$q=array();
...
while ($row = mysqli_fetch_assoc($result)) {
$q[]=$row['header'];
}
...
an example of output :
foreach($q as $header) {
echo $header.'<br>';
}
You are overwriting $q . Try using $q[]=
change $q=$row[header]; line to $q[]=$row[header];
and then not echo $q; but print_r($q);
Try to change this into the while
$q = array();
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
array_push($q, $row['header']); // i guess header is a column of the table
}
}
and then you must have your array when print the $q variable, i mean do an print_r($q)
More info about array_push: http://php.net/manual/es/function.array-push.php
Hope this helps :)

Categories