我通过PHP中的PDO连接到SQL Server数据库.我的问题是为什么每次while循环运行时都不能将参数绑定到命名的位置标记?这是代码:
public function search($search, $field)
{
$c = new PDO("sqlsrv:Server=localhost;Database=$this->_db", $this->_user, $this->_pass);
$sql = "
SELECT Title
FROM Table WHERE 1=1";
//form array of search terms stored in serachArray variable
$searchArray = explode(' ', $search );
//count objects in array
$num_search_terms = count( $searchArray );
$i = 0;//for search term array (starts at 0)
$x = 1;//for parameter incrementin (starts at 1)
while( $i <= ( $num_search_terms -1 ) )
{
$sql .= "
AND $field LIKE :s".$x;
$stmt …Run Code Online (Sandbox Code Playgroud) 我一直在学习使用PDO和PHP的基础知识,我的查询工作正常.这是代码:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
$hostname = "localhost";
$username = "test";
$password = "test";
try {
$db = new PDO("mysql:host=$hostname;dbname=goodsdb", $username, $password);
//echo "Connected to database"; // check for connection
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$sql = "Select * from items";
$result = $db->query($sql);
foreach ($result as $row) {
//echo $row["goods"]. "<br />";
}
$db = null; // close the database connection
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
<p>
<?php
echo $row["goods"];
?>
</p>
<p>
<?php
echo $row["auctiondate"];
?> …Run Code Online (Sandbox Code Playgroud) 下面的代码现在可以工作了,但是如果没有找到结果,我该如何制作它,它会回显一条消息而不是空白。
我想我已经设法为我的数据库创建了一个搜索查询。它只是一个非常基本的搜索,但由于某种原因它似乎不起作用。任何建议都将不胜感激,我还是 pdo 的新手(非常新!请客气!)。
也没有用户提交的数据被插入到数据库中,所以我想我可以排除 xss 假设它的 SQL 注入免费?从我理解的 PDO 是什么?加上我使用没有写访问权限的独立数据库用户。
为安全起见,已将数据替换为 xxx
文件名为 search.php
*更新以反映建议的更改 *第二次更新以反映提供的帮助 *第三次更新
<html>
<head>
</head>
<body>
<form name="frmSearch" method="post" action="search.php">
<table width="599" border="1">
<tr>
<th>Keyword
<input name="var1" type="text" id="var1">
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<?php
$nameofdb = 'xxxxxx';
$dbusername = 'xxxxxxxxxxxxxx';
$dbpassword = 'xxxxxxxxxxxxx';
// Connect to MySQL via PDO
try {
$dbh = new PDO("mysql:dbname=$nameofdb;host=localhost", $dbusername, $dbpassword);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$var1 …Run Code Online (Sandbox Code Playgroud) 我创建此代码来更新mysql,它似乎没有工作.我尽了最大努力,但仍然没有用.它没有更新我的数据库.可能是什么问题呢?我的配置页面
<?php
define("dbuser", "XX");
define("dbpass", "XX");
define("dbhost", "localhost");
define("dbname", "smsplugin");
?>
$smsbal = $_POST['smsbal'];
$values =$_POST['values'];
$api = $_POST['api'];
$db = new PDO('mysql:host='.dbhost.';dbname='.dbname.';charset=utf8', dbuser, dbpass);
$sql="UPDATE user SET api=?,values=?,left=? WHERE id='1'";
$q=$db->prepare($sql);
$q->execute(array($api,$values,$smsbal));
<form method="POST" action="" class="well form-horizontal" style="width:338px;margin:0 auto;"><br />
<br />
<p>
<label for="api"><strong>API Setting</strong></label>
<input type="text" name="api" id="api" data-placement="right" data-html="true" data-animation="true"/>
</p>
<p>
<label for="values"><strong>API Values</strong></label>
<input type="text" name="values" id="values" title="These are the values that are passed to your SMS provider; <strong>Sender, Receiver and the Message</strong>. Depending …Run Code Online (Sandbox Code Playgroud) 我收到这样的错误
解析错误,期望第4行的'T_FUNCTION'
但是当我不使用类时,这些代码工作正常.之前我没有在PHP代码中使用类.我只是编写没有类的PHP代码.现在,我想在我的PHP代码中使用类,所以,我已经实现了一些代码.但它给我一个错误.我在哪里弄错了?(我以为我在课堂上犯了错误).
config.php文件
<?php
class Configuration
{
private $user = "root";
private $password = "password";
public $conn;
public static function connect()
{
if(self::$instance = null)
{
try
{
$conn = new PDO('mysql:host=localhost;dbname=database_table', $user, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
'Database Connection Error' .$e->getMessage();
}
}
return
}
}
?>
Run Code Online (Sandbox Code Playgroud)
post.php中
<?php
class StoreDatas
{
include_once('config.php');
if($_POST["register"] == "alldetails")
{
private $r_uname = trim($_POST["uname"]);
private $r_email = trim($_POST["email"]);
//some codes
//some codes
//some codes
// name validation
if(!preg_match("/^[a-zA-Z ]+$/", …Run Code Online (Sandbox Code Playgroud) 我的代码出了什么问题:
class Database
{
private $db;
public function __construct()
{
$dbname = 'dbname';
$dbhost = 'localhost';
$dbuser = 'dbuser';
$dbpass = 'dbpass';
$connStr = "mysql:$dbhost; dbname=$dbname";
try
{
$this->db = new PDO($connStr, $dbuser, $dbpass);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (Exception $ex)
{
echo $ex->getMessage();
exit;
}
}
public function getTableFromSQLQuery($query, $params)
{
$db = $this->db;
$result = $this->db->prepare($query);
if (isset($params) && count($params)>0)
foreach($params as $key=>$param)
$result->bindParam ($key, $param);
$result->execute();
return $result->fetchAll(PDO::FETCH_ASSOC);
}
Run Code Online (Sandbox Code Playgroud)
}
并从代码中调用:
$db = new Database();
$query = "SELECT …Run Code Online (Sandbox Code Playgroud) 我不明白为什么我的查询没有返回任何结果
<?php
include "connection.php";
$stmt = $db->prepare('SELECT * FROM my_tb WHERE column1 = :par1 ');
$stmt->bindValue(':par1', '%'.$_POST['submit1'].'%');
$stmt->execute();
while($row = $stmt->fetch())
{
print_r($row[0]);
print_r($row[1]);
print_r($row[2]);
};
?>
Run Code Online (Sandbox Code Playgroud)
当我改变'%'.$_POST['submit1'].'%'到$_POST['submit1'] 其工作的罚款.我正在寻找一个子串
我在尝试将新数据插入数据库时遇到问题,我甚至没有收到任何错误
$db = new MyPDO();
$datauser = array(
'account' => $acc,
'tid' => $tid,
'email' => $email,
'amount' => $amount,
'date' => 'NOW()',
'obj_id' => $object_id);
$sql = $db->query("INSERT INTO account_reg_log
(account, tid, email, amount, date, obj_id) VALUES
(:account, :tid, :email, :amount, :date, :obj_id)");
$sql->execute($datauser);
Run Code Online (Sandbox Code Playgroud)
运行脚本后检查数据库,看不到新行..任何想法如何修复帽子?
我刚开始学习PDO.我已连接到我的数据库,并且我在mySql数据库中进行了正常工作的登录.现在我试图从表单中获取三个数据,然后将它们插入表中.我已经在这一周了,我提出的每个版本都失败了.当我检查表时它仍然是空的,我没有收到任何错误消息.
由于我有其他PDO操作正在运行,我相信问题出现在下面的代码中.涉及的按钮名为'addGig'.这是我第一次使用按钮的名称......我对此并不自信.
我刚刚编辑了这篇文章,以包含我修改后的代码.这么多菜鸟错误!
$date = $_POST['date'];
$venue = $_POST['venue'];
$time = $_POST['time'];
if (!empty($date) && !empty($venue) && !empty($time)){
try{
$query = $connect->prepare("INSERT INTO gigs (date, venue, time) VALUES (:date, :venue, :time)");
$query->bindParam(':date' , $date);
$query->bindParam(':venue' , $venue);
$query->bindParam(':time' , $time);
$query->execute();
}
catch(PDOException $e)
{
handle_sql_errors($query, $e->getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
}
这是我的html表单
<form>
<label>date</label><br><input type="text" name="date"><br>
<label>venue</label><br><input type="text" name="venue"><br>
<label>time</label><br><input type="text" name="time"><br>
<br>
<button type="submit" value="addGig" name="addGig">add gig</button>
</form>
Run Code Online (Sandbox Code Playgroud)