homepage Welcome to WebmasterWorld Guest from 54.205.254.108
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Simple SQL PHP help :(
aaron1259




msg:4441785
 4:36 am on Apr 17, 2012 (gmt 0)

Oh so I have a table called "top_topsites".

In that table I have "in" and "redeemed"

I need a PHP script that checks if "in" is => 20 if it is it will show an image. If that image is clicked redeemed will be set to "1" and the image will now longer show. Heres what I have so far:


<?php
$base = mysql_query("SELECT * FROM top_topsites LIMIT 2");
$in = mysql_fetch_array($base);

if ($base['in'] => 20) then
if ($base['redeemed'] == 1 then
<!-- Continue to load without image -->
else
<!-- Load image then continue -->
else
<!-- Do nothing and load -->
Endif

<!-- Web Content -->

?>


Does this look right? I haven't tried it yet, but it seems okay thanks!

 

topr8




msg:4441879
 10:12 am on Apr 17, 2012 (gmt 0)

you know that
IN is a reserved word in mySQL, it looks like you are using it as a column name, this is probably not a good start!
rocknbil




msg:4442045
 4:25 pm on Apr 17, 2012 (gmt 0)

Yeah if you use reserved words you will have to backtick selections on those columns. Which is a safe thing to do anyway. But no, it doesn't "look" correct. Here's why.

You execute a query, that is stored in a object (resource) (correct)

$base = mysql_query("SELECT * FROM top_topsites LIMIT 2");

You execute a fetch on the object of the rows which gives you an array of the row data stored in "$in".(correct, sorta, but not really)

$in = mysql_fetch_array($base);

This returns a single row result which you're storing in "$in". You are expecting two rows by the limit clause. Right? So this will give you only the last row found in the result set. You need to wrap this in a while statement to get at all rows.

while ($in = mysql_fetch_array($base)) {
echo "<p> . $row['id'] . "</p>" // for example, you may not even have a column "id"
}

Next is where you really get sideways, and in two points. :-) I've never seen if/then in PHP, and just did a search to see and still can't find any evidence of it. What you have here is Basic/Visual Basic/VbScript/ASP style programming, a cold, empty and frustrating language. :-)


if ($base['in'] => 20) then
if ($base['redeemed'] == 1 then
<!-- Continue to load without image -->
else
<!-- Load image then continue -->
else
<!-- Do nothing and load -->
Endif

Perl and PHP have clearly delimited code blocks using curly braces and no if/then. If, else, and "else if" control blocks are the same, though switches are slightly different. Comments, as you have them there, will kick errors unless you echo or print them to STDOUT.

The second mistake is you're referencing the $base object, not the row you've stored in $in (pretty common mistake.)

if ($base['in'] => 20) <---- No
if ($in['in'] => 20) <--- yes, presuming you have a column in the database named "in"

So here's today's freebie, all of the above together. :-)

<?php
$base = mysql_query("SELECT * FROM top_topsites LIMIT 2");
while ($in = mysql_fetch_array($base)) {
if ($in['in'] => 20) {
if ($in['redeemed'] == 1) {
echo "<!-- Continue to load without image -->\n";
}
else {
echo "<!-- Load image then continue -->\n";
}
}
else {
echo "<!-- Do nothing and load -->\n";
}
}
echo "<!-- Web Content -->\n";
?>


In looking at your comments, - "Do nothing and load" - if you're really doing NOTHING there, you don't need that code block. That is, PHP executes top to bottom, and unless you tell it to exit() it will continue until it reaches the bottom. The point being, if this is the case, the last else block can be eliminated.


<?php
$base = mysql_query("SELECT * FROM top_topsites LIMIT 2");
while ($in = mysql_fetch_array($base)) {
if ($in['in'] => 20) {
if ($in['redeemed'] == 1) {
echo "<!-- Continue to load without image -->\n";
}
else {
echo "<!-- Load image then continue -->\n";
}
}
}
echo "<!-- Web Content, and anything not captured in the if's above-->\n";
?>


Another comment for an obvious newbie, this is pretty huge, and often mistaken. When you echo or print (these are pretty much synonymous, except that echo only "prints" to STDOUT and print can print to any file handle) you should understand the relevance of quoting to printing (or storing in other variables.) Double quotes, as posted above, allow scalar variables to interpolate - that is, they will print out the value contained in them. If you use SINGLE quotes, it will print a literal, with the dollar sign:

$var = 'hello world'; // Store "hello world" in $var
echo "I said $var"; //prints "I said hello world"
echo 'I said $var'; prints "I said $var"

Lastly, nothing here is copy and paste code. It's all typed on the fly for educational purposes and very well may contain errors.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved