Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I created a connect.php file in my website that I've hosted in godaddy, but when I include this file in my index.php page. All the content in the page went blank.
this is my connect.php file code
<?php
$host = 166.62.10.223;
$con = mysqli_connect("host","root","");
if(mysqli_connect_errno())
{
echo "error".mysqli_connect_errno();
}
?>
I really need some help
Change your code from
$host = 166.62.10.223;
$con = mysqli_connect("host","root","");
To
$host = 166.62.10.223;
$con = mysqli_connect($host,"root","");
And You need a password too if you are working on live server and username might not be root So replace them by actual values.
Example
$user = "liveserver_username"; //which is created in godaddy mysql wizard section
$password = "liverserver_password"; //which is created in godaddy mysql wizard section
$host = 166.62.10.223;
$con = mysqli_connect($host,"$user","$password");
more information on how to create DB,User and password.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm making a website that uses SQL and PHP functionalities. How do I connect to a database?
I would advise you begin by looking here.
You need to ensure that you have created user credentials with the correct permissions to query the database before you try this. You can do this through the cPanel of your web server (I'm going to assume you are using a web hosted server for this question).
Once you have a working and tested connection to the database, you can then start looking at the mySQLi documentation here. Which will show you how to execute and retrieve results from a database query and how to handle the returned data with PHP.
I see you are seriously downvoted.
I learned it the hard way and I am still learning to post here.
Stack sites are supposed to be searched first. If your question is already answered then people downvote you.
The solution to your question:
In your mysql or phpmyadmin you can set whether you use a password or not. The best way to learn is to set mysql with a password in my opinion. If you will launch a website online finally, you have to take security measures anyway.
If you make contact to your mysql database with you have to set:
username, password, database and servername ( for instance localhost).
The most secure way is using the OOP / prepared method:
$servername ='localhost';
$username='yourname';
$password='12345';
$dbname='name_database';
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($stmt = $conn->prepare("SELECT idnum, col2, col FROM `your_table` WHERE idnum ='5' ")) {
$stmt->execute();
$res = $stmt->get_result();
$qrow = mysqli_num_rows($res);
while ($row = mysqli_fetch_assoc($res)) {
var_dump($qrows); // number of rows you have
$total = implode(" / " , $row);
var_dump($total);
$idnum = $row['idnum'];
var_dump($idnum);
}
The easiest way that I do with my site is make a file called db.php containing:
<?php
$host = 'localhost';
$user = 'root';
$pass = 'password';
$db = 'databasename';
$mysqli = new mysqli($host,$user,$pass,$db) or die($mysqli->error);
..then in the index.php file, at the top:
<?php
require_once('db.php')
?>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a website which logs the ip,time and date of every visit. I am trying to write a function that displays how many times a certain ip address has accessed the site. It will say something like you have visited this site .... times.
Here is the php:
<?php
function visitor_counter()
{
require_once 'php_scripts/log_in_script_2.php';
$conn = new mysqli($hn,$un,$pw,$db);
if ($conn->connect_error) die($conn->connect_error);
$ip_var = $_SERVER['REMOTE_ADDR'];
$query = "SELECT COUNT(ip) FROM visitors WHERE ip = '$ip_var'";
$result = $conn1->query($query);
if (!$result) die ($conn->error);
echo $result;
mysqli_close();
}
?>
it is included in the webpage with
<?php require_once "php_scripts/visit_counter.php"; ?>
and the function is called in the code with
You have visited my site: <?php visitor_counter(); ?> times.
It does not output anything, and the rest of the webpage doesn't load after this line. I know the query is correct, I've tested it in phpmyadmin, and I know the login for the function has the correct privilages. Any ideas?
I added the changes from the comments above, but a problem was still there. The problem was that I was trying to echo the query result directly. I changed the query to $query = "select count(ip) as total_visits from visitors where ip = '$ip_var'"; and changed the echo to echo $result->fetch_object()->total_visits;, and it works.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I use Linux Debian
Local Lamp Server
i tried to make connection between Database and php :
<?php
$tf_host = 'localhost' ;
$tf_dbname = 'tinyforum' ;
$tf_username = 'root';
$tf_password = '' ;
$tf_handle = mysql_connect($tf_host , $tf_username , $tf_password);
if (!$tf_handle)
die('connection problem ..');
$tf_db_result = mysql_select_db($tf_dbname);
if($tf_db_result)
{
mysql_close($tf_handle);
die('selection problem');
}
die('OK');
mysql_close($tf_handle);
?>
The result :
http://www.foxpic.com/V0HrSdgb.png
pic of phpmyadmin:
http://www.foxpic.com/V0AlRhi6.png
the place where i save the php file :
/var/www/html/
Unless you copied your code incorrectly, your check is not going to result in what you want.
$tf_db_result = mysql_select_db($tf_dbname);
if($tf_db_result)
your selection seems fine so either you should change your code to
if(!$tf_db_result) //note the !
or restructure your code
if($tf_db_result) {
//Do the stuff you want to do when you are connected
} else {
//Die connection
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm new to PHP and I'm trying to setup a database for users to log in and sign up by using the code provided by this website here.
My website is located at: http://cloudgaming.byethost7.com/.
The problem is that whenever I try to log in or sign up, this error shows up:
Failed to connect to the database: SQLSTATE[28000] [1045] Access denied for user 'dbusername'#'localhost' (using password: YES)
I asked my girlfriend to try it as well, and she got the same error. If I pair the PHP document with CSS document, and place the PHP code into <style> tags, I don't get that error, but I don't get the CSS layout except the background. What am I doing wrong?
Go to your common.php file.
$username = "dbusername";
$password = "dbpassword";
Change above 2 lines with your host's username and password.
Most likely:
$username = "root";
$password = "";
or,
may be you are using your localhost settings on a remote web server. Change below lines with respective details.
$username = "dbusername";
$password = "dbpassword";
$host = "localhost";
//for connect database
$con=mysql_connect("localhost","root","");
mysql_select_db("your database_name",$con);
$dbuser = "db user name";
$dbpass = "db password";
$dbhost = "your host name";
$dbname = "the database name";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
do like this.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
New to PHP. Trying to simply output the contents of a mySQL table onto my HTML page. Here is my html code:
<body>
<div>
<?php
$hostname = "XXXXX";
$username = "XXXXX";
$password = "XXXXX";
$db_name = "XXXXX";
//connect to database
mysql_connect($hostname, $username, $password) or die("cannot connect to server");
mysql_select_db("$db_name") or die("cannot select database");
$sql="SELECT * FROM myTable";
$result=mysql_query($sql);
if (!$result) {die("SQL error retrieving data.");}
while ($row = mysql_fetch_array($result)) {
echo $row['field1'] . $row['field2'] . "</br>";
}
mysql_close();
?>
</div>
</body>
My output is simply this:
"; } mysql_close(); ?>
I know that there is data in the table. I can't see what I am doing wrong. Can anyone help? Thanks!
Try change your file extension to .php instead of .html
Please change your connection to PDO or mysqli because mysql is soon deprecated
The code is not interpreted as PHP code. Thus, the Browser interprets <?php as the start of a HTML element that is closed by /br>. This HTML element is unknown to the browser and therefore ignored. The following lines
";
}
mysql_close();
?>
are then visible in the browser, because they are not part of a HTML element.
You might need to uncomment an appropriate extension for mysql from php.ini, if you have php installed of course.
Also I would suggest you to use sqlite. It is easy to start and manage you DB, check it out: http://php.net/manual/en/book.sqlite.php