How to input the echo value to complete the query? [on hold]
How to input the echo value to complete the query? [on hold]
Help pls. I just want to complete the query but I don't know how to put the echo value show tables from (echo value which is the database).
show tables from (echo value which is the database)
<?php
if (isset($_POST['database'])){
echo $_POST['database'];
$qry = "show tables from ";
$res = mysqli_query($link,$qry);
while ($row = mysqli_fetch_array($res)){
echo $row[0]."<br>";
}
}
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1 Answer
1
you can easily complete your query with input values see below code :
<?php
if (isset($_POST['database'])){
echo $_POST['database'];
$qry = "show tables from " . $_POST['database'];
$res = mysqli_query($link,$qry);
while ($row = mysqli_fetch_array($res)){
echo $row[0]."<br>";
}
}
but take a look at sql injections hack if this function is internal function its ok but if you get your database name from an external client you should complete your query with something like below codes :
in PDO :
$query = "SELECT * FROM test_table WHERE enabled = 1 AND id = :id";
$stmnt = $db->prepare($query);
$stmnt->execute(array(':id' => $_POST['id']));
its not working sir.
– Newbee
Jun 30 at 6:32
could you echo your query ? using this --> die(json_encode($qry)); and send me the result
– Mhmd
Jun 30 at 6:42
yes it says "show tables from (database) I think the problem is the other quotation mark is in the next row of the page.
– Newbee
Jun 30 at 6:48
oh yeah i get it. cuz i use <br> on my first code.. thanks.
– Newbee
Jun 30 at 6:51
ok edit your while echo to this and send the result : echo json_encode($row)."<br>";
– Mhmd
Jun 30 at 6:51
When you echo $row inside the while loop, you use the column name as the array index, not a number, eg $row["column_name"]. See php.net/manual/en/function.mysql-fetch-array.php for some examples
– Stephen
Jun 30 at 6:22