我的代码无法从mysql数据库读取

时间:2019-04-15 08:01:34

标签: php mysql

我尝试调试这次没有成功。到目前为止,这是我尝试过的

//import java.time.LocalDate;
//import java.time.format.DateTimeFormatter;

String s = "190415";
DateTimeFormatter dtfIn = DateTimeFormatter.ofPattern("yyMMdd");
DateTimeFormatter dtfOut = DateTimeFormatter.ofPattern("yyyyMMdd");

LocalDate ld = LocalDate.parse(s, dtfIn);
System.out.println("NEW : " + dtfOut.format(ld));

1 个答案:

答案 0 :(得分:2)

此代码将有效并且更安全

<?php
//Connection part
$servername = "server_adress"; //It can be localhost or 127.0.0.1 or some other IP
$username = "XXXXXX"; //Username for DB
$password = "YYYYYY"; //Password for that user
$database = "ZZZZZZ"; //DB name you are connecting to

//Create a new connection
$conn_to_db = new mysqli($servername, $username, $password,$database);

// Check connection
if ($conn_to_db -> connect_error) {
    die("Connection failed: " . $conn_to_db ->connect_error);
}

//Finished connection part

$cid = mysqli_real_escape_string($conn_to_db, $_GET['cid']); //Escapes special characters in a string for use in an SQL statement

$array = array();

if($stmt = $conn_to_db -> ("SELECT * FROM questions WHERE QType = ?")) {
   $stmt -> bind_param("s", $cid);
   $stmt -> execute();
   $stmt -> bind_result($question_from_db);  //Here you can put all variables you are fetching from DB
   while($stmt -> fetch()){
     //Iterate over rows - put your code here to fetch everything you need from DB and put in array
     $array[] = array('question' => $question_from_db);
   }   
   $stmt -> close();
 }
} 

//you can iterate over rows like this
foreach($array as $key => $value) {
 echo $value['question'];
}
?>

要记住的事情:

  • 避免*(从数据库中选择所有内容)和 仅放入数据库中需要的列
  • 使用准备好的语句,这是一种更安全的方法,可保护您免受SQL注入的侵害
  • MySQL已过时,请尽量避免使用(使用mysqli或PDO)
  • 您需要根据上面的代码进行调整!它将无法复制/粘贴。放置数据库连接并从数据库中选择所需的列,并添加从数据库中获取的变量
  • 请记住,还有更多方法可以执行此操作,并且有人可能会提供其他解决方案。
  • 如果您不在生产服务器上,那么最好进行一些错误报告以查看正在发生的错误