What is the issue with this particular PDO?

So here's the issue - all other PDO queries are working fine, but this one is giving me trouble. I've tried using

execute(array(':t'=>$table));

but no luck. Any ideas?

public function __construct($table){
    try{
        $pdocnx = new PDO("mysql:host=localhost;dbname=sigcat",'root','');
        $stmt = $pdocnx->prepare('select * from sigcat.:t');
        $stmt->bindParam(':t', urldecode($table), PDO::PARAM_STR,45);
        $stmt->execute();
        $row = $stmt->fetchAll(PDO::FETCH_ASSOC);
        var_dump($row);
    }catch(Exception $e){
        echo $e->getMessage();
    }   
}

I have a lot of records in 'supplies', but when I run the query it returns array(0) { }. I'm fetching the 'table' parameter using $_GET['table']. No exceptions are being thrown though.

Answer №1

Table names cannot be bound, only values can.

It is important to keep track of a list of valid table names and make sure that the string is included in this list.

If you are unable to create a list of valid table names, there may be an issue with your approach.

Answer №2

If you need to work with tables dynamically, consider using a clever workaround such as the following:

function dynamicTable($tableName){
    $sql = "SELECT * FROM `" . $tableName ."` ..some sql";
    $statement->prepare($sql);
    $statement->execute();
} 

Feel free to utilize this method in your projects.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Get notified via email when submitting a form on a PHP contact form

I am a front-end developer and do not have experience with PHP. I am trying to set up a contact form on my website using an example from htmldog. Here is the current code snippet: <form action="contact.php" method="post"> <div class="c ...

Transfer image data in JSON format by compressing it into a base64 string from an Android device to a PHP web

I am attempting to upload an image (compressed in base64) as a JSONObject from an Android device to a web service using PHP. I have tried using HttpURLConnection with the POST method. Here is my code in Android: try { JSONObject params = new ...

JavaScript code for sending information to the server

I am attempting to write data to a file on the server when a user clicks on a button, using JavaScript. I followed the method outlined in this Stack Overflow post: Saving a text file on server using JavaScript. Unfortunately, it did not work as expected. ...

Determine the domain for a POST method request

Is there a way to determine the domain of another website when receiving a POST request from it? For example, if helloworld.com sends a POST request to mywebsite.com, how can mywebsite.com identify that the request is coming from helloworld.com? I have a ...

PHP Adding Values to a Nested Array

I am encountering some difficulties with retrieving the output from an array. Despite my efforts, I have not been able to determine why each portion of the array is not being assigned a proper index. I even attempted to introduce a counter variable in hope ...

Ways to access the data from a json response

I'm dealing with this JSON data: Print_r($send); {"status":"INSUFFICIENT_APIBALANCE"} and I also have: $send = var_dump(json_decode($send)); Now, I'm attempting to check for a specific condition: if ($send->status == "INSUFFICIENT ...

DeactivateGLOBALS() in WordPress

Here is a code snippet from Wordpress that I am having trouble understanding: /** * Function to disable register globals. * * @access private * @since 2.1.0 * @return null Will return null if register_globals PHP directive was disabled */ function w ...

Using PHP CURL to manually define the content-length header

Imagine I'm using PHP with CURL to upload a file: $postData = array(); $postData['file_name'] = "test.txt"; $postData['submit'] = "UPLOAD"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url ); curl_setopt($ch, CURLOPT_RETUR ...

Retrieve the GET parameters from a URL string

Is there a simple way to extract GET variables passed through a URL in PHP? The URL is not for the actual page itself. For example, if I have a string like: What is the most effective method to retrieve the values of those variables? ...

Troubleshooting JSON parsing issues in PHP

In reference to the discussion at: , where suggestions mainly revolved around JavaScript. Is there a specific function in PHP that can be utilized to address the issue described below and ensure that the output is valid JSON? Please note that using front ...

Attempting to extract only the text content from a specific div using XPath

I am currently facing a challenge in writing a script to extract the title element from a poorly coded webpage. The developer who created this website did not use any classes, only div elements. Below is a snippet of the source code I am trying to scrape: ...

Achieving a doubled value in PHP

My intention is to add two numbers together and have the result displayed as a double. <?php $a=4.0; $b=4; echo $a+$b ?> The expected output should be 8.0, but it currently displays as 8. I am still learning PHP. ...

The table is not reloading via table.ajax.reload() function after a successful operation

Introduction Currently, I am working on implementing datatables.net with jQuery for server-side processing using JSON, AJAX, and PHP. My issue lies in the fact that even though I am able to delete a row from the database upon clicking the row button, the ...

Implementing file uploads with Bootstrap, jQuery, and Laravel

Looking to incorporate the blueimp jquery file upload feature into my Laravel app. Check it out here: https://github.com/blueimp/jQuery-File-Upload The form is set up and working properly with the plugin, but facing issues with creating server-side script ...

Configuring an exception for api.php in the .htaccess file

Since transitioning my server front end and back-end to use the Symfony framework, I have encountered an issue with accessing my API file over HTTP. Even with exceptions written in my .htaccess file, I am unable to retrieve data from my NodeMCU (such as te ...

The login form constantly displaying the message "Incorrect Login Information"

<?php include 'header.php'; error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); $con = mysql_connect("localhost","root",""); $db_selected = mysql_select_db("layout",$con); $name = $_POST['name']; $password = $_POST['pass ...

Refresh the table every couple of seconds

I need to regularly update a table every two to three seconds or in real-time if possible. The current method I tried caused the table to flash constantly, making it difficult to read and straining on the eyes. Would jQuery and Ajax solve this issue? How c ...

Guide to displaying a custom HTML tag

In this example: <?php $html='<p style="color:#eee;"> Test content 1. </p> <p dir="LTR" style="color:#f00;"> <span>Test content 2.</span> </p> <p style="color:#000;"> Test content 3. </p>' ?> ...

sending an array from Javascript to PHP

Utilizing JavaScript, I have successfully converted data from a file into an array. Now, my goal is to utilize input from a form to search through this array using PHP and present the results in a table. Despite browsing various solutions for similar probl ...

Encountering the error message "undefined is not a function" while attempting to retrieve JSON data

I'm running into some issues with retrieving data from a PHP file. Upon calling the variable msg[0}, I'm getting an undefined error even though it should have been filled in during the JSON request. The JavaScript code is provided below, and you ...