Ok obviously i'm a beginner.
What i'm trying to do here is probably simple.
I'm trying to get all the field of my database to echo on my website. So i use this to show only the basic details:
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("crnew") or die(mysql_error());
$result = mysql_query("SELECT * FROM releases")
or die(mysql_error());
while($row = mysql_fetch_array( $result )
) {
// Print out the contents of each row into a table
echo '<ul class="releaselist">';
echo '<li>';
echo $row['products_name'];
echo '</li>';
echo '<li>';
echo $row['products_title'];
echo '</li>';
echo '<li>';
echo '<img class="releaseimg" src="'.$row['products_image'].'">';
echo '</li>';
echo '</ul>';
}
?>
So far eveything works fine.
What i want to do with this is get my URL to look like this when i click on a single image.
www.mywebsite.com/detailed.php?id=1
and show everything available in the table RELEASES
The way i've done it which is not working is:
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("crnew") or die(mysql_error());
$result = mysql_query("SELECT * FROM releases
WHERE products_id=products_id")
or die(mysql_error());
if ($result){
$row = mysql_fetch_array($result);
echo $_GET['products_id'];
}
?>
If I'm understanding you correctly, your first script needs to be updated to:
echo '';
That will turn your image into a link. At that point your second script should be able to use:
_GET['id']
to retrieve this value and use it in your query. Thusly:
$result = mysql_query("SELECT * FROM releases
WHERE products_id=" . mysql_real_escape_string($_GET['id']));
Now if you really want to products_id in the URL instead of id, just change it 'id' to products_id in the echo line in the first script and all of the $_GET uses in the second script.
Try changing this line:
echo '<img class="releaseimg" src="'.$row['products_image'].'">';
To this one:
echo "<a href='detailed.php?products_id=".$row['products_id']."'><img class='releaseimg' src='".$row['products_image']."'></a>";
Hmm try using $row['products_id'] instead of $_GET['products_id']; $_GET is used when accessing a webpage and the reason that $row['products_image'] worked is because you accessed the database results.
You have misplaced the use of products_id...
Your code should like something like this...
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("crnew") or die(mysql_error());
$result = mysql_query("SELECT * FROM releases
WHERE products_id='".intval($_GET['id'])."'")
or die(mysql_error());
if ($result){
$row = mysql_fetch_array($result);
echo $row['products_id'];
}
?>
Related
I currently have this code which displays all required information on the page:
$sql = "select * from livecalls ORDER BY Completion_Date ";
$query = mysql_query( $sql );
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td>$row[ID]</td>";
echo "<td>$row[Type]</td>";
echo "<td>$row[VNC_Number]</td>";
echo "<td>$row[Completion_Date]</td>";
echo "<td>$row[Logged_By]</td></tr>";
}
echo "</table>";
This works fine, however I am wanting to be able to click the $row[ID] section to open a new window and display the $row[Problem] which is related to that ID number..
I'm struggling to think how to get the ID information across to a new page to be able to search for the right Problem information to display and the code to do this?
Any help would be appreciated.
You can use a anchor tag for this purpose like
echo "<tr><td><a href='yourpage.php?id=".$row[ID]."'>".$row[ID]."</a></td>";
and in new page, you can use
$_GET['id'] for getting the id.
Also while echoing html along with php variable try to do like this:
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td><a href='yourpage.php'>".$row['ID']."</a></td>";
echo "<td>".$row['Type']."</td>";
echo "<td>".$row['VNC_Number']."</td>";
echo "<td>".$row['Completion_Date']."</td>";
echo "<td>".$row['Logged_By']."</td></tr>";
}
I guess your not using any php framework (ex. codeigniter). Because if you do,this is easy using routing.
The very basic solution is Passing variables in a URL
http://html.net/tutorials/php/lesson10.php
Then if you want it to open in a new window instead of current window use
Use _blank in your a href
Visit W3Schools!
$sql = "select * from livecalls ORDER BY Completion_Date ";
$query = mysql_query( $sql );
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td><a href='your_url/?id=".$row[ID]."'>$row[ID]</a>`enter code here`</td>";
echo "<td>$row[Type]</td>";
..
...
....
I have the following code that works by outputting as a link ( the link comes from a field in my database) I wish to do the same for the code below, however i cannot get it work, here is the example of what I have that works, and the code that i wish to make output as a link:
Working Code what I want it to look like
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM adrenaline WHERE title LIKE '%".$term."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
echo '<br> '. $row['title'] .'';
}
}
?>
And the code that i have at the moment, it works by be manually typing in the hyper link, however I wish to make it take the link from the database like the example above
//query the database
$query = mysql_query("SELECT * FROM hobby WHERE id = '1' ");
//ferch the results / convert results into an array
WHILE($rows = mysql_fetch_array($query)):
$title = $rows['title'];
echo "<a href='shard.php'>$title</a>";
endwhile;
?>
Many thanks!
I am not 100% certain if this is what you meant to ask... let me know in comments:
<?PHP
$query = mysql_query("SELECT * FROM hobby WHERE id = '1' ");
if(mysql_num_rows($query) >= 1) {
while($rows = mysql_fetch_array($query)) {
echo sprintf("%s", $rows["description"], $rows["title"]);
}
} else { echo "No hobbies found."; }
?>
I believe you might have faced some syntax issues while dealing with quotes parsing a variable in <a html tag. Consider using sprintf something like in my example.
I have also added a mysql_num_rows() just in case and you can see its a good fail-safe method incase there are no rews found on any select query.
IMPORTANT: STOP using mysql_ functions because its deprecated from new PHP versions. Use PDO or mysqli instead.
I am new to PHP, I want to get image information from database when I click on a specific image.
This is the code I'm using, could you help me out?
Code:
<?php
$connection = mysql_connect("localhost","root","");
$select_db = mysql_select_db("fashion",$connection);
$winter = mysql_query("Select * from winter",$connection);
while($row = mysql_fetch_array($winter))
{
echo "<img src=\"winter images/" . $row['image_name']. "\" width=\"200\" //height=\"293\"/>";
}
?>
Did you try echo $row['image_name']; to see whether you are getting the image name or not. If you are getting it properly then try this
while($row = mysql_fetch_array($winter))
{
$imagePath='winter/'.$row['image_name'];
echo '<img src="'.$imagePath.'" width="200" height="293" >';
}
1. Why are you messing up so much with the image tag in your echo statement. if you put double quotes inside the single quotes then also it will work.
2. **Please stop using *mysql_ as it was deprecated before it was deprecated.
Since mysql_fetch_array only pulls regular array, replace it with mysql_fetch_assoc. This should work !!
<?php
$connection = mysql_connect("localhost","root","");
$select_db = mysql_select_db("fashion",$connection);
$winter = mysql_query("Select * from winter",$connection);
while($row = mysql_fetch_assoc($winter)){
echo '<img src= "folder/'.$row["image_name"].'" width="200" height="293">';
}
?>
Since mysql is deprecated, try using Mysqli or PDO
I have a page displaying random bible quotes from which you can also search. The quotes on this page are displayed in dynamic link format, e.g. bible-query.php?id=200
On the search results page, I have put a link each at the bottom and top of the page to help the users get back to the random display page. These links are dynamic links. The only problem is, I can only get the top link to display the dynamic link, the bottom one just loads a page with no quotes.
What I want is to have the bottom and top 'Back' links display the same dynamic link location.
Here is the code I have for the search results:
<html>
<font face="arial">
<title>BQuotes: Random Bible Verses</title>
<?php
// db requirements
$db_host="localhost";
$db_username="username";
$db_password="password";
$db_name="name";
$db_tb_name="table";
$db_tb_atr_name="line";
$db_tb_atr_name2="book";
$db_tb_atr_name3="cap";
$db_tb_atr_name4="verse";
//do search task
mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");
$query=mysql_real_escape_string($_GET['query']);
$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE
$db_tb_atr_name like '%".$query."%' OR $db_tb_atr_name2 like '%".$query."%'
OR $db_tb_atr_name3 like '%".$query."%' OR $db_tb_atr_name4 like '%".$query."%'");
echo "Search Results<ol>";
// new bible query section begins
define ('HOSTNAME', 'localhost');
define ('USERNAME', 'username');
define ('PASSWORD', 'password');
define ('DATABASE_NAME', 'name');
$db = mysql_connect(HOSTNAME, USERNAME, PASSWORD) or die ('I cannot connect to
MySQL.');
mysql_select_db(DATABASE_NAME);
$query = "SELECT id,book,cap,verse,line FROM table ORDER BY RAND() LIMIT 1 ";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo "<center><a href='bible-query.php?id=$row[id]'>Back</a></center> ";
}
//mysql_free_result($result);
//mysql_close();
//bible query new section ends
while($data_fetch=mysql_fetch_array($query_for_result))
{
echo "<li>";
echo substr($data_fetch[$db_tb_atr_name2], 0,160)," ";
echo substr($data_fetch[$db_tb_atr_name3], 0,160)," ";
echo substr($data_fetch[$db_tb_atr_name4], 0,160)," ";
echo substr($data_fetch[$db_tb_atr_name], 0,160);
echo "</li><hr/>";
}
echo "<center><a href='bible-query.php?id=$row[id]'>Back</a></center> ";
echo "</ol>";
//mysql_close();
?>
</font>
</html>
Please help!
Your while loop is fetching the row using mysql_fetch_array. On the first time it runs it retrieves a result and so the value of $row is set to an array and the conditional is "true" so the bit in the while loop runs. However, on the second run-through there is no second row and so $row is set to false and the while loop doesn't run. This means that after the while loop $row is an empty variable. When it gets to the second calling of that array it is empty. By removing the while loop the $row variable is only fetched once and the first row is returned. This is all you need.
Just change this bit:
while ($row = mysql_fetch_array($result)) {
echo "<center><a href='bible-query.php?id=$row[id]'>Back</a></center> ";
}
to:
$row = mysql_fetch_array($result);
echo "<center><a href='bible-query.php?id=$row[id]'>Back</a></center>";
and both parts should work.
Change your code to this. No need for a while loop since you are limiting 1 row in your query. Also I changed the php mysql function that you should use.
$query = "SELECT id,book,cap,verse,line FROM table ORDER BY RAND() LIMIT 1 ";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo "<center><a href='bible-query.php?id=$row[id]'>Back</a></center> ";
Noticed I used mysql_fetch_assoc
I have a home page where database value are showing (e.g. a image and few information)
My problem is whenever i click on the image it jumps to another page and show all the pictures that are store in database,but i want the clicked one.
Here is my 1st page home_page.php code
<?php
$db = mysqli_connect("localhost", "root", "", "registration_data");
$sql = "SELECT * FROM add_data";
$result = mysqli_query($db,$sql);
while($row = mysqli_fetch_array($result)){
$image = $row['image'];
echo "<div id='img_div'>";
?><?php echo "<img src='images/".$row['image']."' >";?><?php
echo "<p>Description: ".$row['add_description']." </p>";
echo "<p>Cetegory: ".$row['catagory']. "</p>";
echo "</div>";
}
?>
And this is my second page where i want to show only clicked image information.(Details.php)
<?php
$image = intval($_GET["image"]);
$db = mysqli_connect("localhost", "root", "", "registration_data");
$sql = "SELECT * FROM add_data where 'id' = $image ";
$result = mysqli_query($db,$sql);
while($row = mysqli_fetch_assoc($result)){
$imageShow = $row['image'];
print $imageShow;
}
?>
So, if your initial page for calling the image is something like:
<body>
<img src="getImage.php?**id=1**" width="175" height="200" />
</body>
Then getImage.php is
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb");
$sql = "SELECT dvdimage FROM dvd WHERE id=**$id"**;
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo $row['dvdimage'];
?>
You're currently passing row, rather than ID.
Your table isn't telling anyone what cell is which, you should be using a primary key as a reference id.
so your SQL should select the table colums and assign them to values within each row, then you pass in the query to pull back the image with a certain primary key.
i.e. row is a PHP value, you need to extract and reference a DB value, a primary key. i.e. ImageID.