php - Select * from the table name returned from $q = intval($_GET['q']);
Get the solution ↓↓↓I may be doing this the hard way, but I have 2 tables, one of them lists categories with an ID given to each, and each individual category table of its own is named the id, so with a GET request, I'm trying to pull the data using Ajax, PHP, and MySQLi. I've got it figured out, except for calling the variable in the table name. I kinda suck at explaining, so let me mumble along with my code.
The category table is laid out in a selection box with the ID value given to the option, and passed along to the 2nd php page, getcat.php?q=cat_name, for example. I bring q into getcat.php using simply.
$q = intval($_GET['q']);
Obviously SELECT * FROM $q would be too easy, so I've been trying this, but am not sure how to parse it into PHP, or really use it.
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','user','pass','maindb');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
This works fine.
But after that I have no clue. I tried this, but no luck.
SET @cname:=intval($_GET['q']);
SET @sql_text = concat('select * from (''%',@cname,'%'') where id = ?');
PREPARE stmt FROM @sql_text;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
I've tried running queries in PhpMyAdmin, but no luck either. Completely stuck. I've tried DELIMITER, pretty much everything I've searched for, but I'll face it, I'm lost.
All help is appreciated. Thank you kindly.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type bool in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.