mysqli_fetch_array structure error - php

can you help me figure out the correct structure for this mysqli_fetch_array? I read up on http://www.w3schools.com/php/func_mysqli_fetch_array.asp but I still don't know what am I missing.
I am still new to php!
I am getting this error.
Notice: Undefined variable: video_name in D:\xampp\htdocs\video_upload\index.php on line 68
<div id="box">
<?php
$query = mysqli_query($db, "SELECT 'id', 'name', 'url' FROM videos");
while($run = mysqli_fetch_array($query)){
$video_id = $run['id'];
$video_name = $run['name'];
$video_url = $run['url'];
}
?>
<a href='vide.php?videos=<?php echo $video_url; ?>'>
<div id='url'>
<?php echo $video_name; ?>
</div>

You are using ' instead of `. Change that first. Then put the anchor tag inside the loop.
Edit like this,
<div id="box">
<?php
$query = mysqli_query($db, "SELECT `id`, `name`, `url` FROM videos");
while($run = mysqli_fetch_array($query)){
$video_id = $run['id'];
$video_name = $run['name'];
$video_url = $run['url'];
?>
<a href='vide.php?videos=<?php echo $video_url; ?>'>
<div id='url'>
<?php echo $video_name; ?>
</div>
</a>
<?php
}
?>
</div>

Related

display images from db

i have a table
tbl_image
--------------
imgID(int)
imgName(varchar)
image(blob)
here is code to display image :
<?php
$query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)){
$imgName = $row['imgName'];
echo '<div class="col-sm-3 gallery-grids-left">
<div class="gallery-grid">
<a class="example-image-link" href="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"
data-lightbox="example-set"
data-title='.$imgName.'>
<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"/></a></div></div>';
}
?>
but I'm really not like to use echo ''; so I changed to
<?php
$query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)){
$imgName = $row['imgName'];
?>
<div class="col-sm-3 gallery-grids-left">
<div class="gallery-grid">
<a class="example-image-link" data-lightbox="example-set"
href="<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>"
data-title="<?php echo imgName;?>">
<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>
</a>
</div>
</div>
<?php }
?>
It just display only image , no title no "image block" like this image demo
Plz help me to show my mistake?and how to fix it.
Many thanks,
HTML error in your code: wrong binding of PHP scripting
<?php
$query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)){
$imgName = $row['imgName'];
?>
<div class="col-sm-3 gallery-grids-left">
<div class="gallery-grid">
<a class="example-image-link" data-lightbox="example-set"
href="data:image/jpeg;base64,<?php echo base64_encode($row['image'] )?>"
data-title="<?php echo $imgName;?>">
<img src="data:image/jpeg;base64,<?php echo base64_encode($row['image']) ?> />
</a>
</div>
</div>
<?php }
?>
You have missed $ for variable imgName
Try below code:
<?php
$query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)){
$imgName = $row['imgName'];
?>
<div class="col-sm-3 gallery-grids-left">
<div class="gallery-grid">
<a class="example-image-link" data-lightbox="example-set"
href="<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>"
data-title="<?php echo $imgName;?>">
<?php echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'"; '?>
</a>
</div>
</div>
<?php }
?>
First of all, as #Suyog pointed out, you missed $ on imgName variable in data-title. Second, if you are simplifying the code to use html as much as possible - do that and remove the extra stuff. Have something like this:
<?php
$query = "SELECT * FROM tbl_image ORDER BY imgID DESC";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)){
$imgName = $row['imgName'];
?>
<div class="col-sm-3 gallery-grids-left">
<div class="gallery-grid">
<a class="example-image-link" data-lightbox="example-set"
href='<img src="data:image/jpeg;base64,<?php echo base64_encode($row['image'] );?>"/>'
data-title="<?php echo $imgName;?>">
<img src="data:image/jpeg;base64,<?php echo base64_encode($row['image'] ); ?>">
</a>
</div>
</div>
<?php } ?>
Other than that, I would strongly advise against storing binary data in the database. This is not what the database is for. Store images are files and store filenames in the database. It'll make your life easier.

