How to repeat different pic? - mysql

I wan to always display the different pic. So there's a problem with looping the different pic. How do i do that?
index.php:
<?php
$result = mysql_query("SELECT * FROM table2", $connection );
echo '<ul>';
while($row = mysql_fetch_array($result)) {
if ($row)
echo "<li>";
echo "<img src='code.php?id=3'>";
echo "&nbsp";
echo "</p>";
echo "</li>";
echo "<br/>" ;
}
echo '</ul>';
mysql_close($connection);
?>

You need to pull the id value from the database. If you have a column called, maybe id you would want to put:
while($row = mysql_fetch_array($result)){
if ($row){
echo "<li>";
echo "<img src='code.php?id=".$row['id']."'/>"; // see what I did there?
echo "&nbsp";
echo "</p>"; // take this out
echo "</li>";
echo "<br/>"; // take this out
}
}
P.S. - don't write new code using the deprecated mysql extension. Use PDO or mysqli at least. And don't put <br />s between your <li>s, or close unopened <p>s. And, generally speaking, don't store your images in your database - just store the path to the image and put the images themselves in a folder on the server where they belong.
And please format your code - it is hard to read with no indentation or separation of files (your if statement was not properly enclosing what should have been conditioned statements). And mysql_real_escape_string is not as cool as you think it is.
Hope this helps.

Related

Getting data from database using part of the URL as a value - PHP

My url type is like "http://localhost/boardgame/profile/Duncan/" the last word is the user name...
I just try to get some data from database and I need a user name to do this. I tried to get it from page URL but it doesn't work. Where did I go wrong? What has to be done? I'll be greateful if anybody can help...
<?php
$actual_link = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
//echo $actual_link;
$rest = substr($actual_link, 35, -1);
echo $rest;
$link = mysqli_connect("localhost", "root", "", "bgt");
if (mysqli_connect_error()) {
die ("Veritabanı bağlantısında hata var");
} if (($rest) !="") {
if (get_current_user_id($link) == '0') {
echo "<p>Kullanıcının koleksiyonunu görebilmek için giriş yapmanız
gerekiyor.</p>";
} else {
$query = "SELECT TITLE FROM wp_user_collections WHERE `user_name` =
'".mysqli_real_escape_string($link, $rest)."'";
echo "<div><h1>'".mysqli_real_escape_string($link, $rest)."'in
Koleksiyonu</h1><div>";
if($result = mysqli_query($link, $query)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Oyun Adı</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>'" . $row['TITLE'] . "'</td>";
echo "</tr>";
}
echo "</table></div>";
}
}
}
} else {
echo "Kullanıcının koleksiyonunu görebilmek için <a
href='http://localhost/boardgame/profile/'".mysqli_real_escape_string($link,
$rest)."'>tıklayınız</a>";
}
?>
Here is the solution I have thought...using array then extracting the last element (or second last since there is a trailing '/' slash which is a delimiter)
$uri="http://localhost/boardgame/profile/Duncan/";
$uriParts=explode('/', $uri); //converts the uri string into an array of words separated by forward slash
$name=$uriParts[sizeOf($uriParts)-1-1]; //the user name comes from 2nd last item as the last item is empty
echo $name;
I could suggest you use Regular Expression to extract the user name, however I'm not well versed with it yet.

Create a div on my website that shows database information with php

