Different meaning in the mysql code? - php

$result=mysql_query("select * from dosyabegeni where veri_id='" . get_custom_field('dwcode') . "'");
Not Working
It says the number and the screen, but the application does not work
veri_id='" . get_custom_field('dwcode') . "'");
veri_id='" . echo get_custom_field('dwcode') . "'");
Working
veri_id='HelloTest'");
veri_id='1234567890'");
veri_id='" . $_GET['test'] . "'");
Main Codes
<?php
include('/home/emre2010/public_html/EntegreOz/DosyaBegeni/config.php');
$result=mysql_query("select * from dosyabegeni where veri_id='" .get_custom_field('dwcode') . "'");
while($row = mysql_fetch_array($result))
{
$sira_id=$row['sira_id'];
$veri_id=$row['veri_id'];
$begeni=$row['begeni'];
?>
<div class="reviewbox">
<div class="summarywrap">
<div class="summarywrapinner">
<div class="summary">
<div class="reviewsection"><div class="rating points">
<a href="#" class="begeni" id="<?php echo $sira_id; ?>">
<span style="color:#fff;" align="center"> <?php echo $begeni; ?> </span>
</a>
<p class="ratingtext">completed!</p></div>
</div><div class="clear"></div>
<div class="clear"></div>
</div>
<div class="ratingsummary"></div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
What's the problem?

Are you meaning to run the review box from within the while loop? If your SQL is only supposed to return one row, you really shouldn't have all that stuff within the while loop. I don't see an end bracket. If you are then you're doing it pretty archaically. You should put the information from the while loop into an array instead and then run a separate loop in your content, but that's not really going to solve the problem. The only issue you could have is either not returning rows because they don't exist, or an error in your SQL.
If your SQL query isn't returning any rows then you need to do two things. One, echo out the sql query. And two, print any possible errors.
echo $sql.'<br />';
print(mysql_error());
You never know, you might have misspelled a column on the table of the db itself.
If that's not working, then print the $row out.
Outside of your while loop do:
print_r($row);

Related

how to call columns from database and format it with HTML code

Actually I am beginner programmer in HTML, CSS and PHP. I have simple website for add and register in courses. The user should be add course into website and the courses should be posted on the site.so users can browse and register.
Actually my problem is how to call the course name from database and how to format it with HTML code as I want.
This is the page of courses which is content the list of available courses in website ( please note it is only HTML code, I do that to see how the page will be )
Screenshot of page:
So as you see, the first page include many this HTML code to add course list into website with the following code:
<div class="card card-1">
<a href="http://127.0.0.1/project2/course details/course1.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% "></a> <div class="container">
<h4 class="textstyle"><b>Operating System</b> </h4>
<p class="textstyle">Free Course</p>
</div>
</div>
what i want do with PHP?
I want to write a PHP code to replace the P and h4 with the course name, cost of courses from my database for each available course.
Please note: the pic for each course it will be from my pc, no need to call the pic from database.
I tried to write PHP code like below:
<div>
<div class="card card-1">
<a href="http://127.0.0.1/project2/course details/course1.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% "></a> <div class="container">
<?php
include_once("db.php");
$result = mysqli_query(OpenCon(), "SELECT Course_Name,cost FROM `course`");
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
while($res = mysqli_fetch_array($result)) {
echo "<p>".$res['Course_Name']."</p>";
echo "<p>".$res['cost']."</p>";
}
?>
</div>
</div>
</div>
This is my result:
It's okay but I want the style to be like the first screenshot. each course should have picture.
After that when the user click on course name. I want move to another page which is content the course details ( for the same course that user clicked ) also it's called for my database
like this:
I hope any one help my to solve this problem only, I should solve this problem within 2 days only. and sorry if my explanation is bad.
Thanks in advance for everyone.
Put the code in a PHP loop.....
So, this
<div class="card card-1">
<a href="http://127.0.0.1/project2/course details/course1.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% ">
</a>
<div class="container">
<h4 class="textstyle"><b>Operating System</b> </h4>
<p class="textstyle">Free Course</p>
</div>
</div>
Becomes (after cleaning up the code a bit - I think you didn't mean to use two <p> in there, but I left them so you can see it. Note that using different lines for the segments makes it a lot easier to see what you have.)
include_once("db.php");
$result = mysqli_query(OpenCon(), "SELECT Course_Name,cost FROM `course`");
$count = 0;
while($res = mysqli_fetch_array($result)) {
$count ++;
// NOTE: Here is the LOOP! - not outside the query, but INSIDE it
// First you 'jump out' of PHP, going back to HTML
?> <!-- now you are in HTML (when you need PHP again, you 'jump in' and 'jump out' as needed - see the code below....) -->
<div class="card card-<?php echo $count;?>">
<a href="http://127.0.0.1/project2/course details/course<?php echo $count;?>.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% ">
</a>
<div class="container">
<h4 class="textstyle">
<b><p><?php echo $res['Course_Name'];?></p></b>
</h4>
<p class="textstyle">
<p><?php echo $res['cost'];?></p>
</p>
</div>
</div>
<?php // we are in PHP again....
}
That should do what you asked for - though I would go a step (well, more than one...) further and make as much of this dynamic as you can.
For this I will presume that:
your database table has a column called 'id' (if it doesn't, you should have) and it relates to the course number (you could make a course number column if they don't match up, but I'm keeping it simple)
you have all your pictures labeled 'coursepicX' where the X is the course number.
We'll use 'coursepic' as a default in case there isn't a picture yet...
Now, the code is more dynamic!
include_once("db.php");
$result = mysqli_query(OpenCon(), "SELECT id,Course_Name,cost FROM `course`");
while($res = mysqli_fetch_array($result)) {
// NOTE: Here is the LOOP! - not outside the query, but INSIDE it
// First you 'jump out' of PHP, going back to HTML
?> <!-- now you are in HTML (when you need PHP again, you 'jump in' and 'jump out' as needed - see the code below....) -->
<div class="card card-<?php echo $res['id']?>">
<a href="http://127.0.0.1/project2/course details/course<?php echo $res['id']?>.php">
<?php
$pic = "http://127.0.0.1/project2/icons/coursepic.jpg";
if(file_exists("http://127.0.0.1/project2/icons/course" . $res['id'] . ".jpg") {
$pic = "http://127.0.0.1/project2/icons/course" . $res['id'] . ".jpg";
}
<img src="<?php echo $pic; ?>" alt="Avatar" style="width:101% ">
</a>
<div class="container">
<h4 class="textstyle">
<b><p><?php echo $res['Course_Name'];?></p></b>
</h4>
<p class="textstyle">
<p><?php echo $res['cost'];?></p>
</p>
</div>
</div>
<?php // we are in PHP again....
}
Note that this is the basic 'shopping cart' sort of program - you will likely use it many (many) times in your career.
Happy Coding!

MYSQL error 500

Issue
I'm currently attempting to create a "cell" for each mysql row similar to how twitter has it, but for some reason i'm logging
PHP Parse error: parse error, expecting `','' or `';''
Code
I'm fairly new to PHP I am unsure of why my code is creating this error.
<?php
while ($row = mysql_fetch_array($rs)) {
$unformatted = $row['mcap_HKD'];
$output = number_format($unformatted);
$time = time_elapsed_string($row['time']);
echo
"<div class="content">
<div class="headers">
<div class="info">
<strong class="scode">".$row['scode']."</strong><br>
<span class="sname">".$row['sname']."</span><br>
<span class="industry">".$row['main_industry']."</span>
</div>
<small class="time">."$time."</small>
</div>
<div class="news">
".$row['title']."
</div>
</div>";
}
I know that through testing that I am able to reach the server and that it will output the data just fine on its own, and even when i output single variables onto one div, but for some reason this is not working when i put extra divs like i do above. I have attempted to echo each line by itself but it still returns the same error.
Issue
PHP use " as a string delimiter (it has several string delimiters). When, in you echo statement, you are not escaping the ", it's as if you stop the string. So PHP is waiting for a string concatenation . or , or the ; end of statement character.
Resolution
You must escape " characters in you echo statement as such: \" ; it should look like:
Escape string delimiters
echo
"<div class=\"content\">
<div class=\"headers\">
<div class=\"info\">
<strong class=\"scode\">".$row['scode']."</strong><br>
<span class=\"sname\">".$row['sname']."</span><br>
<span class=\"industry\">".$row['main_industry']."</span>
</div>
<small class=\"time\">."$time."</small>
</div>
<div class=\"news\">
".$row['title']."
</div>
</div>";
Mix HTML and PHP
or do something like:
<?php while ($row = mysql_fetch_array($rs)): ?>
<?php
$unformatted = $row['mcap_HKD'];
$output = number_format($unformatted);
$time = time_elapsed_string($row['time']);
?>
<div class="content">
<div class="headers">
<div class="info">
<strong class="scode"><?= $row['scode']; ?></strong><br>
<span class="sname"><?= $row['sname']; ?></span><br>
<span class="industry"><?= $row['main_industry']; ?></span>
</div>
<small class="time"><?= $time; ?></small>
</div>
<div class="news">
<?= $row['title']; ?>
</div>
</div>
<?php endwhile; ?>
This has the benefit to not use PHP to print HTML tags, and with the <?= PHP opening tags, it's more readable!
I hope this helps you! :)

PHP Undefined index in foreach loop makes no sense

This error makes no sense. Here is the code block and my explanation below.
<?php foreach($rows as $value): ?>
<?php echo $value['authorname'] . "<br />\n";?>
<?php echo $value['title'] . "<br />\n";?>
<?php echo $value['rating'] . "<br />\n";?>
<?php echo $value['imagelocation'] . "<br />\n";?>
<div class="block">
<div class="row">
<div class="col-md-4 col-md-8">
<div class="widget-block">
<input id="rate1" value="<?php echo $value['rating'];?>" type="number" class="rating" data-max="5" data-min="0" data-size="sm" data-show-clear="false" readOnly="readOnly">
<img class="img-responsive wow fadeInLeftBig animated" data-wow-duration="1.5s" src=<?php echo $value['$imagelocation'];?> alt=<?php echo $value['$authorname'];?>>
<br>
Buy this book
</div>
</div>
<div class="col-md-6 col-md-8">
<div class="section-sub-title">
<article class="section-title-body white">
<h1 class="head-title">Author: <span><?php echo $value['$authorname'];?> -</span> <?php echo $value['$title'];?></h1>
<span class="point-line hidden-xs hidden-sm"></span>
<p>
<?php echo $value['$review'];?>
</p>
</article>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
The echo statements right after the start of the foreach loop prints out each variable fine. It isn't until we get down to the html that there are issues.
This code feeds an array of data from a DB then builds blocks of html code depending on the amount of data. In this case, I am pulling 8 records so this loop creates 8 copies of this code block. The thing that is frustrating is the variable "$rating" injects in all 8 blocks but none of the other variables do even though they print correctly on the page in the echo statements.
Maybe it is the data in the variable? For example as the code is parsed the first variable evaluated is $rating and works. The next one is the src property in the img tag $imagelocation and has an actual value of img\book_covers\TrueConviction.jpg
Are _ and . special characters and causing the issue? My return values would have _ . \ and spaces.
Thanks for any help.
I was right that it was the data IN the variables. The answer was urldecode. This solved my problems. I did this to all my variables.
$cleanauthorname = urldecode($value['authorname']);?>

Unable to Display Images from MySQL

I am unable to display images from MySQL. I've checked path etc. but still there are broken images on site. Below is my Code. Please guide me.
<div id="right_content">
<div id="headline">
<div id="headline_content"> <b>Welcome Guest!</b> <b style="color:#F56013">Shopping Cart</b> <span>-Items: -Price: </span> </div>
</div>
<div id="products_box">
<?php
$get_products ="SELECT * FROM products ORDER BY rand() LIMIT 0,6";
$run_products = mysqli_query($con,$get_products);
while($row_products=mysqli_fetch_array($run_products)){
$prod_id = $row_products['product_ID'];
$prod_title = $row_products['product_title'];
$prod_cat = $row_products['category_ID'];
$prod_brand = $row_products['brand_ID'];
$prod_price = $row_products['product_price'];
$prod_desc = $row_products['product_desc'];
$prod_img =$row_products['product_img1'];
echo "
<div id='single_product'>
<h3>$prod_title</h3>
<img src='admin_area/product_images/$prod_img' width='180' height='180' />
</div>
";
}
?>
</div>
As per comment, problem is solved. The images were not moving into the directory and i didn't check that. When i manually copied them into the specified directory the problem was gone.
When you write html please note that you should always use double and not single quotes for tag properties. So for example
src='admin_area/product_images/$prod_img'
should be
src="admin_area/product_images/$prod_img"
and so on. Since you are doing echo using double quotes you should escape double quotes with .
So your echo should look like:
echo "
<div id=\"single_product\">
<h3>$prod_title</h3>
<img src=\"admin_area/product_images/$prod_img\" width=\"180\" height=\"180\" />
</div>
";
IMHO it is better to write plain html and use php only to echo variables just to avoid this kind of problems with the generated code. So I'd write:
?>
<div id="single_product">
<h3><?php echo $prod_title; ?></h3>
<img src="admin_area/product_images/<?php echo $prod_img; ?>" width=\"180\" height=\"180\" />
</div>
<?php } ?>
This will save you a lot of headache in case of debugging if your code is not working

Advice on cleaner way to write this While statement

So the following code works, it is doing everything i want it to do. However, as i step back it seems like an overly convoluted approach to what is arguably one of the most common tasks in php.
I know enough about php to figure out what most things are doing when i see them, and to create some rather ugly code like you will see below; however, the finer points evade me.
I was hoping that if someone had some free time, he/she could look this over and show me a more concise way to approach this.
<?php
$result = mysql_query('SELECT * FROM events');
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
echo '<div id="item_gallery_s'.$i .'"'. 'class="fluid profileImgWrap goldDiagGrad">' .
'<div class="profile_name">' . $row['name'] . '<br /><span class="profile_date">' .
'<a href="http:#"
target="_blank"
title="some title">' . $row['place'] .
'</a></span></div><!-- DCD Diva Name -->' .
'<a rel="events[events]"
href="#">' .
'<div class="profile_banner">Custom Banner</div><!-- Banner -->' .
'<img src='.'"img/upload/'.$row['icon'].
'"' .
'alt="image description |'.$row['name'].
'"/>' .
'<!-- Photo --></a></div><!-- END #item_gallery_s'.$i .'-->';
$i++;
}?>
The loop itself is fine but you'll find varying opinions on the HTML-in-strings. For the past seven years I've encouraged my team to either use HTML with php tags or we rely on a full templating system:
<?php while ($row = mysql_fetch_assoc($result)): ?>
<div><?= $row['something'] ?></div>
<?php endwhile ?>
Though we have short tags enabled for even cleaner code. The benefit of this is that it's cleaner - less quotes, escaping problems, and IDEs will be able to syntax highlight the html. Most treat the html as string when it's inside quotes.
That's as "concise" as it gets.
You could not use an echo inside the while. And use php short tags.
while ($row = mysql_fetch_assoc($result)) {
?>
<?=$row['place'];?>
<?php
}
?>
Another way to "clean up", would be to use a template engine, but once again that would be just for the HTML part.
{place}
Good coding!
You can clean this up a bit by interspersing actual HTML, rather than simply echoing it:
<?php
$result = mysql_query('SELECT * FROM events');
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
?>
<div id="item_gallery_s<?php echo $i; ?>" class="fluid profileImgWrap goldDiagGrad">
<div class="profile_name">
<?php echo $row['name']; ?>
<br />
<span class="profile_date"><?php echo $row['place']; ?></span>
</div><!-- DCD Diva Name -->
<a rel="events[events]" href="#"><div class="profile_banner">Custom Banner</div><!-- Banner -->
<img src="img/upload/<?php echo $row['icon']; ?>" alt="image description |<?php echo $row['name']; ?>"/><!-- Photo --></a>
</div><!-- END #item_gallery_s<?php echo $i; ?> -->
<?php
$i++;
}?>
Another option, depending on how much work like this you have to do, would be a full-blown template engine such as Smarty.
Here's how I would probably format this code (as a matter of personal style):
<?php
$result = mysql_query('SELECT * FROM events');
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
echo
'<div id="item_gallery_s'.$i.'" class="fluid profileImgWrap goldDiagGrad">
<div class="profile_name">' . $row['name'] . '<br /><span class="profile_date">
<a href="http:#" target="_blank" title="some title">' . $row['place'] .
'</a></span>
</div><!-- DCD Diva Name -->
<a rel="events[events]" href="#">
<div class="profile_banner">Custom Banner</div><!-- Banner -->
<img src="img/upload/' . $row['icon']. '"
alt="image description |' . $row['name']. '"/>
<!-- Photo -->
</a>
</div><!-- END #item_gallery_s'.$i .'-->';
$i++;
}
?>
Try also to use consistent indentation to make it easy to tell what matches up with what. By the way, a <div> (block element) inside an <a> (inline element) is bad form. Did you mean to use a <span>? Learn to use the W3C validator to pick up this stuff.

Categories