I built a local database and used a connect script to connect to the database under localhost with the following line:
mysql_connect('localhost', 'root', '');
I now need to move the production server work to my actual server.
My first question would have been what should I change "localhost" to, and the understanding of the root? I need to prepare a PRIVATE section on my server but currently everything is in public_html. So is that what I replace localhost
with?
However on searching I've found that mysql_connect
is deprecated. So what is the replacement?
This is the entire script for connecting to the database marmit_1
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('marmit_1');
Here is the simple example of MySQLi connection.
<?php
$con=mysqli_connect("localhost","root","password","database_name");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM test");
echo "<table border='1'>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Caption</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['caption'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
References:
localhost
in this case is the host name where your script will attempt to connect to MySQL server. localhost
normally connects to the loop back adapter, in other words, it connects to the same machine as where the code is running.
If the MySQL server is running on the same physical machine as Apache/PHP, then you can connect to MySQL server using localhost
as the host name, provided MySQL server is configured to run on localhost (the default).
You would only change localhost
if MySQL server were on a remote host or manually configured to not bind to the default loopback adapter of 127.0.0.1
.
Here is super simple PDO-based solution:
<?php
include 'bestpdo.php';
$result = DB::query("SELECT * FROM test")->fetchAll();
?>
<table border='1'>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Caption</th>
</tr>
<?php foreach($result as $row) {
<tr>
<td><?=$row['firstname']?></td>
<td><?=$row['lastname']?></td>
<td><?=$row['caption']?></td>
</tr>
<?php endforeach ?>
</table>
Note that with PDO wrapper your code will remain as simple as old mysql ext-based used to be, no matter what query you run.
While with raw mysqli it would be PAIN and suffering - which none of these ads care to tell you.
As others already said, you should use the mysqli or the PDO library (PDO is what I would prefere) - however you should definitly make use of prepared statements.
I do not quite understand you with this part:
[...] what should I change "localhost" to, and the understanding of the root? I need to prepare a PRIVATE section on my server but currently everything is in public_html. So is that what I replace localhost with? [...]
Let split this in two parts:
"localhost in the mysql connection"
This value depends on your server. If you have a simple webhost your provider should have given you some information on how to access the mysql database. When you have a dedicated/virtual server and hosting your own solution you have to access the server via the hostename or ip. So localhost or 127.0.0.1 is ok, when mysql is provided by the same server as your webpage (e.g. same server running apache and mysql)
"need to prepare a private section"
I think you are looking for virtual hosts[1] - with these you can put different webpages in different folders: e.g.:
/srv/www/mypage.com/
/srv/www/blog.mypage.com/
/srv/www/myothersuperpage.com/
so you can access with mypage.com the webpage that is found under /srv/www/mypage.com (and same for the others)