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>';
Related
EDIT: Updated code, still not working
I'm trying to get tables to be displayed on html through a button click, unless it is possible to automatically load up tables on page initiation.
I keep running into an error: Fatal error: Uncaught Error: Call to undefined function mysql_fetch_array() on Line 15.
I am using a .php script
Here is my code:
<?php
include ("ConnDetails.php");
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn, "SELECT * FROM Tracks");
if ($result !== false) {
echo "<table border='1'>
<tr>
<th>Tracks<th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Tracks'] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "You have encountered an error";
}
$conn->close();
?>
Thanks in advance
I think you can use:
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['Tracks'] . "</td>";
echo "</tr>";
}
echo "<table>";
} else {
echo "You have encountered an error";
}
$conn->close();
?>
<?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";
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>";
}
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'];
}
I'm trying to display a list of titles in a modify/delete page. They load fine since I get the correct amount of rows, put for some reason I can't the text printed.
Here's the function which corresponds to the issue.
function manage_content()
{
echo '<div id="manage">';
$sql = "SELECT * FROM content";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)): ?>
<div>
<h2 class="title"><?php $row['title'] ?></h2>
<span class="actions">Edit | Delete</span>
</div> <?php
endwhile;
echo '</div>'; //End of Manage Div
}
And this is the full code:
<?php
class blog {
private $host;
private $username;
private $password;
private $db;
private $link;
public function __construct($host, $username, $password, $db){
$this->db = $db;
$this->link = mysql_connect($host, $username, $password, $db);
mysql_select_db($this->db, $this->link) or die (mysql_error());
}
function get_content($id=''){
if($id !=NULL):
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM content WHERE id = '$id'";
$return = '<p>Vover al Indice</p>';
else:
$sql = "SELECT * FROM content ORDER by id DESC";
endif;
$res = mysql_query($sql) or die (mysql_error());
if(mysql_num_rows($res) !=NULL):
while($row = mysql_fetch_assoc($res)){
echo '<h1>'.$row['title'].'</h1>';
echo '<p>'.$row['body'].'</p>';
}
else:
echo '<p>Oops, post not found!</p>';
endif;
if(isset($return)){
echo $return;
}
}
function add_content($p){
$title = mysql_real_escape_string($p['title']);
$body = mysql_real_escape_string($p['body']);
if(!$title OR !$body):
if(!$title):
echo "<p>You have to fill the title.</p>";
endif;
if(!$body):
echo "<p>You have to fill the body.</p>";
endif;
echo '<p>Try again!</p>';
else:
$sql = "INSERT INTO content VALUES (null, '$title', '$body')";
$res = mysql_query($sql) OR DIE (mysql_error());
echo "Added sucessfully!";
endif;
}
function manage_content()
{
echo '<div id="manage">';
$sql = "SELECT * FROM content";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)): ?>
<div>
<h2 class="title"><?php $row['title'] ?></h2>
<span class="actions">Edit | Delete</span>
</div> <?php
endwhile;
echo '</div>'; //End of Manage Div
}
}// End of Class
?>
You're forgetting an echo in your manage_content() function:
<?php $row['title'] ?>
Should be:
<?php echo $row['title']; ?>
That's part of the problem anyway.