我知道如何进行设置查询显示,但我需要输入,运行语句并显示结果。这就是我所拥有的:
HTML:
<form name="form3" method="post" action="sqlexp.php">
<input name="sqlstatement" type="text" id="sqlstatement" style="width: 340px;">
<input type="submit" name="create" value="Create">
</form>
PHP:
ob_start();
$host = "localhost"; // Host name
$username = "root"; // Mysql username
$password = "Passw0rd"; // Mysql password
$db_name = "time_tracker"; // Database name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name") or die("cannot select DB");
// Define $sqlstatement
$sqlstatement = $_POST['sqlstatement'];
// runs statement
$sql = " $sqlstatement ";
$result = mysql_query($sql);
echo $sql;
echo $result;
ob_end_flush();
我也想让这些陈述只读,所以没有人可以搞砸我的桌子。 我对此有点新意,我可能需要一些额外的解释
答案 0 :(得分:0)
Hello亲爱的,试试这段代码吧。我希望它对你有用
<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="Passw0rd"; // Mysql password
$db_name="time_tracker"; // Database name
// Connect to server and select databse.
mysql_connect($host, $username,"")or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
// Define $sqlstatement
if(isset($_POST['create']))
{
$sqlstatement=$_POST['sqlstatement'];
echo $sqlstatement;
}
ob_end_flush();
?>
<form name="form3" method="post" action="">
<input name="sqlstatement" type="text" value="Test" style="width: 340px;">
<input type="submit" name="create" value="Create">
</form>
答案 1 :(得分:0)
要检索列名和结果集,请查看此SO问题:How to get the columns names along with resultset in php/mysql?
具体来说,此代码段应该可以帮助您打印出结果:
// Print the column names as the headers of a table
echo "<table><tr>";
for($i = 0; $i < mysql_num_fields($result); $i++) {
$field_info = mysql_fetch_field($result, $i);
echo "<th>{$field_info->name}</th>";
}
// Print the data
while($row = mysql_fetch_row($result)) {
echo "<tr>";
foreach($row as $_column) {
echo "<td>{$_column}</td>";
}
echo "</tr>";
}
echo "</table>";
现在,那说,在继续之前请非常非常小心。看起来你对PHP不熟悉,你正在尝试的是非常危险的,因为它打开了一个经典的SQL注射漏洞:https://xkcd.com/327/
您可以通过将您登录的用户的数据库权限限制为只读($ username和$ password)来限制损坏的可能性。不要以root身份登录 - 永远不要!
如果此表单仅供管理员使用,为什么不给他们一个真正的SQL IDE,如Toad或SQL Server Management Studio?
答案 2 :(得分:0)
首先,你应该永远不要通过表单输入传递SQL语句,这是非常不安全的。第二个使用新的mysqli_*
或pdo。
相反,您可以拥有一个表单,使您可以执行某些操作,例如创建联系人。
您可以使用一个简单的表单:
<form method="post" action="link/to/script.php">
<input type="text" name="first_name"/>
<input type="text" name="last_name"/>
<input type="email" name="email"/>
<input type="submit" name="create_contact"/>
</form>
现在用于创建联系人的PHP脚本:
if(isset($_POST['create_contact'])){ // if submit is set, then create contact
$firstname=$_POST['first_name'];
$lastname=$_POST['last_name'];
$email=$_POST['email'];
$sql="INSERT INTO contacts (firstname, lastname, email) VALUES (?, ?, ?)"; //query here. The `?` are placeholders
$stmt = $connection->prepare($sql); //prepare query
$stmt->bind_param("sss", $firstname, $lastname, $email); // In the first argument, `s` stands for string, there is also `i` for integer
// Set paramters
$stmt->execute(); //Execute the query
}
现在获取数据库中的联系人列表(数组)并输出它们。
$sql="SELECT * FROM cotacts"; // SQL statement
$result=mysqli_query($con,$sql); //Do query
$row=mysqli_fetch_array($result,MYSQLI_ASSOC); //Fetch array
//Output array
foreach($row as $contact){
echo $contact['firstname'];
echo $contact['lastname'];
echo $contact['email'];
}
绝不允许用户或任何人运行他们将自己键入的SQL。
答案 3 :(得分:0)
$query = "SELECT type, name FROM something WHERE user = :user AND privlages = 2 ";
$query_params = array(':user' => $user );
try{$stmt = $db->prepare($query); $yourArgs= $stmt->execute($query_params);}
catch(PDOException $ex){}
$stmt->execute();
$yourArgs = $stmt->fetchAll();
//call array wherever
foreach($yourArgs as $key => $row){
$type = $row['type'];
$name = $row['name'];
}
顺便创建一个PDO连接
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', PDO::ATTR_PERSISTENT => true);
try {$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);}
catch(PDOException $ex) {die("Failed to connect to the database: " . $ex->getMessage());}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);