/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Empty lookup query

When I run my code I either get an empty query, or I change the code and I get error messages. I can get the post table data and put it into the form. I can get the topic table info for all of my topics. I just can’t get the posttopic lookup table to return anything so I can see if I need to create a checked checkbox or not. Echo statements are used for debugging. The last echo statement is never run due to the empty query.

Echo output:

[code]
Post ID: 5 Topic ID: 1
SELECT topicid FROM posttopic WHERE postid = :id
Post ID: 5 Topic ID: 2
SELECT topicid FROM posttopic WHERE postid = :id
Post ID: 5 Topic ID: 3
SELECT topicid FROM posttopic WHERE postid = :id
Post ID: 5 Topic ID: 4
SELECT topicid FROM posttopic WHERE postid = :id
Post ID: 5 Topic ID: 5
SELECT topicid FROM posttopic WHERE postid = :id
Post ID: 5 Topic ID: 6
SELECT topicid FROM posttopic WHERE postid = :id
[/code]

posttopic table:

[code]
postid topicid
5 3
7 3
13 1
[/code]

PHP/MySQL:

[code]
$id = $_GET[‘id’];

// Grabs data from the post table
try
{
$pdo = new PDO(DSN, DBUSER, DBPASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, DO::ERRMODE_EXCEPTION);
$sql = “SELECT * FROM post WHERE id = :id”;
$stmt = $pdo->prepare($sql);
$stmt->bindParam(‘:id’, $id, PDO::PARAM_STR);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$subject = stripslashes($row[‘subject’]);
$content = stripslashes($row[‘content’]);
} // while
}
catch (PDOExecption $e)
{
$markup->content = “<p>PDO Execption caught.</p>n
<p>Error with database:</p>n
<p>SQL Query: “.$sql.”</p>n
<p>Error: “.$e->getMessage().”</p>n”;
}
// Places the retrieved post data into the form
$markup->content = “<form action = “.$_SERVER[‘SCRIPT_NAME’].” method = post’>n
<p><label for ‘subject’>Subject:</label><br /><input type = ‘text’ name = ‘subject’ value = ‘”.$subject.”‘ /></input>/p>n
<p><label for=’content’>Content:</label><br /><textarea name = ‘content’ rows = ‘5’ cols = ’45’/>”.$content.”</textarea></p>n
<p>Place in categories:<br />n”;
// Grabs all of the topics
try
{
$pdo = new PDO(DSN, DBUSER, DBPASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = $pdo->query(“SELECT id, name FROM topic”);
while($topics = $sql->fetch(PDO::FETCH_ASSOC)){
$topicid = $topics[‘id’];
echo “Post ID: “.$id. ” Topic ID: “.$topicid.”<br />”;
$name = htmlspecialchars($topics[‘name’]);
// Grabs the related topics from the posttopic lookup table
try
{
$pdolu = new PDO(DSN, DBUSER, DBPASS);
$pdolu->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqllu = “SELECT topicid FROM posttopic WHERE postid = :id”;
echo $sqllu.”<br />”;
$stmt = $pdolu->prepare($sqllu);
$stmt->bindParam(‘:id’, $postid, PDO::PARAM_STR);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$lookuptopic = $row[‘topicid’];
echo “Post ID: “.$id. ” Topic ID: “.$topicid.” Lookup Topic: “. $lookuptopic.”<br />”;
if ($topicid = $lookuptopic)
$markup->content .= “<label><input type = ‘checkbox’ name = ‘topics[]’ value = ‘”.$topicid.”‘ checked />”.$name.”</label><br />n”;
else
$markup->content .= “<label><input type = ‘checkbox’ name = ‘topics[]’ value = ‘”.$topicid.”‘ />”.$name.”</label><br />n”;
} // while
}
catch (PDOExecption $e)
{
$markup->content = “<p>PDO Execption caught.</p>n
<p>Error with database:</p>n
<p>SQL Query: “.$sql.”</p>n
<p>Error: “.$e->getMessage().”</p>n”;
}
} // while
}
[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 31.2011 — Where to you set the [B]$postid[/B] value?

PS: You should be able to do all that with one PDO connection/object -- nothing to do with your problem, but it would be a bit more efficient. ?
Copy linkTweet thisAlerts:
@spufiauthorDec 31.2011 — And this is where taking a break from coding hurt me. I was still confused on the why the corrections that I put in worked until I looked further up in my code and another SQL statement made it clear what I needed to do and it came back to me. My if statement was wrong as well.

I went and started to clean out the multiple connections and such. I guess I hadn't thought of it and hadn't gone through my code to do a polish to it just yet. Like you said, it's not a huge issue, but it does make the code cleaner and run better.

This should be the working code where the heart of the changes happened.
<i>
</i>try
{
$sql = $pdo-&gt;query("SELECT id, name FROM topic");
while($topics = $sql-&gt;fetch(PDO::FETCH_ASSOC)){
$topicid = $topics['id'];
$name = htmlspecialchars($topics['name']);
// Grabs the related topics from the posttopic lookup table
try
{
$sqllu = "SELECT topicid FROM posttopic WHERE postid = :id";
$stmt = $pdo-&gt;prepare($sqllu);
$stmt-&gt;bindParam(':id', $id, PDO::PARAM_STR);
$stmt-&gt;execute();
while($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) {
$lookuptopic = $row['topicid'];
if ($topicid == $lookuptopic)
$markup-&gt;content .= "&lt;label&gt;&lt;input type = 'checkbox' name = 'topics[]' value = '".$topicid."' checked /&gt;".$name."&lt;/label&gt;&lt;br /&gt;n";
else
$markup-&gt;content .= "&lt;label&gt;&lt;input type = 'checkbox' name = 'topics[]' value = '".$topicid."' /&gt;".$name."&lt;/label&gt;&lt;br /&gt;n";
} // while
}
catch (PDOExecption $e)
{
$markup-&gt;content = "&lt;p&gt;PDO Execption caught.&lt;/p&gt;n
&lt;p&gt;Error with database:&lt;/p&gt;n
&lt;p&gt;SQL Query: ".$sql."&lt;/p&gt;n
&lt;p&gt;Error: ".$e-&gt;getMessage()."&lt;/p&gt;n";
}
} // while
}
×

Success!

Help @spufi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...