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
Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in index.php on line 17
Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'username'#'localhost' (using password: YES) in index.php on line 17
Warning: mysqli::query(): Couldn't fetch mysqli in index.php on line 19
Warning: get_data(): Couldn't fetch mysqli in index.php on line 19
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());
Parse error: syntax error, unexpected 'public' (T_PUBLIC), expecting end of file in index.php on line 15
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:
Issues connecting to MySQL. First the usernames and passwords are
here: login info
The XAMPP control panel: XAMPP
The testcode that worked previously: Registration
And the error it now presents: Registration Error
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.
Related
I am having trouble getting php to connect to my mysql database on a shared hosting. This worked when it was on my MAMP local server.
I get the following error:
Error: No database selected Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /hermes/bosnaweb03b/b2270/ipg.theburguersaredownco/cheval/main.php on line 34
My db_connect php file:
<?php
$connect = mysqli_connect('theburguersaredownco.ipagemysql.com', 'user', 'password');
if (!$connect) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_select_db('db_cheval');
?>
I get the message: "Connected successfully", so I am assuming the connection to the database is established and therefore there are no issues with the mysql_connect function or any of its arguments (i.e. the user and password are correct etc...).
my main.php file:
<?php
include 'db_connect.php';
include 'header.php';
error_reporting( E_ALL );
ini_set( "display_errors", 1 );
?>
<a id="roundLogo" href="index.php" class="not-active"></a><div>
</div></a>
<div id="subheader" class="notVisible">
<nav id="menu2">
<ul>
<?php
$query = mysqli_query($connect,"SELECT * FROM categories");
if (!$query) {
printf("Error: %s\n", mysqli_error($connect));
// exit();
}
while ($q = mysqli_fetch_array($query)) {
if($q['id']<4){ ?>
etc...
Line 34 is this one:
while ($q = mysqli_fetch_array($query)) {
It is the first time I do this and I am new to php and mysql so I don't have much experience. Can someone help me find the problem here. How can I debug this?
Thank you
You should use localhost for host parameter even if you running on hosting or local webserver but your problem is missing parameter in mysqli_select_db that why "No database selected" return
$connect=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($connect,"db_cheval");
I am trying to build a dynamic website for connecting pages with database I used the code as follows. connection with server is Ok but unable to select database. data base name, user id, password, ip of host all gave but not working. please help....
define('DB_NAME', '');
define('DB_USER', '');
define('DB_PASSWORD', '');
define('DB_HOST', '');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
else {
echo 'connected to server..................'; }
$db_selected = mysql_select_db($DB_NAME, $link);
if ($db_selected) {
print "Database Found";
}
else {
print "Database NOT Found";
}
Mysql has been deprecated and will eventually be removed. Consider using PDO or MySqli.
Here is a link to the PHP documentation page for making connections to a database using MySqli.
http://php.net/manual/en/mysqli.quickstart.connections.php
If you follow the instructions carefully you 'should' be able to connect. If not, perhaps you can post the error message you receive.
Try to give proper credential to connect to mysql server. mysqli_connect("localhost","root","password","db_name");
I'm new with mysql and php so please bear with me.
I'm trying to connect my first form to a table and I keep running into a new issue every time I "fix" something. I'm trying to test my form to make sure it connects before I move forward.
This is the form:
<form action="demo.php" method="post">
<p>input 1: <input type="text" name="input1"/></p>
<input type="submit" value="Submit" />
</form>
and this is my "demo.php" file:
<?php
define('BD_NAME', 'DEMO');
define('DB_USER', 'DEMO');
define('DB_PASSWORD', 'PASSWORD');
define('DB_HOST', 'HOST');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
echo 'Connected successfully';
mysql_close();
?>
I keep getting this error:
Can't use DB_NAME: Access denied for user 'USER'#'%' to database 'DB_NAME'
Again I'm new at this and I'd appreciate any help. Thanks!!
You have a typo.
define('BD_NAME', 'DEMO'); should be define('DB_NAME', 'DEMO');
Notice the DB_NAME global variable that is not resolve :
Can't use >>>>DB_NAME<<<<: Access denied for user 'USER'#'%' to database 'DB_NAME'
It means that either DB_NAME is not declared OR that the value of DB_NAME is "DB_NAME".
always use error_reporting(E_ALL) and display errors while coding to avoid these mistakes
in dbconect
<?php
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (mysqli_connect_errno()) {
echo " can't connect : " . mysqli_connect_error();
exit;
}
?>
and in dbconfig
<?php
define('DB_HOST', "localhost");
define('DB_USER', "root");
define('DB_PASS', "");
define('DB_NAME', "carwash");
define('DB_PREFIX', "carwash_");
?>
and output
Warning: mysqli_connect() [function.mysqli-connect]: (42000/1049): Unknown database 'carwash' in C:\xampp\htdocs\carwash\includes\dbconnect.php on line 2
can't connect : Unknown database 'carwash'
I don't know what's wrong or what's happened, it happen only today but the day pass it didn't happen
Make sure you have a database named carwash.
Xampp comes with phpmyadmin,have a look for the databases you have there.
I would love to check if i can connect to database using given username, password, and database host.
So far i was trying:
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$conndatabase = mysql_select_db($dbname);
if(!mysql_ping($conn) || !$conndatabase){
//do something when cant connect
} else {
//do something when you connected
}
It works when i give bad $dbname, cuz it cant select this database. But when i give wrong host, username or password, it will give me white page with errors. For example:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'rootx'#'localhost' (using password: YES) in C:\xampp\htdocs\strony\planer\config\opendb.php on line 6
Warning: mysql_ping() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\strony\planer\grupy.php on line 3
Question:
Is there a way to check if i can connect using given data without errors? It just gives errors before getting to mysql_ping()
EDIT
After trying Mike Brant solution, i think i could have explained it wrong. I am trying to connect to database, and if it lets me, it shows user page he wanted to access, but when it's impossible, it redirects him to the other page, to modify database information.
if(!$conndatabase){
header('Location: index.php');
}
I am using that for checking if database exists. But if i try if(!$conn) or something similar, its too late, because i got errors displayed on $conn = mysql_connect($dbhost, $dbuser, $dbpass). So i cant redirect, as it gives me
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'#'localhost'~~
Warning: Cannot modify header information - headers already sent by~~
Yes. Just test the value of $conn. If it is false then the connection failed, and you shouldn't even try to proceed to the db selection step.
Your code might look like this:
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if (false === $conn) {
throw new Exception('Could not connect to database: ' . mysql_error());
}
$db_select = mysql_select_db($dbname. $conn);
if (false === $db_select) {
throw new Exception('Could not select database: ' . mysql_error($conn));
}
The PHP documentation is very clear on this: http://php.net/manual/en/function.mysql-connect.php
By the way, you should look at using mysqli or PDO as mysql_* functions are deprecated.
Try to debug your connection with something like
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link){
die('Could not connect: ' . mysql_error());
}
In this way you can easilly check if connection is estabilished otherwise you will print an error.
Then I would like to remember you that mysql_ functions are deprecated so i would advise you to switch to mysqli or PDO
When you look on my edit in question, you can see those answers didn't solve my problem completely. I still lacked some way to avoid errors. That's where i used error_reporting(0);.
I am not sure if that's best solution, but it works like a charm for me.
So the code for everything i tried to achieve looks like that:
opendb.php file
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password123';
$dbname = 'databasename';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$conndatabase = mysql_select_db($dbname);
?>
And there on the site i am checking connection:
<?php
error_reporting(0);
include 'opendb.php';
if(!mysql_ping($conn) || !$conndatabase){
header('Location: index.php'); //or any other site where you can config your database connection information.
}
So yeah, thats complete solution to my problem. I guess i could use !$conn instead of !mysql_ping($conn) with same result.
As has already been said, it’s the first example on the PHP manual page for the mysql_connect function.
Secondly, you should be either using the mysqli_ functions or PDO, as the mysql_ functions are deprecated and currently being phased out.
To check a connection with PDO:
try {
$db = new PDO('mysql:host=127.0.0.1;dbname=dbname', 'user', 'pass');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
header('HTTP/1.1 500 Internal Server Error');
die($e->getMessage());
}