我试图使用PHP将数据存储在数据库中,但它没有与此连接

时间:2016-02-02 11:54:55

标签: php html mysql

我是PHP的初学者并尝试在MySql中存储数据,但在存储数据时存在问题,数据未输入数据库。

<?php
$hostname = "localhost"; // usually is localhost
$db_user = "root"; // change to your database password
$db_password = " "; // change to your database password
$database = "dictionary"; // provide your database name
$db_table = "dict"; // your database table name

$db = mysql_connect('localhost', 'root',’’); //root is a default user and password usually blank
mysql_select_db($database,$db); ?>
<?php

if (isset($_REQUEST['Submit'])) {
$ sql = "INSERT INTO $db_table(eword,hword) values ('".mysql_real_escape_string(stripslashes($_REQUEST['eword']))."','".mysql_real_escape_string(stripslashes($_REQUEST['hword']))."')";
if($result = mysql_query($sql ,$db)) {
echo "<h1>Thank you</h1>Your information has been entered into our database<br><br>";
}
else {`enter code here`
echo "ERROR: ".mysql_error();
}
}?>

1 个答案:

答案 0 :(得分:0)

使用此代码进行数据库连接。
该代码用于从数据库中获取数据&amp;直接在窗口上显示..

$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password
mysql_select_db('your_database_name');

$query = "SELECT * FROM your_table_name"; //You don't need a ; like you do in SQL
$result = mysql_query($query);

echo "<table>"; // start a table tag in the HTML

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
echo "<tr><td>" . $row['your_first_column_name'] . "</td><td>" . $row['your_second_column_name'] . "</td></tr>";  //$row['index'] the index here is a field name
}

echo "</table>"; //Close the table in HTML

mysql_close(); //Make sure to close out the database connection