I'm trying to display on the browser 3 images stored in mysql database , but i figured out that just the first image of the first iteration on loop WHILE is displayed. how to display the 3 images in the browser ?
<?php
$con = mysqli_connect("localhost","root","","othmane") or die(mysqli_error($con));
if($_SERVER['REQUEST_METHOD']=='GET'){
//$id = $_GET['id'];
$sql = "SELECT image,image_type FROM images where id between 2 and 6";
$r = mysqli_query($con,$sql) or die(mysqli_error($con));;
$result=mysqli_fetch_array($r);
header('Content-Type:image/jpeg');
$ss=$result['image_type'];
while($row = $r->fetch_assoc()){
if ($ss == 'php') {
echo ( $result['image']);
} else if ($ss == 'android') {
echo base64_decode( $result['image'] );
}
}
}else{
mysqli_close($con);
}
?>
this code :
<?php
$con = mysqli_connect("localhost","root","","othmane") or die(mysqli_error($con));
if($_SERVER['REQUEST_METHOD']=='GET'){
//$id = $_GET['id'];
$sql = "SELECT image,image_type FROM images where id between 1 and 3";
$r = mysqli_query($con,$sql) or die(mysqli_error($con));;
//$result=mysqli_fetch_array($r);
$result=var_dump(mysqli_fetch_all($r));
//header('Content-Type:image/jpeg');
while($row = mysqli_fetch_assoc($r))
{
var_dump($row) ;
}
/*while($row = $r->fetch_assoc()){
$ss=$row['image_type'];
if ($ss == 'php') {
echo ( $row['image']);
} else if ($ss == 'android') {
echo base64_decode( $row['image'] );
}
}*/
}
else{
mysqli_close($con);
}
?>
and this is the result :
edit. as i've looked throught the problem, you should be able to do:
<img src="data:image/jpeg;base64,
<?php echo base64_encode( $image_data ); ?>
" />
to display all of your image, so below should work:
while($row = mysqli_fetch_assoc($r)){
$ss = $row['image_type'];
if ($ss == 'php') {
<img src="data:image/jpeg;base64, <?php echo ( $row['image']); ?>" />
} else if ($ss == 'android') {
<img src="data:image/jpeg;base64, <?php echo base64_decode( $row['image'] ); ?> " />
}
}
When you are doing $r->fetch_assoc() ( so as long as your query is getting you result, your While loop will go on), you are fetching one result row of your data into associative array, so you have your data in $row variable.
You have to use that variable for checking your ['image_type'] and getting your ['image'].
In your code your $result['image_type'] and $result['image'] was bound to the first record that you have fetched with $result=mysqli_fetch_array($r) ( as documentation states: mysqli_result::fetch_array -- mysqli_fetch_array — Fetch a result row as an associative, a numeric array, or both ).
if($_SERVER['REQUEST_METHOD']=='GET'){
$sql = "SELECT image,image_type FROM images where id BETWEEN 2 and 6";
$r = mysqli_query($con,$sql) or die(mysqli_error($con));
header("Content-type: image/jpeg");
while($row = mysqli_fetch_assoc($r)){
$ss = $row['image_type'];
if ($ss == 'php') {
echo ( $row['image']);
} else if ($ss == 'android') {
echo base64_decode( $row['image'] );
}
}
}else{
mysqli_close($con);
}
also when you are using procedular style, use it all the way, as (examples from php.net):
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_assoc($result)) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
or just use object oriented style:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
$result = $mysqli->query($query);
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
More info, examples here: mysqli_result::fetch_assoc
Related
so basically I am trying to create a little thing where it outputs stars, based on the database saved rating integer. The problem is it does not seem to put the number I from the database, in the variable. Here is the code I used:
<?php
$productID = 100;
$con = mysqli_connect("localhost", "root", "", "example");
function connect()
{
$con = mysqli_connect("localhost", "root", "", "example");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
return $con;
}
}
function getStars($con)
{
$productID = 100;
$sql = "SELECT rating
FROM reviews
-- JOIN stockitemstockgroups USING (StockItemID)
-- JOIN stockgroups USING (StockGroupID)
WHERE reviewID = '5'
";
$result = $con->query($sql);
if ($con && ($result->num_rows > 0)) {
// output data of each row
while ($row = $result->fetch_assoc()) {
echo $row["rating"];
}
} else {
echo "error";
}
}
$value = getStars($con);
echo $value;
for ($x = 1; $x <= $value; $x++) {
echo '<div class="rating"><span>★</span></div>';
}
?>
I'm having trouble finding a duplicate, though I'm sure this is one. You aren't returning anything from your function, so $value doesn't have a value.
function getStars($con)
{
$productID = 100;
$sql = "SELECT rating FROM reviews WHERE reviewID = 5";
$result = $con->query($sql);
if ($result && ($result->num_rows > 0)) {
// output data of first row
$row = $result->fetch_assoc();
return $row["rating"];
} else {
return false;
}
}
As a general rule, never echo from a function. Also, no need for a loop over what will presumably be a single result.
i have two button on my homepage one is time-in and the other is time-out,
i want to prevent the user/student to time-in using same id if he did not put time-out on his last time-in to create valid entry. Hope you can help me.
here is my php code:
<?php
include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$time=date("H:i:s");
$sql = mysqli_query($conn, "SELECT * FROM stud WHERE rfid_num = '$rfid'");
$count = mysqli_num_rows($sql);
if ($count == 0 ) {
header("location:notexist.php");
} elseif (empty($row['timeout'])) {
header("location:page say the user/student need to put timeout first before time-in again");
} else {
while( $row = mysqli_fetch_array($sql)) {
$rfid=$row['rfid_num'];
$id=$row['id'];
$name0 = $row['name'];
$course0 = $row['course'];
$image = $row['image'];
$InsertSql = "INSERT INTO student_att(rfid_num,id,name,course,image,timein) VALUES ('$rfid','$id','$name0','$course0','$image','$time')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
?>
this is my answer just wanna share it, i just add select student_att table
to fetch the data and check if timeout column is empty.
<?php
include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$time=date("H:i:s");
$sql = mysqli_query($conn,"select * from stud where rfid_num ='$rfid' ");
$count = mysqli_num_rows($sql);
if ($count == 0) {
header("location:notexist.php");
}else{
while( $row = mysqli_fetch_array($sql)) {
$rfid=$row['rfid_num'];
$id=$row['id'];
$name0 = $row['name'];
$course0 = $row['course'];
$image = $row['image'];
$sql1 = mysqli_query($conn,"select * from student_att where rfid_num ='$rfid' order by number DESC limit 1 ");
while( $row = mysqli_fetch_array($sql1)) {
if(empty($row['timeout'])){
header("location:logout.php");
}else{
$InsertSql = "INSERT INTO student_att(rfid_num,id,name,course,image,timein) VALUES ('$rfid','$id','$name0','$course0','$image','$time')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
}
}
?>
So I have a database table, named todolist, and I want to display the whole to do list in a box. What I have right now is this:
$db = (INFO HERE :));
$sql = "SELECT * FROM todolist";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) == 0) {
echo "There is nothing else to do! :)";
} else {
}
What do I put in the else, for it to display everything on a list? Thanks
try something like below:
<?php
$db = (INFO HERE :));
$sql = "SELECT * FROM todolist";
$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) == 0) {
echo "There is nothing else to do! :)";
}
else{
?>
<ul>
<?php
while($row = mysqli_fetch_assoc($result)){
?>
<li><?php echo $row["yourColumnName"] ?></li><?php
}?>
</ul><?php
}
?>
$sql = "SELECT * FROM todolist";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) == 0) {
echo "There is nothing else to do! :)";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
// To see all data
// print_r($row);
// to print single column value
//echo $row['id];
}
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
//Do something here with $row array , for example print_r
print_r($row);
}
I am trying to make an If statement in PHP loop for each row in an Sql table. I think I'm almost there but there is a syntax error that I cant quite figure out. Heres what i've got so far:
$mysqli = new mysqli("localhost", "root", "usbw", "favourites");
// check connection
if ($mysqli->connect_errno) {
die("Connect failed: ".$mysqli->connect_error);
}
$query = "SELECT * FROM defaultTiles";
$result = $mysqli->query($query);
while($row = $result->fetch_array()){
echo "<?php if ($tile_name === '$row[name]'){
$tile_colour = '$row[colour]';
$img_url = '$row[img_url]';
$link_url = '$row[link_url]';
} ?>";
}
Replace
while($row = $result->fetch_array()){
echo "<?php if ($tile_name === '$row[name]'){
$tile_colour = '$row[colour]';
$img_url = '$row[img_url]';
$link_url = '$row[link_url]';
} ?>";
}
with
while($row = $result->fetch_array()){
if ($tile_name === $row['name']){
$tile_colour = $row['colour'];
$img_url = $row['img_url'];
$link_url = $row['link_url'];
}
}
as, when you put an echo in front of the if statement, it just prints the statement inside the echo.
Here I am trying to get some data from database and want to display it as a json response so that user can fetch each field.
Here is how user can perform query
http://localhost/safari/index.php?getbranch=true
this should give branch details from tables.
Here is PHP code to do it
<?php
if(isset($_GET['getbranch']))
{
$getbranch = $_GET['getbranch'];
if($getbranch == 'true')
{
getbranch($getbranch);
}
}
function getbranch($getbranch)
{
$con = mysqli_connect('127.0.0.1', 'root', '', 'safari');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return;
}
$today = date("Ymd");
$result = mysqli_query($con,"SELECT division, branch,branchofficename,branchofficecode,status from tbl_branchoffice");
while ($row = #mysqli_fetch_array($result))
{
$result1 = json_encode($row);
}
echo $result1;
}
What's wrong wit this?
JSON response:
[{"0":"3","sno":"3","1":"2","division":"2","2":"2","branch":"2","3":"SAFFARI TRAVELS","branchofficename":"SAFFARI TRAVELS","4":"gfgbhghfhf","branchofficecode":"gfgbhghfhf","5":"active","status":"active"},
{"0":"4","sno":"4","1":"2","division":"2","2":"chennai","branch":"chennai","3":"chennai","branchofficename":"chennai","4":"br01","branchofficecode":"br01","5":"active","status":"active"},{"0":"5","sno":"5","1":"3","division":"3","2":"delhi","branch":"delhi","3":"delhi","branchofficename":"delhi","4":"br02","branchofficecode":"br02","5":"notactive","status":"notactive"},{"0":"6","sno":"6","1":"2","division":"2","2":"bangalore","branch":"bangalore","3":"bangalore","branchofficename":"bangalore","4":"br03","branchofficecode":"br03","5":"active","status":"active"},{"0":"7","sno":"7","1":"3","division":"3","2":"pune","branch":"pune","3":"pune","branchofficename":"pune","4":"br04","branchofficecode":"br04","5":"notactive","status":"notactive"}]
Change your while loop
$result1 = array();
while ($row = #mysqli_fetch_array($result))
{
array_push($result1 , $row);
}
by doing so, you have collected all result in $result1
now you can encode it
echo $result1 = json_encode( $result1);
I will prefer to use array, ignor json_encode line code,
foreach($result1 as $resultset){
//resultset contains one single row of table
foreach($resultset as $column => $columnValue){
//assuming your table column name as 'city'
if($column == 'city' && $columnValue == 'pune' ){
//displaying array of table which satisfies the condition
var_dump($resultset );
}
}
}
$result1 = array();
if(isset($_GET['getbranch']))
{
$getbranch = $_GET['getbranch'];
if($getbranch == 'true')
{
getbranch($getbranch);
}
}
function getbranch($getbranch)
{
$con = mysqli_connect('127.0.0.1', 'root', '', 'safari');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return;
}
$today = date("Ymd");
$result = mysqli_query($con,"SELECT division,
branch,branchofficename,branchofficecode,status from tbl_branchoffice");
while ($row = #mysqli_fetch_array($result))
{
$result1[] = $row;
}
echo json_encode($result1);
}
First collect each row from the result into the array result1 and then finally output the json_encode of $result1 array