im having a simple mysql/php problem. so i am adding in Image titles for my website, and the code is displayed below. It works, but when you dont put a image, it shows up as blank. I need it to show up as 'No image title' (bc i will use this for image description to). It basically gets the image name, then takes the title from that row.
So how do i do it? :/ im still very new to PHP.
<?php
if (isset($imgtitleset))
{
$sql = "SELECT image_title FROM images WHERE image_name = '$image_main'";
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result))
{
$imgtitle= $row["image_title"];
echo "$imgtitle";
}
}
else {
echo 'no image title';
}
?>
Change the while loop like so:
while ($row = mysql_fetch_array($result)) {
$imgtitle= $row["image_title"];
if($imgtitle != '') {
echo $imgtitle;
} else {
echo 'no image title';
}
}
Also, I'm not sure what the $imgtitleset variable is for, but you can probably get rid of the if statement checking to see whether it's set.
Edit: the whole thing should probably look like this:
<?php
$sql = "SELECT image_title FROM images WHERE image_name = '$image_main'";
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result)) {
$imgtitle= $row["image_title"];
if($imgtitle != '') {
echo $imgtitle;
} else {
echo 'no image title';
}
}
?>
This all depends on what $imgtitleset is equal to. It is clearly set against something:
while ($row = mysql_fetch_array($result)) {
$imgtitle = $row["image_title"];
if (isset($imgtitle))
echo "$imgtitle";
else
echo 'no image title';
}
This would mean if nothing was found in the database then it will echo the no image title. However like I said, this could depend on what $imgtitleset is, maybe post the code for that?
If you only expect the select to return a single row, then use if rather than while and return the error on else:
<?php
if (isset($imgtitleset))
{
$sql = "SELECT image_title FROM images WHERE image_name = '$image_main'";
$result = mysql_query ($sql);
if ($row = mysql_fetch_array($result))
{
$imgtitle= $row["image_title"];
echo "$imgtitle";
}
else {
echo 'no image title';
}
}
?>
Related
This problem should be simple to resolve, but I can't...
After a request, a condition has to verify if the concerned article really exists, verifying the URL ($_GET).
My code : ( a testing file with simple echos )
$id = $bdd->prepare('SELECT content FROM articles WHERE idArticle = ?');
$id->execute(array($_GET['numArticle']));
while ($dataID = $id->fetch()) {
if (empty($dataID) or $dataID == null or !isset($dataID)) {
echo 'No content';
} else {
echo 'Can load the page';
}
}
$id->closeCursor();
The page behaviour : "can load the page" is writing when numArticle is right, but if it is not, nothing appears, neither an error message or something.
Any idea/advice? Thank you.
One way to do this is by checking the number of rows returned by mysqli:
$id = $bdd->prepare('SELECT content FROM articles WHERE idArticle = ?');
$id->execute(array($_GET['numArticle']));
if($id->num_rows > 0){
echo "can load page";
}else{
echo 'No content';
}
You can use fetchAll() function of PDO then run a foreach loop on data:
$rows = $id->fetchAll(PDO::FETCH_ASSOC);
if(!empty($rows)){
foreach($rows as $row){
echo "content";
}
}
else{
echo "No Content";
}
Assuming you are using Pdo, it looks like you want to be using Pdo's fetchColumn method.
<?php
$stmt = $db->prepare('SELECT content FROM articles WHERE idArticle = ? LIMIT 1');
$stmt->execute(array($_GET['numArticle']));
if ($result = $stmt->fetchColumn()) {
echo 'A result';
} else {
echo 'Db fetch returned false (or could be a string that evaluates to false).';
}
I am going out of my mind as it is. I am trying to pass a variable from a page that shows all albums thumbnail image and name to a page that will display all the pictures in that gallery using that passed variable, but the variable is empty in the url on the target page. I have seen similar cases on the web and on this site and I've applied the suggestions but it's still the same. Here is the code that lists the thumbnail and passes the variable(id).
<?php
include ("config.php");
$conn = mysqli_connect(DB_DSN,DB_USERNAME,DB_PASSWORD,dbname);
$albums = mysqli_query($conn,"SELECT * FROM albums");
if (mysqli_num_rows($albums) == 0) {
echo "You have no album to display. Please upload an album using the form above to get started. ";
}
else{
echo "Albums created so far:<br><br>";
echo "<table rows = '4'><tr>";
while ($thumb = mysqli_fetch_array($albums)) {
echo '<td><a href ="view.php?id="'.$thumb['id'].'"/><img src = "'.$thumb['thumbnail'].'"/><br>'.$thumb['album_name'].'<br>'.$thumb['id'].'</a></td>';
}
echo "</tr></table>";
}
?>
The code for getting the passed variable is as follows:
<?php
include("config.php");
$conn = mysqli_connect(DB_DSN,DB_USERNAME,DB_PASSWORD);
$db = mysqli_select_db($conn,dbname);
if (isset($_GET['id'])) {
$album_id = $_GET['id'];
$pic = "SELECT * FROM photos WHERE album_id ='$album_id'";
$picQuery = mysqli_query($conn,$pic);
if (!$picQuery) {
exit();
}
if (mysqli_num_rows($picQuery) == 0) {
echo "Sorry, no Pictures to display for this album";
}
else{
echo "Pictures in the gallery:<br><br>";
while ($result = mysqli_fetch_assoc($picQuery)) {
echo "<img src='".$result['photo_path']."'/>";
}
}
}
?>
Please help as i have spent the last two days trying to get it right.
First, your's code is weak against sql injections:
$album_id = $_GET['id']; // here
$pic = "SELECT * FROM photos WHERE album_id ='$album_id'";
Use either $album_id = intval($_GET['id']) or prepared statements functionality.
Second, add debug lines to your code, like:
<?php
include("config.php");
if (isset($_GET['id'])) {
$album_id = intval($_GET['id']);
var_dump($album_id); // should print actual passed id
$conn = mysqli_connect(DB_DSN,DB_USERNAME,DB_PASSWORD);
var_dump($conn _id); // should print conn resource value
$db = mysqli_select_db($conn, dbname);
var_dump($db); // should print 'true' if db select is ok
$pic = "SELECT * FROM photos WHERE album_id ='$album_id'";
$picQuery = mysqli_query($conn, $pic);
var_dump($picQuery); // should print query resource value
if (!$picQuery) {
exit();
}
if (mysqli_num_rows($picQuery) == 0) {
echo "Sorry, no Pictures to display for this album";
} else {
echo "Pictures in the gallery:<br><br>";
while (($result = mysqli_fetch_assoc($picQuery)) !== false) {
var_dump($result ); // should print fetched assoc array
echo "<img src='".$result['photo_path']."'/>";
}
}
}
Notice $album_id = intval($_GET['id']) and while (($result = mysqli_fetch_assoc($picQuery)) !== false) parts
Then follow link view.php?id=<existing-album-id> and observe debug result. On which step debug output differs from expected - there problem is.
I have the following code to check if a row exists in MySQL:
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT 1 FROM files WHERE id='$code' LIMIT 1");
if (mysql_fetch_row($result)) {
echo 'Exists';
} else {
echo 'Does not exist';
}
}
?>
This works fine. But I need to change it a bit. I have the following fields:
id, title, url, type. When someone uses the code above ^ to check if a row exists, I need a variable to get the url from the same row, so I can redirect the user to there.
Do you have any idea how I can do that?
Thanks in advance! :)
Try this:
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT * FROM files WHERE id=" . $code . " LIMIT 1");
if (mysql_num_rows($result) > 0) {
while($rows = mysql_fetch_array($result)) {
echo 'Exists';
$url = $rows['url'];
}
} else {
echo 'Does not exist';
}
}
?>
It is quite simple. I think you don't show any effort to find the solution by yourself.
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT url FROM files WHERE id='$code' LIMIT 1");
if ($result) {
$url = mysql_fetch_row($resultado);
} else {
echo 'Does not exist';
}
}
<?php
$sql_query = "SELECT * FROM test WHERE userid ='$userid'";
$result1 =mysql_query($sql_query);
if(mysql_num_rows($result1)>0){
while($post = mysql_fetch_array($result1))
{
$url = $post['url'];
}
}
?>
If mysql_num_rows($result1)>0 it means row is existed fir the given user id
I need a little help of my brilliant friends.
Actually i m new to development so that i have no much idea how can i show my page in words like www.testsite.com/index.php?pname=**Home** except of www.testsite.com/index.php?pid=**1**
i have the following code for showing page in number
if (!$_GET['pid']) {
$pid = '1';
} else {
$pid = ereg_replace("[^0-9]", "", $_GET['pid']); }
and the sql code
$sqlCommand = "SELECT id, link FROM main_page WHERE showing='1' ORDER BY id ASC";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$menu='';
while ($row = mysqli_fetch_array($query)) {
$pid = $row["id"];
$link = $row["link"];
if ($linklabel){
$menu .=''. $link .'';
}}
i want to show and href name of page not id how can i do that.
help plz
Your example will fail if I enter *1*2*3*
you should be searching for the contents of
**(contents)**
and nothing else.
That will get you the name and the number.
Here is my example
$string = "**123naasdme456**";
preg_match("/[^\*+](?P<val>\w+)[^\*+]/",$string,$matches);
echo $matches[0];
will echo 123naasdme456
and here it is implemented into your code
function getReal($urlVar)
{
if(preg_match("/[^\*+](?P<val>\w+)[^\*+]/",$urlVar,$matches))
{
return $matches[0];
}
return false; // or default value
}
$pid = getReal($_GET['pid']);
$name = getReal($_GET['pname']);
You should add an extra field in your database (table main_page) with the name name or something similar. Then you could:
if (!$_GET['pname']) {
$pid = 'home';
} else {
$pid = mysql_real_escape_string($pid);
$sql = mysql_query("SELECT name FROM main_page WHERE name = '$pid'");
if (mysql_num_rows($sql) == 1))
{
echo "Content";
} else {
echo "404 error. Couldn't find the page you were looking for.";
}
}
URL Rewriting. http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/
I have setup an image table in my database to store my images as blob type. My problem is that i do not know how to display the images in the db from my web search page. Anytime i enter a searh query, it will display the keyword & the image name but it will not display the image itself. rather it displays long sql codes.
Here are my php codes for Imagesearch.php;
<style type="text/css">
body {
background-color: #FFF;
}
</style>
<?php
//get data
$button = $_GET['submit'];
$search = $_GET['search'];
$x = "";
$construct = "";
if (!$button){
echo "You didint submit a keyword.";
}
else{
if (strlen($search)<=2) {
echo "Search term too short.";
}
else {
echo "You searched for <b>$search</b><hr size='1'>";
//connect to database
mysql_connect("localhost","root","");
mysql_select_db("searchengine");
//explode our search term
$search_exploded = explode(" ",$search);
foreach($search_exploded as $search_each) {
//constuct query
$x++;
if ($x==1) {
$construct .= "keywords LIKE '%$search_each%'";
}
else {
$construct .= " OR keywords LIKE '%$search_each%'";
}
}
//echo out construct
$construct = "SELECT * FROM images WHERE $construct";
$run = mysql_query($construct) or die(mysql_error());
$foundnum = mysql_num_rows($run);
if ($foundnum==0) {
echo "No results found.";
}
else {
echo "$foundnum results found!<p>";
while ($runrows = mysql_fetch_assoc($run)) {
//get data
$name = $runrows['name'];
$image = $runrows['image'];
echo "
<b>$name</b><br>
$image<br>
";
}
}
}
}
You should have used Google... Link
You need a separate php script that will return the image itself, and then you will need to call that from your PHP script.
The other PHP script needs to set the Content-type header to the proper mime type.
You should make another script, at another URL, that accepts an ID through a get parameter to output the image. Could then do something along the lines of:
<?php
// ..your MySQL stuff
function error() {
echo "There was an error";
}
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$id = (int) $_GET['id'];
$sql = "SELECT * FROM images WHERE id = '$id'";
$query = mysql_query($sql, $conn);
if (mysql_num_rows() == 1) {
$data = mysql_fetch_assoc($query);
// Change this to the correct image type for your stored data
header("Content-type: image/gif");
echo $data['image'];
} else {
error();
}
} else {
error();
}
You'd then echo:
echo "<b>$name</b> <img src='theimagescript.php?id={$id}' alt='Image of $name' />";