so I've been trying to get this working for a few days now and I am completely lost. I've tried following a few of the answers on here but I can't seem to figure out what I need to change to adapt it to my database.
There have been complex and simple solutions I have seen, but what I am attempting at the minute is this:
...
<?php
echo "";
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
define('DB_NAME', 'username_test');
$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die('Could not connect to MySQL: ' . mysqli_connect_error() );
mysqli_set_charset($dbc, 'utf8');
?>
<?php
public function get_data()
{
$mysqli= new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die("Couldn't connect".mysqli_connect_error());
$sql="SELECT Staff_Surname, Staff_Forename FROM users";
$result=$mysqli->query($sql) or die($mysqli->error);
while ($row=$result->fetch_array(MYSQLI_ASSOC))
{
echo "<option value=\"".$row["Staff_Surname"]."\" selected>".$row["Staff_Forename"]."</option>";
}
}
?>
<html>
<body>
<select name="abc" id="xyz">
<?php get_data(); ?>
</select>
</body>
</html>
...
which just results in a bunch of errors when I try to run it in a browser. I am using Microsoft Expression Web 4 and XAMPP.
This is a screenshot of the SQL database I just need to pull the forename and surname into a dropdown box: SQL database
Any help you guys can offer would be really appreciated. Thank you.
Sorry, in the preview it looked like it showed what the error was. When I try to run the code in a browser I get this: enter image description here
: Use of undefined constant DB_PASS - assumed 'DB_PASS' in on line :使用未定义的常量 DB_PASS - 在中第行假定为“DB_PASS”
: mysqli::__construct(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES) in on line :mysqli::__construct(): (HY000/1045): 在第行的,用户 'username'@'localhost'(使用密码:YES)被拒绝访问
: mysqli::query(): Couldn't fetch mysqli in on line :mysqli::query():无法在第行的获取 mysqli
: get_data(): Couldn't fetch mysqli in on line :get_data():无法在第行的获取 mysqli
Change
$mysqli= new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die("Couldn't connect".mysqli_connect_error());
to
$mysqli= new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die("Couldn't connect".mysqli_connect_error());
: syntax error, unexpected 'public' (T_PUBLIC), expecting end of file in on line :语法错误,意外的“公共”(T_PUBLIC),预计第行的文件结束
Change
public function get_data()
to
function get_data()
These changes should take care of all the errors you are receiving.
Code with @stealthyninja edits:
<?php
echo "";
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
define('DB_NAME', 'username_test');
$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die('Could not connect to MySQL: ' . mysqli_connect_error() );
mysqli_set_charset($dbc, 'utf8');
?>
<?php
function get_data()
{
$mysqli= new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die("Couldn't connect".mysqli_connect_error());
$sql= "SELECT Staff_Surname, Staff_Forename FROM users";
$result= $mysqli->query($sql) or die($mysqli->error);
while ($row=$result->fetch_array(MYSQLI_ASSOC))
{
echo "<option value=\"".$row["Staff_Surname"]."\"selected>".$row["Staff_Forename"]."</option>";
}
}
?>
<html>
<body>
<select name="abc" id="xyz">
<?php get_data(); ?>
</select>
</body>
</html>
EDIT:
I don't think I have changed anything from when it worked before. I built the Registration page following a tutorial in the book "PHP and MySQL for Dynamic Web Sites fifth edition" by Larry Ullman.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.