Php mysql fetch result - mysql

I've made a script for movies but i have an issue.
The home of script is contain code like this :
<div class="box">
<div id="movie"></div><div id="movie"></div><div id="movie"></div>
</div>
I've do a mysql query but i don't know how to echo just 3 record per div .
Im using this code for mysql query :
$query=mysql_query("select * from movies where id=$id");
while($row=mysql_fetch_assoc($query))
{
echo $row['name'];
}

You will need something like this I think
$query = mysql_query("select * from movies");
$result = array();
while($r = mysql_fetch_assoc($query)) {
$result[$r['id']] = array($r['name'],$r['thumb']);
}
$i = 0;
foreach($result as $id => $data){
if($i == 0)
{
echo "<div class=\"box\">";
echo "<div id=\"movie\">";
echo "ID: $id";
echo "Name: $data[0]";
echo "Thumb: $data[1]";
echo "</div>";
$i = $i + 1;
}
elseif($i == 1)
{
echo "<div id=\"movie\">";
echo "ID: $id";
echo "Name: $data[0]";
echo "Thumb: $data[1]";
echo "</div>";
$i = $i + 1;
}
elseif($i == 2)
{
echo "<div id=\"movie\">";
echo "ID: $id";
echo "Name: $data[0]";
echo "Thumb: $data[1]";
echo "</div>";
echo "</div>";
$i = 0;
}
}

Multiple elements with the same id? Not a good idea.
You didn't say what you want to happen if you're query returns less then 3 rows.
You might try:
...
$x=0;
while($row=mysql_fetch_assoc($query) && ++$x<=3) {
echo $row['name'];
}

Related

Trying to echo a table but it's not working

The table is called bookings. I'm trying to echo rows from the table, but I'm getting errors in some places according to user_id. Dreamweaver tells me the last few lines have errors, but I'm not sure what's the problem.
<?php
require 'connect.php';
include("auth.php");
$user = $_SESSION['username'];
$uid = $_SESSION['user_id'];
$sql = "SELECT * FROM bookings WHERE user_id =". $uid;
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) { // if the mysqli_query performed above works
echo "<br>";
echo '<table border="1">';
echo '<tr>
<th>booking_id</th>
<th>"Room_name"</th>
<th>"booking_date"</th>
<th>"period_name"</th>
<th>"booking_id"</th>
<th>"Cancel Booking"</th>
</tr>';
while ($row = mysqli_fetch_assoc($result)) { // important line
echo '<tr>';
echo '<td>'.$row["booking_id"].'</td>';
echo '<td>'.$row["Room_name"].'</td>';
echo '<td>'.$row["booking_date"].'</td>';
echo '<td>'.$row["date_booked"].'</td>';
echo '<td>'.$row["period_name"].'</td>';
echo '<td>'.$row["booking_id"].'</td>';
echo "</tr>";
echo "</table>";
}
else echo '<p>You have no bookings.</p>';
I have made some change
And here is the code-
require 'connect.php';
include 'auth.php';
$user = $_SESSION['username'];
$uid = $_SESSION['user_id'];
$sql = "SELECT * FROM bookings WHERE user_id ='". $uid."'";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) { // if the mysqli_query performed above works
echo "<br>";
echo '<table border="1">';
echo '<tr>
<th>booking_id</th>
<th>Room_name</th>
<th>booking_date</th>
<th>period_name</th>
<th>booking_id</th>
<th>Cancel Booking</th>
</tr>';
while ($row = mysqli_fetch_assoc($result)) { // important line
echo '<tr>';
echo '<td>'.$row["booking_id"].'</td>';
echo '<td>'.$row["Room_name"].'</td>';
echo '<td>'.$row["booking_date"].'</td>';
echo '<td>'.$row["date_booked"].'</td>';
echo '<td>'.$row["period_name"].'</td>';
echo '<td>'.$row["booking_id"].'</td>';
echo '</tr>';
}
echo '</table>';
} else echo '<p>You have no bookings</p>';

PHP Mysql - adding an empty row can't figure it out