PHP Mysql $Id Issues Cannot Retrieve Data Using only $id as parameter

I am having problem of getting the values from mysql / php and I would appreciate if someone could help me. My problem is that I have a table called albums and in that table I have 6 columns which are id, album_name, artist, company, genre, price. I have written this code in my index.php:
<?php
$query = mysqli_query ($dbconn, "SELECT * FROM albums ORDER BY id DESC LIMIT 4 OFFSET 8");
while($result = mysqli_fetch_assoc($query)){
$id = $result["id"];
$album_name = $result["album_name"];
$img = $result["image"];
$artist = $result["artist"];
$company = $result["company"];
$genre = $result["genre"];
$price = $result["price"];
echo "<div class='col-md-3 col-xs-6'>
<a href ='album_page.php?id=$id' target='_blank' class='box_link_hover'><div class='box'>
<div class='inside_box'>
<div class='small_title'>
<h4>$album_name</h4>
</div>
<div class='photo_box'>
<img src=$img class='img_dim'>
</div>
<div class='info'>
<p>Artist: $artist</p>
<p>Company: $company</p>
<p>Genre: $genre</p>
<p>Price: $price</p>
</div>
</div>
<div class='buy_now'>
<p>Buy Now</p></a>
</div>
</div>
</div>";
}
?>
I have another page called album_page.php which i am sending from my index.php to album.php the $id in order to get the data from my database. In my album_page.php page i have this code:
<?php
$sql = mysqli_query ($dbconn, "SELECT id,album_name,artist,company,genre,price FROM albums WHERE id='$id'");
$id = $_GET["id"];
$album_name = $_GET["album_name"];
$artist = $_GET["artist"];
$company = $_GET["company"];
$genre = $_GET["genre"];
$price = $_GET["price"];
echo "<div class='col-md-4 right_box'>";
echo "<p>Album Name: $album_name</p>";
echo "<p>Artist: $artist</p>";
echo "<p>Company: $company</p>";
echo "<p>Genre: $genre</p>";
echo "<p>Price: $$price</p>";
echo "<div class='buy_now_box'>Buy Now</div>";
echo "</div>";
?>
What i would like to do is to send that $Id from my index.php and get all the data in my album_page.php using the $_GET. I would like to get all the data from my database with the $id = 5 for example. I have tried this using many paremeters for example:
<a href ='album_page.php?id=$id&image=$img&album_name=$album_name&artist=$artist&company=$company&genre=$genre&price=$price&buy_now=$buy'target='_blank' class='box_link_hover'><div class='box'>
and it is working fine but i would like to have the same result using only this:
<a href ='album_page.php?id=$id' target='_blank' class='box_link_hover'><div class='box'>
Thanks.
OK i have found what the problem was. I had to write in my album_page.php this in order to grap the data:
$sql = mysqli_query ($dbconn, "SELECT id,album_name,artist,company,genre,price FROM albums WHERE id='$id'");
$result = mysqli_fetch_assoc($sql);
$id = $_GET["id"];
$album_name = $result["album_name"];
$artist = $result["artist"];
$company = $result["company"];
$genre = $result["genre"];
$price = $result["price"];
Maybe this works even better ;-)
$id = $_GET["id"]
$sql = mysqli_query ($dbconn, "SELECT id,album_name,artist,company,genre,price FROM albums WHERE id='$id'");
$result = mysqli_fetch_assoc($sql);
// ....

Recommended user video Like youtube

