How to dynamically generate table names in SQL queries?
P粉588152636
P粉588152636 2023-09-03 19:57:51
0
1
525
<p>I have a SQL query that uses a MySQL database to get information from it. The table name is the first constant with a different IMEI number at the end. </p> <p>gs_object_data_12345678</p> <p>12345678 is identified in my script as <code>$_GET</code> but when I try to change the table name it does not display the data from that table</p> <pre class="brush:php;toolbar:false;">$imei = $_GET['imei']; $result = $db->query("SELECT * FROM gs_object_data_'$imei' LIMIT 200 ");</pre> <p>If I use the following string it works</p> <pre class="brush:php;toolbar:false;">$result = $db->query("SELECT * FROM gs_object_data_12345678 LIMIT 200 ");</pre> <p>I need to change the table name with <code>$_GET</code>information</p> The <p>URL <code>alarm_action.php?id=58543®=****&imei=862202228007105</code> contains the IMEI, so the information should pass through </p>
P粉588152636
P粉588152636

reply all(1)
P粉232793765

Use like this

$imei = $_GET['imei'];
$result = $db->query("SELECT * FROM gs_object_data_" . $imei . " LIMIT 200 ");

or

$result = $db->query("SELECT * FROM gs_object_data_{$imei} LIMIT 200 ");

In a protected manner

$imei = $_GET['imei'];

$stmt = $db->prepare("SELECT * FROM gs_object_data_? LIMIT 200");
$stmt->bind_param("s", $imei);
$stmt->execute();

$result = $stmt->get_result();
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template