Getting one row in PDO mysql PHP

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

40 Replies - 2717 Views - Last Post: 30 September 2014 - 06:27 AM Rate Topic: -----

#1 PC-2011   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 07-September 14

Getting one row in PDO mysql PHP

Posted 14 September 2014 - 09:50 AM

So I have been looking around for a way to get a row from a table that has a list of products to display the one I define in my sesion data. So far I have tried about everyway on Stack overflow. Code Below

<!DOCTYPE html>
 <?php
 $pdo = new PDO("mysql:host=localhost:3308;dbname=vei", "Client",'password');
 error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
// Stored session data
$_SESSION['Product']="1";
?>
<head>
<link rel="stylesheet" type="text/css" href="cgstyle.css">
<title>
CC
</title>
</head>
<body>
<div class='headerbar'>
<div class='logo'>
<a href='index.php'>
<img width='40%' src='pics/logo.png'>
</a>
</div>
<div class='bar'>
<ul id="menu-bar">
 <li><a href="index.php">Home</a></li>
<?php
$sql = "SELECT * FROM catbar";
//$dbh = $pdo->query( $sql );
foreach ($pdo->query($sql) as $row)
{
    print "<li><a href='{$row['code']}'>{$row['cat']}</a></li>";
}
?>
</ul>
</div>
</div>
<div class="body">



That all works great but here is the issue

<?php
include"header.php";
$stmt = $pdo->query("SELECT * FROM products"); 
$row=$stmt->fetch($_SESSION['Product']);
?>
<div class="body">
<div class="description">
<?
print {$row[description]};
?>
</div>
<div class="pic">
<?
print "<img width='40%' src='pics/{$row[description]}'>";
?>
</div>
</div>
<?php
include"footer.php";
?>


Here I just need the one product specified in $_SESSION['Product'] to be displayed.

Is This A Good Question/Topic? 0
  • +

Replies To: Getting one row in PDO mysql PHP

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14096
  • View blog
  • Posts: 56,492
  • Joined: 12-June 08

Re: Getting one row in PDO mysql PHP

Posted 14 September 2014 - 10:13 AM

03	$stmt = $pdo->query("SELECT * FROM products"); 

Add a WHERE clause for the item ID or item number.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3795
  • View blog
  • Posts: 13,738
  • Joined: 08-August 08

Re: Getting one row in PDO mysql PHP

Posted 14 September 2014 - 10:38 AM

In addition to the WHERE clause if you're only looking for 1 row then I'd add LIMIT 1 so that the database stops looking after finding one.
Was This Post Helpful? 0
  • +
  • -

#4 PC-2011   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 07-September 14

Re: Getting one row in PDO mysql PHP

Posted 14 September 2014 - 01:08 PM

So I changed it to this
$stmt = $pdo->query("SELECT * FROM products WHERE=ID LIMIT=1"); 
$row=$stmt->fetch($_SESSION['Product']);


but I get an error of
Fatal error: Call to a member function fetch() on a non-object in C:\Webservers\Projects\test.php on line 5
Was This Post Helpful? 0
  • +
  • -

#5 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2542
  • View blog
  • Posts: 10,187
  • Joined: 03-December 12

Re: Getting one row in PDO mysql PHP

Posted 14 September 2014 - 01:16 PM

I have a PDO tutorial link in my signature, it might be wise for you to give it a read.
Was This Post Helpful? 0
  • +
  • -

#6 PC-2011   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 07-September 14

Re: Getting one row in PDO mysql PHP

Posted 14 September 2014 - 02:43 PM

OK I love reading long threads and all like the next user but I am just starting and want to finish this project for a friends before I start spending a ton of time on this right now. Just some quick help would be nice. Thanks
Was This Post Helpful? 0
  • +
  • -

#7 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2542
  • View blog
  • Posts: 10,187
  • Joined: 03-December 12

Re: Getting one row in PDO mysql PHP

Posted 14 September 2014 - 03:03 PM

The problem with "quick help" is you fail to grasp the concepts. So, you are right back were you started when you have the same problem. Is it better to have a semi-working product full of holes; or something that is solid and scalable/ maintainable?
Was This Post Helpful? 2
  • +
  • -

#8 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3795
  • View blog
  • Posts: 13,738
  • Joined: 08-August 08

Re: Getting one row in PDO mysql PHP

Posted 14 September 2014 - 03:15 PM

Your query demonstrates that you haven't grasped a fundamental SQL concept. This is likely a result of "quick" fixes.
Was This Post Helpful? 1
  • +
  • -