I am trying to make a youtube like main page. With the code below I want to make videos that are recommended for my users.
The following code shows only a user's video.
<?php $query = "SELECT
user.uid,
user.user_name,
user.user_avatar,
user_posts.uid_dk,
user_posts.post_id,
user_posts.post_name,
user_posts.post_info,
user_posts.post_time,
user_posts.post_ext,
user_posts.post_num,
user_posts.post_views
FROM user
JOIN user_posts
ON user_posts.uid_dk = user.uid
WHERE user_name='$user_name' LIMIT 5";
$run_query = mysql_query($query);
while($data=mysql_fetch_assoc($run_query)){
$post_name=$data['post_name'];
$post_time = $data['post_time'];
$post_views = $data['post_views'];
$post_numid = $data['post_num'];
$post_id = $data['post_id'];
$user_name = $data['user_name'];
$user_avatar = $data['user_avatar'];
?>
<div class="onerilent"><img src="<?php echo $user_avatar;?>"><?php echo $user_name ;?> Recommended for you</div>
<div class="onmnwrp">
<div class="onmn">
<div class="onmn_img"><img src="<?php echo $base_url.'user_uploads/'.$post_num;?>.png"></div>
<div class="onmg_tit"><?php echo $post_name;?></div>
<div class="onm_snm">gönderen: <?php echo $user_name;?></div>
<div class="onm_tim"><?php echo $post_views;?> views</div>
</div>
</div>
<?php } ?>
I want to show this section only one time.
<div class="onerilent"><img src="<?php echo $user_avatar;?>"><?php echo $user_name ;?> Recommended for you</div>
Anyone can help me in this regard ?
The easiest way would be with a counter, like this:
<?php
$query = "SELECT
user.uid,
user.user_name,
user.user_avatar,
user_posts.uid_dk,
user_posts.post_id,
user_posts.post_name,
user_posts.post_info,
user_posts.post_time,
user_posts.post_ext,
user_posts.post_num,
user_posts.post_views
FROM user
JOIN user_posts
ON user_posts.uid_dk = user.uid
WHERE user_name='$user_name' LIMIT 5";
$run_query = mysql_query($query);
$counter = 1;
while($data=mysql_fetch_assoc($run_query)){
$post_name=$data['post_name'];
$post_time = $data['post_time'];
$post_views = $data['post_views'];
$post_numid = $data['post_num'];
$post_id = $data['post_id'];
$user_name = $data['user_name'];
$user_avatar = $data['user_avatar'];
if($counter == 1){
$counter++;
echo '<div class="onerilent"><img src="'.$user_avatar.'">'.$user_name.' Recommended for you</div>';
}
?>
<div class="onmnwrp">
<div class="onmn">
<div class="onmn_img"><img src="<?php echo $base_url.'user_uploads/'.$post_num;?>.png"></div>
<div class="onmg_tit"><?php echo $post_name;?></div>
<div class="onm_snm">gönderen: <?php echo $user_name;?></div>
<div class="onm_tim"><?php echo $post_views;?> views</div>
</div>
</div>
<?php
}
?>
note the $counter variable is set to 1 before the loop, and inside the loop there is a condition to check if it is set to the value 1, and if it is, then it echo's your html and increments the $counter, so that it is no longer set to 1

How do I loop through 2 columns?

