[英]Connecting site to phpMyAdmin database
I have a website that has forms, images, text, etc. I want extract data from the forms and keep a record of them in mySQL. 我有一个包含表单,图像,文本等的网站。我想从表单中提取数据并将其记录在mySQL中。 In order to do this do I need to change the file extension from '.html' to '.php'?
为此,我需要将文件扩展名从“ .html”更改为“ .php”吗? And if so then will this effect any inline css?
如果是这样,那么这会影响任何内联CSS吗?
Also, when I need to connect to the server via the php, how to I know the database username, database password, and the database host? 另外,当我需要通过php连接到服务器时,如何知道数据库用户名,数据库密码和数据库主机?
I have go daddy as the web host, and use the CPanel they provide to access the phpmyadmin 我已经将爸爸作为网络主机,并使用他们提供的CPanel访问phpmyadmin。
Thanks - any help is highly appreciated! 谢谢-非常感谢您的帮助!
This is the PHP code I have so far, and I keep getting error alerts when I run the test through XAMPP: 这是到目前为止的PHP代码,通过XAMPP运行测试时,我会不断收到错误警报:
mysql_connect()
is already deprecated please consider using mysqli
or PDO
. mysql_connect()
已被弃用,请考虑使用mysqli
或PDO
。 PDO . PDO。 database connection example :- You should create a separate class containing the functions for basic operation in database and keep that file separate from your other code , just inherit the class and use the connection and function .
数据库连接示例:-您应该创建一个单独的类,其中包含数据库中基本操作的功能,并将该文件与其他代码分开,仅继承该类并使用连接和功能。
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
foreach($dbh->query('SELECT * from FOO') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
As the other answer stated, use pdo
or mysqli
instead of 'mysql`. 如其他答案所述,请使用
pdo
或mysqli
代替“ mysql”。 A good way to use PDO is to put the connection code within a file, and include this file anywhere you need to make use of the database. 使用PDO的一种好方法是将连接代码放在文件中,并在需要使用数据库的任何位置包含此文件。
Let's call this file dbconnector.php
. 我们将此文件称为
dbconnector.php
。
try
{
$conn = new PDO($connectionString, 'root', '');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
Now, wherever you want to use database, just use :- 现在,无论您想在哪里使用数据库,都只需使用:-
include 'dbconnector.php'
Now, you can access the connection variable via $conn
. 现在,您可以通过
$conn
访问连接变量。
Read more on PDO. 了解有关PDO的更多信息。 http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html
http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.