Here is the table code - when this is printed out the has one empty row at the bottom. I am not sure why the last row is added? Is it perhaps the if else statement?
<?php
$row = mysql_fetch_array($result);
if ($row['Price'] == '0.00') {
echo "<TABLE width='100%'>\n";
echo "<TR><TD id=tblrowsHead2>Item</TD><TD id=tblrowsHead2>Size</TD><TD
id=tblrowsHead2>Contact</TD></TR>\n";
for ($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo "<TD id=Size>" . $row['Item'] . "</TD><TD id=Size>" . $row['Size']
. "</TD>
<TD id=Price>Contact Us</TD>\n";
}
} else {
echo "<TABLE width='100%'>\n";
echo "<TR><TD id=tblrowsHead2>Item</TD><TD id=tblrowsHead2>Size</TD><TD
id=tblrowsHead2>Price</TD></TR>\n";
for ($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo "<TD id=Size>" . $row['Item'] . "</TD><TD id=Size>" . $row['Size']
. "</TD>
<TD id=Price>$" . $row['Price'] . "</TD>\n";
}
}
//now let's close the table and be done with it
echo "</TABLE>\n";
?>
Try this code instead:
<?php
$row = mysql_fetch_array($result);
$i = 0;
if ($row['Price'] == '0.00') {
echo "<TABLE width='100%'>\n";
echo "<TR><TD id=tblrowsHead2>Item</TD><TD id=tblrowsHead2>Size</TD><TD id=tblrowsHead2>Contact</TD></TR>\n";
do {
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo '<TD id=Size>'.$row['Item'].'</TD><TD id=Size>'.$row['Size'].'</TD>';
echo "<TD id=Price>Contact Us</TD></TR>\n";
$i++;
} while ($row = mysql_fetch_array($result)); //get a row from our result set
} else {
do {
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo '<TD id=Size>'.$row['Item'].'</TD><TD id=Size>'.$row['Size'].'</TD>';
echo "<TD id=Price>$" . $row['Price'] . "</TD></TR>\n";
$i++;
} while ($row = mysql_fetch_array($result)); //get a row from our result set
}
//now let's close the table and be done with it
echo "</TABLE>\n";
When you using the mysql_fetch_array at the first line of for-loop, you will lost the first record of your data-table, so you will have an empty line at the end

i created a table in database to display photos and i made it to display ,but i want them to display from the last to the first

<?php
$conn = mysql_connect("localhost","root","");
if(!$conn){
echo mysql_error();
}
$db = mysql_select_db("imagestore",$conn);
if(!$db ){
echo mysql_error();
}
$q = "SELECT * FROM imagetable";
$r = mysql_query("$q",$conn);
if($r)
{
while($row=mysql_fetch_array($r) )
{
header("Content-type: text/html");
echo "</br>";
echo $row['photoname'];
echo "</br>";
$type = "Content-type: ".$row['phototype'];
header($type);
echo "<img src=image.php?fotoid=". $row['fotoid']."width =300 height = 35. 300/>";
}
}
else{
echo mysql_error();
}
?>
I know ORDER by but is not working to display photos as i want in my page .
I am a beginner .
I used $q = "SELECT * FROM imagetable ORDER BY fotoid DESC";

HTML select option values from mysql db via php are not coming properly inside the tag

I am creating a html form, in which select dropdown is used.
The values in select are coming from MySql db using PHP.
The problem is, the values from DB are not coming inside the select options, but printing outside of it, while i am writing code like this:
$result = mysqli_query($link,$sql);
if ($result != 0) {
echo '<select name="name" >';
$num_results = mysqli_num_rows($result);
for ($i=0;$i<$num_results;$i++) {
$row = mysqli_fetch_array($result);
$name = $row['name'];
echo '<option value="' .$name. '">' .$name. '</option>';
}
echo '</select>';
}
But when i put one more select, the values comes inside of the select options.
$result = mysqli_query($link,$sql);
if ($result != 0) {
#Extra select#
echo '<select>';
##############
echo '<select name="name" >';
$num_results = mysqli_num_rows($result);
for ($i=0;$i<$num_results;$i++) {
$row = mysqli_fetch_array($result);
$name = $row['name'];
echo '<option value="' .$name. '">' .$name. '</option>';
}
echo '</select>';
}
But of-course, there is one select box coming for no use.
What is wrong in this code ?

adding different class/style to rows that variable fulfills the condition

The problem is that all images are displayed with opacity: 0.4. I need different style to each row that value from number column is less than 100.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$progres = $row['number'];
$num_rows++;
echo "$progres <br/>";
echo "<div class='right_achiev'>";
echo '<div id="box1" class="box">';
echo '<span class="caption simple-caption">';
echo '<p class="sq1">'.$row['name'].'</p>';
echo '<p class="unlok">UNLOCKED:<br/> 2014-01-09 16:25 </p>';
echo '</span>';
if ($progres < 100)
{
echo "<img class='achiev_icon' src='".$row['icon']."'/>";
}
else
{
echo "<img class='achiev_icon' style='opacity:0.4' src='".$row['icon']."'/>";
}
echo "</div>";
echo "</div>";
}
you can try to use this code
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$progres = intval($row['number']);
$num_rows++;
echo "$progres <br/>";
echo "<div class='right_achiev'>";
echo '<div id="box1" class="box">';
echo '<span class="caption simple-caption">';
echo '<p class="sq1">'.$row['name'].'</p>';
echo '<p class="unlok">UNLOCKED:<br/> 2014-01-09 16:25 </p>';
echo '</span>';
if ($progres < 100)
{
echo "<img class='achiev_icon' src='".$row['icon']."'/>";
}
else
{
echo "<img class='achiev_icon' style='opacity:0.4' src='".$row['icon']."'/>";
}
echo "</div>";
echo "</div>";
}