Jump to content

exceptions with prepare


Go to solution Solved by Barand,

Recommended Posts

I'd love to use anonymous placeholders on my ecommerce site project.  I am writing half with php and half with golang.  On the three examples below, when run, gives the following exception, " Error: Call to a member function execute() on string. "  I tried it with a decimal too.  Thanks in advance.
 

$stmt = $dbo->prepare = ("SELECT * FROM products WHERE ProductName = ?");
//this one calls exception
$stmt->execute();

$stmt = $dbo->prepare = ("SELECT * FROM products WHERE ProductName = ?");
//this one calls exception
$stmt->bindParam(1, $productID, PDO::PARAM_INT);
$stmt->execute();

$stmt = $dbo->prepare = ("SELECT * FROM products WHERE ProductName = ?");
//this one calls exception
$stmt->bindValue(1, $productID, PDO::PARAM_INT);
$stmt->execute();

Here is the rest of the code :

<?php
$filename = "";
$keyword1 = $_GET['keyword'];
$titleOfSelectedDropDown = $_GET['val1'];
$fileID = "";
$imageID = "a";
$displayID = "";
$keyword1 = "test";
$titleOfSelectedDropDown = "cc";

$host = 'localhost';
$user = 'root';
$pass = '';
$database = 'ecommerce';

$options = array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => false
);

$gKeyword1 = "";
$gKeyword2 = "";
$gKeyword3 = "";

$key1ID = "";
$key2ID = "";
$key3ID = "";

$string1 = "<center><h1><u>Search Results</u><h1></center></p>";

$dbo = new PDO("mysql:host=$host;dbname=$database", $user, $pass, $options);

$stmt = $dbo->prepare = ("SELECT * FROM products WHERE ProductName = ?");
$test = "1";
$stmt->execute([ $test ]);

 

Edited by JoshEir
mistake
Link to post
Share on other sites
  • Solution
Posted (edited)

Too many "="s.

$stmt = $dbo->prepare = ("SELECT * FROM products WHERE ProductName = ?");
                      ^
                    REMOVE

EDIT: Turn on your error reporting.

Edited by Barand
Link to post
Share on other sites
Posted (edited)

Thank you, I would never have suspected that!  Hence, this error reporting is important, is it an extension?  I'm using Visual Code.

Josh

Edited by JoshEir
Link to post
Share on other sites
4 minutes ago, JoshEir said:

this error reporting is important, is it an extension?

The settings are in you "php.ini" file. I have highlighted the settings you want for development.

On a production site you would log errors instead of displaying them.

image.png.54e736746162a574d0ee1036e49c8038.png

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.