I've got a table called tickets. The Tickets table has 4 columns: user_id, ticket_id, subject, message. I want to loop the ticket_id and subject of the logged in user. I want to echo it with foreach, but I cant get it to work. This is what I've got:
Query:
$showTheTickets = array();
$user_id = $_SESSION['user_id'];
$getTickets = mysqli_query($mysqli,"SELECT * FROM tickets WHERE user_id = '$user_id'") OR die (mysqli_error($mysqli));
while($row = mysqli_fetch_assoc($getTickets)){
$showTicketID = $row['ticket_id'];
$showTicketSubject = $row['subject'];
}
Code in the page:
<?php foreach ($showTheTickets as $showTheTickets): ?>
<div class="preview">
<?php echo $showTicketID ?>
<?php echo $showTicketSubject ?>
</div>
<?php endforeach; ?>
Anyones willing to help me? Thanks.
Try this:
$showTheTickets = array();
$user_id = $_SESSION['user_id'];
$getTickets = mysqli_query($mysqli,"SELECT * FROM tickets WHERE user_id = $user_id") OR die (mysqli_error($mysqli));
while($row = mysqli_fetch_array($getTickets)){
$row = array( 'ticket_id' => $row['ticket_id'], 'subject' => $row['subject'] );
$showTheTickets[] = $row;
}
And then:
<?php foreach ($showTheTickets as $stt): ?>
<div class="preview">
<?php echo $stt['ticket_id'] ?>
<?php echo $stt['subject'] ?>
</div>
<?php endforeach; ?>
Hope it helps...
You are overwriting the values you get from the query and your $showTheTickets array remains empty.
You probably want something like:
$showTheTickets = array();
while($row = mysqli_fetch_assoc($getTickets)){
$showTheTickets[$row['ticket_id']] = $row['subject'];
}
and:
<?php foreach ($showTheTickets as $id => $subject): ?>
<div class="preview">
<?php echo $id; ?>
<?php echo $subject; ?>
</div>
<?php endforeach; ?>
Why not simply
while($row = mysqli_fetch_assoc($getTickets)){
?><div class="preview"><?
echo $row['ticket_id'];
echo $row['subject'];
?></div><?
}
you have to save $showTicketID and $showTicketSubject in a array (could be the same array or 2 arrays) and then in the view show the arrays, like this:
while($row = mysqli_fetch_assoc($getTickets)){
$array[$row['ticket_id']] = $row['subject'];
}
and the view:
<?php foreach ($showTheTickets as $ticketid => $ticketsubject): ?>
<div class="preview">
<?php echo $ticketid ?>
<?php echo $ticketsubject ?>
</div>
<?php endforeach; ?>

Next and previous links and ID displayed in the URL for sharing

I have a database with a few images already set, I would like to have the url display an ID from each query as the user hits next. The user should be able to share the URL and paste it into their browser, the url should pull that unique ID from the query. The issue i am having is every time i paste a url, I get a random image and not the image that is in the ID. I'm at a loss here and im not sure what to do :( here's the code I have so far.
<?php
if (isset($_GET['id'])) {
include("PHP/db.php");
echo $where = $_GET["id"];
echo $query = "SELECT * FROM images WHERE ID =" . $where;
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$ID = $row['ID'];
$title = $row['name'];
$image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
if($_GET['next4']) {
echo 'HELLO THIS IS THE NEXT IF METHOD';
include("PHP/db.php");
$query = "SELECT * FROM images ORDER BY RAND() LIMIT 1";
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$ID = $row['ID'];
$title = $row['name'];
$image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
?>
<body>
</div>
<div id="title"> <?php echo $title ?> </div>
<div id="mainpic">
<?php echo $image ?>
</div>
<div id="prevnext">
<div id="next">
<a href="?id=<?php echo $ID ?>" name="name4" >Next</a>
</div>
<div id="prev">
Previous
</div>
</div>
Try this:
<?php
include("PHP/db.php");
$query2 = "SELECT * FROM images ORDER BY RAND() LIMIT 1";
$result2 = mysqli_query($dbc, $query2);
$rand_row = mysqli_fetch_array($result2);
$rand_id = $rand_row ['ID'];
if (!isset($_GET['id'])) {
$_GET['id'] = $rand_id;
}
if (isset($_GET['id'])) {
echo $where = $_GET["id"];
echo $query = "SELECT * FROM images WHERE ID =" . $where;
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$ID = $row['ID'];
$title = $row['name'];
$image = "<img height=500 width=600 src=http://www.goupics.com/img/" . $row['name'] . " >";
}
?>
<body>
</div>
<div id="title"> <?php echo $title ?> </div>
<div id="mainpic">
<?php echo $image ?>
</div>
<div id="prevnext">
<div id="next">
<a href="?id=<?php echo $rand_id; ?>" name="name4" >Next</a>
</div>
<div id="prev">
Previous
</div>
</div>
I changed:
-link to next is now id=rand
-changed your code to give me a "rand ID" and its already defined on the href of the page you load
It will go inside condition (next == true).
Make sure that your variables are initialized before use.

Categories