Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8805479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:52:42+00:00 2026-06-14T01:52:42+00:00

I took my old mysql script and attempted to convert it to a more

  • 0

I took my old mysql script and attempted to convert it to a more secure PDO format. In my old script I ran a SELECT field FROM table statement then put it into a variable
$Product = $row[‘product’]; and run an IF and ELSE statement. If the users product = sony I wanted to echo ” you may be interested in these as well” Here is my example code below

<?php
$connect = mysql_connect("host","username","password");
mysql_select_db("dbname");

$username = $_SESSION['username'];

$q = "SELECT product FROM users WHERE username = ('$username')";

$r       = mysql_query($q);
$row     = mysql_fetch_assoc($r);
$Product = $row['product'];

if($Product == "sony")
{
  echo "You may be interested in these as well"; 
}
?>

Now with the PDO-Statement, it seems you can not run the same sort of statement as before as easiliy. I tried assigning a variable to the row product that my query is selecting from but I get the error:

Cannot use object of type PDOStatement as array

Here is my code

<?php
session_start();

$db = new PDO('mysql:host=host;dbname=dbname;charset=UTF-8', 'username', 'password');

$username = $_SESSION['username'];
$getProduct = $db->query("SELECT product FROM users  WHERE username = '$username'");
$getProduct->execute();

$Product = $getProduct['product'];

if($Product == "sony")
{
  echo "You may be interested in these as well"; 
}

?>

How can I pass the field “product” from the table I am selecting from to a variable with PDO and IF the product =”sony” echo “message”; ?

I also tried replacing the statements with this too with no success.

<?php
$query = $db->query("SELECT product FROM users  WHERE username = '$username'");
$query->execute();

$Product = $query ->fetchAll(PDO::FETCH_ASSOC);

$Product = $getProduct['product'];

if($Product == "sony")
{
    echo "You may be interested in these as well"; 
}

?>

The manual is not very friendly in regards to converting old statements to newer and secure ones, so I appreciate any direction on this, thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:52:44+00:00Added an answer on June 14, 2026 at 1:52 am

    use

    $query->fetch(PDO::FETCH_ASSOC);
    

    to get 1 row and

    $query->fetchAll(PDO::FETCH_ASSOC);
    

    to get all rows

    You are expecting one row so use the first one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I took over some old php application with MySQL as database. Inside the database,
I took over an old HTML based site with all hard coded links, no
I took the rendered page from the SWT Browser and exported it to an
I took the example code from the Kendo UI demos at http://demos.kendoui.com/web/grid/remote-data.html , binding
I took the facebook example but it does use the old versions of libraries
I have this old C++ COM component. I took the latest code base, built
i'm working with an old 1.1 site that uses the depreciated MailMessage class from
Hello everyone I am learning functions so I took a old practice program and
I imported about 60 posts from my old blogger blog into my new wordpress
I took an existing MySQL database, and set up a copy on a new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.