I need to create a div that only shows the information of the database, how should i connect the div to mysql so it only shows my database information?
I'm not sure I'm understanding your question... but I'll give it a try. The code below this will select whatever you tell it to from the database and echo it into a table.
<?php
$db = mysqli_connect('host', 'username', 'password', 'database');
$sql = "SELECT * FROM database"
mysqli_stmt_bind_param($db, "params here", your info here);
$res = mysqli_stmt_execute($sql);
mysqli_stmt_close($sql);
mysqli_close($db);
echo "<table>";
while($row = mysqli_fetch_array($res)){ //Creates a loop to loop through results
echo "<tr><td>" . $row[''] . "</td><td>" . $row[''] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>";
?>

PDO: How can I etrieve an image address from MySQL and print it?

I'm stepping up from mysql to PDO and I'm getting close as all my fields print now except the images. These are held in a separate file with only the address in the database. I can retrieve these in mysql but I'm having trouble doing it in PDO.
Where I should see the image, the best I've been able to achieve is getting it to print "Arrayh" at the end of each line. I don't seem to be able to find how to do this anywhere so here I am again. This is the part of the file that prints.
$STM->bindParam(':Country', $Country, PDO::PARAM_STR);
$STM->execute();
$row= $STM->fetchAll();
$img_url = "http://www.xxxxx.net/images/";
if(count($row)){
foreach($row as $data){
echo $data['Country'] ." ".$data['Year']." ".$data['Description']." ".$data.$img_url['Images']."<br />";
}
}else {
echo 'no row found';
}
Say if you called your image field img_url
foreach($row as $data){
echo "<img src='"$data['img_url']."'><br />";
}
I've eventually got it to work so I will put what I've used here otherwise this question will be useless for people in the future who have the same problem. The echo data line is replaced by two lines :
echo $data['Key'] ." ".$data['Country'] ." ".$data['Year']." ".$data['Description']." ";
echo "<img src='$img_url".$data['Images']."' <br /><br />" ;
"<br />";
Now I just need to get it into a table to neaten it up a bit. Thanks to all who helped.

how to display images whose names are stored in database

I had created a table in mysql database. I am using php One column was for images, but it only stored images name. I wanted to print the whole table so did the coding but got got only the name of the images in the table.
Please explain how can I display images on the webpage that are stored in my PC. I don't want to save images in the database.
I used this code to print the table :
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$query="SELECT * FROM images LIMIT 0,5";
$result = #mysql_query($query);
echo "<table border='1'>
<tr>
<th>S.No.</th>
<th>Image</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['image_id'] . "</td>";
echo "<td>" . $row['filename'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Hope you got some answer....
All your code is doing is sending the filename to the screen as a string. You need to the use image tags.
You may find this page useful, http://www.htmlcodetutorial.com/images/_IMG.html

Getting mysql field data when a link is clicked?

I'm trying to get data from a database if a link is clicked.
Basically i have a main.php that contains this:
$sql2="SELECT projectsid, projectname, description,
SUBSTRING(description,0,80) FROM projects";
$result2=mysql_query($sql2);
while($row2 = mysql_fetch_assoc($result2)) {
echo "<div id=\"links\">";
echo "<ul>";
echo "<li> <a href=\"fullproject.php\">" . $row2['projectname'];
$_SESSION['projectname']= $row2['projectname'];
echo "<em>" . $row2['description'] . "</em></a></li>";
echo "</ul>";
echo "</div>";
}
This displays a list of names and a brief description.
Proj1
description
Proj2
description
What i'd like to do is to display the full contents of a project if that one is clicked.
fullproject.php
<?php
session_start();
$projectname= $_SESSION['projectname'];
// Connect to server.
require ("connect.php");
$sql1="SELECT projectsid, projectname, programme, difficult, requirements,
resources, description, contact, gsize, size
FROM projects WHERE projectname = '$projectname'";
$result1=mysql_query($sql1);
while($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['projectname']. "<br />";
echo "Programme : " . $row1['programme'] . "<br />";
echo "Difficult : " . $row1['difficult'] . "<br />";
echo "Requirements : " . $row1['requirements'] . "<br />";
echo "Resources : " . $row1['resources'] . "<br />";
echo "Description : " . $row1['description'] . "<br />";
echo "Contact : " . $row1['contact'] . "<br />";
echo "Group size : " . $row1['gsize'] . " " . $row1['size'] . "<br />";
echo "<br /> ";
}
Whenever I click any of the project list items, it only displays the last itemin the list.
I believe this is happening because when the while loop ends the session variable is set to the last one.
Can someone tell me how to fix this?
Thanks
Try using GET variable instead of session
in main.php use
echo "<li> <a href='fullproject.php?projectname=$row2[projectname]'>";
in fullproject.php declare a variable and initialize it to the get variable
$projectname= $_GET['projectname'];
There is a lot wrong with this code.
1) You are trying to send the primary key of the table via 2 different methods in main.php: in the session and via the path of the URL. This is just bad practice - the reason you have access to different conduits for passing data is that they all have different semantics, but even then, sending the same thing twice then there's always going to be a risk that the receiving end may see different values and must terefore have a way of reconciling the discrepancy.
2) You are using the session to pass a transactional data item. This is a bad idea - the data item relates to a specific transition - not to the session. $_SESSION should only be used for storing data relating to the sesion - not to navigation or transactions.
3) The other conduit you provided for passing the data item is by appending it to the path within the URL. While in some circumstances this approach has benefits it also has complications specific to the webserver the code is implemented on - you've not provided any explanation of why you are using this apporach instead of a conventional $_GET or $_POST var.
The reason your code is failing is because you can only store a single value in $_SESSION['projectname']. Even if you stored an array of values in the session, there is no mechanism for the webserver to know which of these values was selected when the code was delivered to the browser.
Your HTML is badly structured and poorly formatted too.
The code you've shown does not match the description you've given (it does not display the project name).
Your code is also wide open to SQL injection attacks.
Change it so that in main.php:
echo "<div id=\"links\">\n";
echo "<ul>\n";
while($row2 = mysql_fetch_assoc($result2)){
echo "<li> <a href=\"fullproject.php?project=\""
. urlencode($row2['projectname']) . "\">"
. htmlentities($row2['projectname']) . "</a>\n";
echo "<br /><em>" . $row2['description'] . "</em></li>";
}
echo "</ul>";
echo "</div>";
And in fullproject.php
<?php
session_start();
require ("connect.php");
$projectname= mysql_real_escape_string($_GET['projectname']);
echo"<a href='fullproject.php?project_id='".$row2['project_id'].
"> $row2['projectName']</a>";
in fullproject.php
$project_id=$_GET['project_id'];
then you can use this id to display contents of that project id from database