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
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 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.
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 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 8 years ago.
Improve this question
I have already build a functioning login page which redirects the user the index.php file.
From previous help, I was able to get the wage and display it on the page depending on which user logs in. This is the table users in the database user_registration
user_id username password email wage
1 johnsmith jsmith99 jsmith#gmail.com 100
2 davidscott dscott95 davidscott#gmail.com 90
The part i am stuck on is creating a functioning form that the user can update their wage to the sql database.
Can someone please help me with the php code?
This is the form i already have in place:
<form id="change-wage" action="update.php" method="post">
<input type="text" id="new_wage" name="new_wage">
<input type="button" value="Save">
</form>
EDIT: this is the code, The aim of it is that the user can update the wage value in the table by filling in the textbox and pressing submit. any Ideas how i can acieve this?>
<?php //CHANGING THE WAGE
$username = '$_SESSION['MM_Username'];';
if (isset($_POST['submit'])){
$wage = $_POST['wage-new'];
//connect to server
mysql_connect ("localhost","root","") or die ("Could not connect");
mysql_select_db("user_registration") or die ("Could not connect to the database");
mysql_query ("UPDATE users SET wage='$wage' WHERE username = '$username'") or die ("Could not update");
}
?>
I wont give you the code unless you demonstrate as previous commentator said. However I will give you a an overview so you can work at it your self.
update.php
Check if your is logged in.
if TRUE, continue.
get the new wage from the form
$new_wage = $_POST['new_wage'];
Be sure to validate and clean the $new_wage variable.
Next stage assumes your using PDO
$params = array($new_wage, $logged_in_user_id);
$update = "UPDATE user_registration SET wage=? WHERE user_id=?";
$pdo->prepare($update);
$pdo->execute($params);
First of all if you are using session variables make sure you start the session -session_start();
$username = '$_SESSION['MM_Username'];';
should be
$username = $_SESSION['MM_Username']; (without single quotes)
$wage = $_POST['wage-new'];
should be
$wage = $_POST['new_wage']; as you named it in your html file
you are selecting database user_registation and I assume it should be user_registration
And last, think about switching to PDO or mysqli.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have a function that handles my db connection. In casse my main DB isn't available I want to use a backup one.
I tried to do so using below code but it's not working...
$host = 'xxx';
$database = 'xxx';
$login = 'xxx';
$pass = 'xxx';
if (! mysql_connect( $host, $login, $pass ) )
{
// try to connect to backup db
$host = 'yyy';
$database = 'yyy';
$login = 'yyy';
$pass = 'yyy';
mysql_connect ( $host, $login, $pass ) or die ( "Failed to connect to the database: " . mysql_error());
}
mysql_select_db ( $database ) or die ( "Failed to find the database" . mysql_error());
mysql_query("SET NAMES 'utf8'");
I'm sure about my connection parameters, so the problem isn't there
Edit:
I'm using an old version of php so I'm limited to Mysql_*
My 1st server is currently down, and I'm getting a 'Warning: mysql_connect() [function.mysql-connect]: Too many connections in ...' error
In fact my problem is that I need NOT to print this warning message if the 1st connection failed...
Please note Mysql_* is deprecated, use mysqli_ or PDO which is far more secure.
Have you tried outputting the error? I don't see it in your code.
I haven't used mysql_ for a long time, but try outputting the error using:* mysql_error()
As mentioned, it could be a variety of problems:
Mysql isn't started
Firewall is blocking the connection
Not using default port
...
Edit:
Also you may want to look into setting up replecation for such events as DB failure.
Don't do this in your code, just use something like MySQL Proxy to do it for you.
When php fails to connect, it generates a warning, which stops the php execution.
You may use #mysql_connect (function name prefixed with an "#") to avoid the warning. And your code will continue to execute, even if first sql server does not respond. crappy, but working.