#9 Sheepings   User is offline

  • D.I.C Addict

Reputation: 73
  • View blog
  • Posts: 571
  • Joined: 05-December 13

Re: Getting one row in PDO mysql PHP

Posted 14 September 2014 - 03:17 PM

One day there was a user in another forum who was to lazy to read documentation on code i provided. Guess what happens when he comes back asking for more help? I step out of his topic and ignore him. What does he do?

Sits there posting replies to himself and asks himself pointless questions about some thing he doesn't know about because he wouldn't take the time to read a little to understand the code given. That's were quick help gets you.
Was This Post Helpful? 2
  • +
  • -

#10 EdNolan   User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 423
  • Joined: 22-September 13

Re: Getting one row in PDO mysql PHP

Posted 15 September 2014 - 08:36 AM

Here's some quick help. Even though you set the WHERE to equal the ID, you didn't set the ID to anything! Check out the tutorial now for the answer to that riddle!
Was This Post Helpful? 0
  • +
  • -

#11 PC-2011   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 07-September 14

Re: Getting one row in PDO mysql PHP

Posted 26 September 2014 - 01:54 PM

ok after some reseach I have come up with this

<?php
include"header.php";
$stmt;
settype($stmt, "array");
$ps = $pdo->prepare("SELECT * FROM products WHERE 'ID'=".$_SESSION['Product']." LIMIT=1");
$ps->execute(array('stmt'));
 
?>
<div class="body">
<div class="description">
<?

print " ".$stmt['description']." ";

?>
</div>
<div class="pic">
<?php

print "<img width='40%' src='pics/".$stmt['img']."'>" ;

?>
</div>
</div>
<?php
include"footer.php";
?>



But I end up with this error

Notice: Undefined index: img in C:\Webservers\Projects\test.php on line 20
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3795
  • View blog
  • Posts: 13,738
  • Joined: 08-August 08

Re: Getting one row in PDO mysql PHP

Posted 26 September 2014 - 01:59 PM

I can't get past this insanity: $ps = $pdo->prepare("SELECT * FROM products WHERE 'ID'=".$_SESSION['Product']." LIMIT=1");. Read the tutorial on prepared statements!
Was This Post Helpful? 0
  • +
  • -

#13 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2031
  • View blog
  • Posts: 6,202
  • Joined: 15-January 14

Re: Getting one row in PDO mysql PHP

Posted 26 September 2014 - 02:08 PM

These lines are pretty pointless:

$stmt;
settype($stmt, "array");


You could just do $stmt = array();, but even that is unnecessary, because you're not using $stmt for anything, you don't need to declare it there.

Quote

$ps = $pdo->prepare("SELECT * FROM products WHERE 'ID'=".$_SESSION['Product']." LIMIT=1");

That is definitely not the way to use a prepared statement. There is zero point to preparing a statement if you are just going to stick values in there anyway. That defeats the entire purpose. Also, limit does not have an equal sign after it. You also put quotes around "ID", so you're telling it to use the text "ID", not the ID column.

Quote

$ps->execute(array('stmt'));

You're telling PHP to pass the text "stmt" as a value to the PDO query, but the PDO query has no placeholders for values. It seems like you're also not doing any error handling with PDO, or else you would see the error when you try to do that. There are plenty of articles about how to handle errors with PDO, including the manual:

http://php.net/manua...or-handling.php

Moreover, you do not get the results of the query. You're trying to use $stmt like it should have the results of the select query, but you never get the results. You execute the query, but then don't fetch the results.

http://www.php.net/m...ement.fetch.php

This post has been edited by ArtificialSoldier: 26 September 2014 - 02:11 PM

Was This Post Helpful? 1
  • +
  • -

#14 PC-2011   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 07-September 14

Re: Getting one row in PDO mysql PHP

Posted 26 September 2014 - 02:28 PM

so is the
$ps->execute(array('stmt'));
not doing anything?
Was This Post Helpful? 0
  • +
  • -

#15 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2031
  • View blog
  • Posts: 6,202
  • Joined: 15-January 14

Re: Getting one row in PDO mysql PHP

Posted 26 September 2014 - 02:41 PM

That line tells it to execute the query, and use the text "stmt" as the value for the first placeholder in the query. But the query doesn't have any placeholders. This has examples of using prepare to build a query with placeholders, and then execute to send the values to put in those placeholders:

http://www.php.net/m...pdo.prepare.php
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3