PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Cakey
Forum Newbie
Posts: 6 Joined: Thu Mar 26, 2009 7:45 pm
Post
by Cakey » Thu Mar 26, 2009 7:51 pm
I've been googling and I can't figure it out but here's my code maybe you can help me out!
Code: Select all
<?php
if(isset($_SESSION['PoemID']))
{
if($_SESSION['PoemID'] > 0)
{
$dbh = mysql_connect("BLANK.com", "BLEEEP", "BLEEEP") or die("Unable to connect to MySQL.");
$selected = mysql_select_db("BLEEEEEP", $dbh) or die("Could not select Data-Base. Please report error to Web-Master!");
//Here's where I forget the command
$result = mysql_query("SELECT * FROM Poetry") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
if($row['id'] == $_SESSION['PoemID'])
{
echo "<table border = 1 cellpadding = 4 cellspacing = 0 width = 100%>";
echo "<tr><td align = 'left' valign = 'top'>Title: </td><td align = 'left'> " . $row['title'] . " </td></tr>";
echo "<tr><td align = 'left' valign = 'top'>Poem:</td><td align = 'center'> " . $row['poem'] . "</td></tr>";
echo "<tr><td align = 'left' valign = 'top'>Author Note:</td><td align = 'left'> " . $row['meaning'] . "</td></tr>";
echo "</table>";
}
}
mysql_close($dbh);
}
}
?>
Any help? I haven't done PHP in a few years... Thanks for reading!
Last edited by
Cakey on Thu Mar 26, 2009 9:55 pm, edited 1 time in total.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Thu Mar 26, 2009 7:59 pm
What's the problem?
Cakey
Forum Newbie
Posts: 6 Joined: Thu Mar 26, 2009 7:45 pm
Post
by Cakey » Thu Mar 26, 2009 8:01 pm
It's not a problem but an optimization issue thing.
Like the code works but I know there's a faster way to search for an item in a Table using a variable then reading each row. Do you know what I mean?
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Thu Mar 26, 2009 8:39 pm
Use a WHERE in that query.
Code: Select all
SELECT fields FROM table WHERE field = value
Cakey
Forum Newbie
Posts: 6 Joined: Thu Mar 26, 2009 7:45 pm
Post
by Cakey » Thu Mar 26, 2009 8:45 pm
Ah!!!! Thanks! Hahaha the irony in how in my commented line I said "where" hahaha.
Quick question or two:
is it any different when using a variable as the determiner
WHERE id=$varName?
and do I still get the array the same way?
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Thu Mar 26, 2009 8:48 pm
The only thing that matters is the query. Doesn't matter where you put variables as long as the query is valid.
Cakey
Forum Newbie
Posts: 6 Joined: Thu Mar 26, 2009 7:45 pm
Post
by Cakey » Thu Mar 26, 2009 8:58 pm
Code: Select all
if(isset($_SESSION['PoemID']))
{
if($_SESSION['PoemID'] > 0)
{
//Load DB and table same way as above
$result = mysql_query("SELECT `id` FROM `Poetry` WHERE `id`=$_SESSION['PoemID']") or die(mysql_error());
$row = mysql_fetch_array($result);
echo "<span class = 'subTopic'>Poem: " . $row['title'] . " </span><br>";
echo "<span class = 'subContent'><center>";
echo $row['poem'] . "</center><br>";
echo "<span class = 'subTopic'>Author Comments:</span><br>";
echo "<span class = 'subContent'>" . $row['meaning'] . "</span><br><br>";
mysql_close($dbh);
}
}
Now my page is completely blank. Which usually is result of a PHP error haha. Any idea's why this is happening?
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Thu Mar 26, 2009 9:16 pm
Cakey wrote: Now my page is completely blank. Which usually is result of a PHP error haha.
Quite. If you're developing on a test server it's a good idea to have the display_errors INI setting on.
It was working before but isn't now, right? So it must be in something you added or changed. Unfortunately, forum highlighting won't show the problem, but it's
When putting something from an array into a string there are three ways of doing it:
Code: Select all
// 1. Concatenating it
$query = "... WHERE `id`=" . $_SESSION['PoemID'];
// 2. Using {}s
$query = "... WHERE `id`={$_SESSION['PoemID']}";
// 3. No {}s
$query = "... WHERE `id`=$_SESSION[PoemID]";
Look carefully at #3: no apostrophes. Your code is mixing #2 and #3 together which is not valid.
Cakey
Forum Newbie
Posts: 6 Joined: Thu Mar 26, 2009 7:45 pm
Post
by Cakey » Thu Mar 26, 2009 9:35 pm
I got it to work thanks so much!
Here's how to those whom are curious!
Code: Select all
if(isset($_SESSION['PoemID']))
{
if($_SESSION['PoemID'] > 0)
{
//Load and select DB
$result = mysql_query("SELECT `id`,`title`,`poem`,`meaning` FROM `Poetry` WHERE `id`=" . $_SESSION['PoemID']) or die(mysql_error());
$row = mysql_fetch_array($result);
echo "<span class = 'subTopic'>Poem: " . $row['title'] . " </span><br>";
echo "<span class = 'subContent'><center>";
echo $row['poem'] . "</center><br>";
echo "<span class = 'subTopic'>Author Comments:</span><br>";
echo "<span class = 'subContent'>" . $row['meaning'] . "</span><br><br>";
mysql_close($dbh);
}
}
Sadly I'm not on a test-server. I'm just typing it all in and praying on a free host.