Another PDO problem

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

77 Replies - 2018 Views - Last Post: 07 December 2013 - 04:03 AM Rate Topic: ***-- 2 Votes

#1 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 897
  • Joined: 06-July 13

Another PDO problem

Posted 30 October 2013 - 03:45 AM

I have yet another problem.I am trying to combine all of my site download areas into one, and this won't work.It just echos nothing.

<?php
$ps = $pdo->query("SELECT site_title FROM download_sites");
		$ps->execute();		
		foreach ($ps as $row)
		{
		$row['site_title'] = $site_title;
echo '<td class="upload"><a href="index.php?action=display&site='.$site_title.'"><b>'.$site_title.'</b></a></td>';
}
?>



Go to http://downloads.str...o.uk/index2.php for a live demo.

Is This A Good Question/Topic? 0
  • +

Replies To: Another PDO problem

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Another PDO problem

Posted 30 October 2013 - 03:52 AM

you’ve got the assignment reversed. though I would recommend to change the code a bit anyways:
$ps = $pdo->query("SELECT site_title FROM download_sites");
// since you only want one value, tell PDO that
$ps->setFetchMode(PDO::FETCH_COLUMN, 0);
// set HTML template
$cell = <<<TD
<td class="upload">
  <!-- use CSS for bold -->
  <a href="index.php?action=display&site=%s">%s</a>
</td>
TD;
foreach ($ps as $title)
{
  // no need for repetition
  printf($cell, urlencode($title), htmlentities($title));
}


This post has been edited by Dormilich: 30 October 2013 - 03:53 AM

Was This Post Helpful? 1
  • +
  • -

#3 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 897
  • Joined: 06-July 13

Re: Another PDO problem

Posted 30 October 2013 - 03:55 AM

Would this bring out multiple results?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Another PDO problem

Posted 30 October 2013 - 03:58 AM

one per row.
Was This Post Helpful? 0
  • +
  • -

#5 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 897
  • Joined: 06-July 13

Re: Another PDO problem

Posted 30 October 2013 - 04:02 AM

error said:

Warning: printf() [function.printf]: Too few arguments in /home/*directories*/index2.php on line 138


Does this mean that there is still nothing there?
Was This Post Helpful? 0
  • +
  • -

#6 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 897
  • Joined: 06-July 13

Re: Another PDO problem

Posted 30 October 2013 - 04:11 AM

I have managed to get it.

But what does the %s mean in your code?
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Another PDO problem

Posted 30 October 2013 - 05:28 AM

see printf format definition.
Was This Post Helpful? 0
  • +
  • -

#8 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 897
  • Joined: 06-July 13

Re: Another PDO problem

Posted 30 October 2013 - 06:28 AM

How could I get a WHERE statement?

I have tried this, but it brings up "unexpected T_VARIABLE".


$site = isset($_GET['site']) ? $_GET['site'] : null;
$ps = $pdo->query("SELECT category FROM download_categories WHERE site = :site");
$ps->bindValue(1, $_GET['site']);
// since you only want one value, tell PDO that
$ps->setFetchMode(PDO::FETCH_COLUMN, 0);
// set HTML template
$cell = <<<TD
<td class="upload">
  <a href="lister.php?category=%s&site=$site">%s</a>
</td>
TD;
foreach ($ps as $title)
{
  // no need for repetition
  printf($cell, urlencode($title), htmlentities($title));
}


Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Another PDO problem

Posted 30 October 2013 - 06:37 AM

if you want a WHERE clause, you need to use prepare(), not query().

and then you need to decide to use either named or question mark placeholders.

PS. printf() is made so that you do NOT use variable replacement inside the template string.
Was This Post Helpful? 1
  • +
  • -

#10 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 897
  • Joined: 06-July 13

Re: Another PDO problem

Posted 30 October 2013 - 06:43 AM

I changed it to this, but this error now occurs.

Error said:

unexpected T_CONSTANT_ENCAPSED_STRING


$ps = $pdo->prepare("SELECT category FROM download_categories WHERE site = ?");
$ps->bindValue(1, $_GET['site']);
foreach ($ps as $row)
{
echo '
<td class="upload">
  <a href="lister.php?category='.$row['category'].'&site='.$site.'">'.$row['category']'.</a>
</td>';
}


This post has been edited by chris98: 30 October 2013 - 06:47 AM

Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Another PDO problem

Posted 30 October 2013 - 07:03 AM

on which line?

One good thing about printf(), it doesn’t have these issues.
Was This Post Helpful? 0
  • +
  • -

#12 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 897
  • Joined: 06-July 13

Re: Another PDO problem

Posted 30 October 2013 - 07:08 AM

Line 210 in the page coding, line 8 of the code above.

</td>';


Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Another PDO problem

Posted 30 October 2013 - 07:13 AM

look at the line before, 4 letters from the EOL.
Was This Post Helpful? 0
  • +
  • -

#14 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 897
  • Joined: 06-July 13

Re: Another PDO problem

Posted 30 October 2013 - 07:19 AM

Thanks.Rather than this topic be called "Another PDO problem", it should really be called "Another stupid mistake".

But now I have done that it says that there is an unknown error on line 205.

foreach ($ps as $row)



Error said:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[]: <<Unknown error>>' in /home/a4971657/public_html/downloads/index2.php:205 Stack trace: #0 /home/a4971657/public_html/downloads/index2.php(205): unknown() #1 {main} thrown in /home/*directories*/index2.php on line 205

This post has been edited by chris98: 30 October 2013 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Another PDO problem

Posted 30 October 2013 - 10:01 AM

what is the code that belong to that message?


Quote

Rather than this topic be called "Another PDO problem", it should really be called "Another stupid mistake".

shall I update the title?
Was This Post Helpful? 0
  • +
  • -

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »