Mysql database connection details never work in included fle - mysql

Whenever I use a seperate file to store DB connection details:
<?
$username = "xxx";
$password = "xxx";
$database = "xxx";
?>
To use in my connection string, I can never get it to work. I have to embed the details in the connection string itself.
// Opens a connection to a mySQL server
$connection=mysql_connect ('xxx', 'xxx', 'xxx');
if (!$connection) {
die("Not connected : " . mysql_error());
}
// Set the active mySQL database
$db_selected = mysql_select_db('xxx', $connection);
if (!$db_selected) {
die ("Can\'t use db : " . mysql_error());
}
Can anyone please tell me why I have to do that? I'd really like to keep my details in a seperate file.
Thankyou

include_once('path_to_the_file/db_details.php')
You have to include the file.
Additionally, please don't use MySQL, but MySQLi, or PDO. MySQL is deprecated and not maintained any more.

Related

Migrate mySQL data from php5.2 to php7

We have a really old codebase with php5.2 some of them even in php3 using mysql 5.1 that we are trying to migrate to laravel 7/8. The data stored in the database is Japanese characters stored using latin1 encoding as,
¥í¥°¥¤¥óÀ®¸ù
¥í¥°¥¢¥¦¥È
¥á¡¼¥ë¥¢¥É¥ì¥¹Êѹ¹
Those data are displayed correctly when using php5.2 and are working fine in the current codebase but when I try to access that data using any version beyond php5.2 I cannot get the correct value.
Things I tried but didn't work.
Changed the file encoding with header in php file.
Changed string encoding with mb_convert_encoding.
Set default_charset in php.ini to empty string.
But none of the solutions seems to work. Is there any other way I can correctly display those data?
$dsn = 'mysql:dbname=dbname;host=127.0.0.1;port=3306';
$user = 'root';
$password = '';
$db = new PDO($dsn, $user, $password);
$query = $db->prepare('SELECT * FROM tablename');
if ($query->execute()) {
echo '<ul>';
while ($row = $query->fetch()) {
echo '<li>' . $row['column_name'] . '</li>';
}
echo '</ul>';
}
The same block of code displays correct data in the browser using php5.2 but it doesn't work in php7.3, how is that possible?
Specify the charset in the dsn, something like
$db = new PDO('dblib:host=host;dbname=db;charset=utf8mb4', $user, $pwd);
http://mysql.rjweb.org/doc.php/charcoll#php
More on Mojibake: Trouble with UTF-8 characters; what I see is not what I stored

What does this icon mean

What does this icon mean. I know my question must be answer even without the image but how hahaha, Im creating a database, do I need additional code in my php to create connection with my database? "fcm" and "fcm_db" are my databases, I created it and I think somehow it auto generated that "fcm" that I highlightened.
It is a Group icon.
It displays when two or more databases start with the same name (prefix).
It has no effect on the database connection or anything.
For your php code you need to specify the server where your database is located, then the username, and password this an example:
$dbname="fcm";
$servername="localhost"
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else
$conn->query($sql);

Admin panel - Creating an edit users button

I've created an admin panel on my website so when the admin logs in he can edit users. I'm trying to get it to create a table that displays a list of all the users on the database, however, when I run it I get the error:
No database selected
Here is the code in my editusers.php:
<?php
include 'adminpage.php';
include 'connection.php';
$sql = "SELECT * FROM Users";
$result = mysql_query($sql)or die(mysql_error());
echo "<table>";
echo "<tr><th>UserID</th><th>First Name</th><th>Last Name</th><th>Email</th><th>D-O-B</th></tr>Username</th><th>Password</th><th>";
while($row = mysql_fetch_array($result)){
$userid = $row['UserID'];
$firstname = $row['FirstName'];
$lastname = $row['LastName'];
$email = $row['Email'];
$dob = $row['DateofBirth'];
$username = $row['Username'];
$password = $row['Password'];
// Now for each looped row
echo "<tr><td style='width: 200px;'>".$userid."</td><td style='width: 200px;'>".$firstname."</td><td>".$scale."</td><td>".$lastname."</td><td>".$email."</td></tr>".$dob."</td></tr>".$username."</td></tr>".$password."</td></tr>";
} // End our while loop
echo "</table>"
?>
First of all it looks like you are using mysql which isn't a wise move. This is because Mysql is actually deprecated and was improved to mysqli. Your problem may be to do with your database connection. You also haven't set a database. Like I said you can set an active database in your connection script. It should or could look something like this.
<?php
$conn = mysqli_connect("localhost", "root", "password", "database");
// Evaluate the connection
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}
?>
After that, your sql query is correct by selecting all from you table 'users' but in order to proceed I recommend creating a query where you use mysqli_query an select the $sql and $conn as parameters. In all honesty it is much advised to stop and continue once you have adapted to mysqli. Alternatively you can use PDO which in some cases can be seen as better to use rather than mysqli but the choice is yours. I personally would get to grips with mysqli and then look at some answers on Stack Overflow to decide whether you should use PDO or not. Visit the PHP manual here. Enter all the mysql functions you know and it will show you how to use the new mysqli version of the functions. Don't think that it is as simple as just adding and 'i' to the end of a mysql function. That's what I initially thought but there is alot to do with extra parameters etc. Hope this helps :-)

Need help connecting to my Mysql database on local server

I'm quite new to databases and have no idea where I have gone wrong. Please help me find out why I can't connect to my database.
I'm getting an error with Dreamweaver - Dynamicaly-related files cannot be discovered because there is no site definition for this document
My Site root is located in htdocs.
the main file I'm trying to run is "dataquery.php" - htdocs/LetsPlays/dataquery.php
<?php
include 'includes/databaseform.php';
$query = "SELECT * FROM userchanel";
$result = mysql_query($query);
while($person = mysql_fetch_array($result));
{
echo $person['chanelurl'];
}
?>
Dataquery is connected to "databaseform.php" - htdocs/LetsPLays/includes/databaseform.php
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass ='';
$db = 'mysql_tut';
$conn = mysql_connect($dbhost,$dbuser,$dbpas);
mysql_select_db($userchanel);
?>
So I'm trying to connect to userchanel table through user tbl
screenshot: http://imageshack.us/f/23/usertbl.png/
Files run through wordpress are set up to run on 127.0.0.1
Please help me.
Sorry for the noobishness! I have no idea what I'm missing!
When I run the html file all I get is a blank page!
Replace the line
$conn = mysql_connect($dbhost,$dbuser,$dbpas);
with
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
notice the double 's' on $dbpass, also if I may you should not rely on deprecated features when writing new code, use PDO instead, also don't login to db as root, create a user, don't be lazy, with that your database connection code should like:
<?php
$db = new PDO('mysql:dbname=databasename', 'username', 'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
the init command is not actually necessary I just included it cause I always use it, while the querying could be done as
<?php
include 'includes/databaseform.php';
$query = $db->prepare("SELECT * FROM userchanel");
$query->execute();
while(($person = $query->fetch(PDO::FETCH_ASSOC)) !== false);
{
echo $person['chanelurl'];
}
If you're only using one column you should fetch only that as below:
<?php
include 'includes/databaseform.php';
$query = $db->prepare("SELECT chanelurl FROM userchanel");
$query->execute();
while(($channelurl = $query->fetch(PDO::COLUMN)) !== false);
{
echo $channelurl;
}
I didn't include closing braces for php code as they are not necessary also do some error checking var_dump($db->errorInfo()); and var_dump($query->errorInfo()); - didn't include this in the code as I only use them in checking any issues with my code, good luck!
instead of htdocs/LetsPlays/dataquery.php
try localhost/dataquery.php
I recommend you to make and additional file for database connection and include it anywhere you need. Because when you need to push project live you need to change in every file incase file is includeed you need to change just at one place and it effects every where connection for localhost is
<?php
// Replace the variable values below
// with your specific database information.
$host = "localhost";
$user = "root";
$pass = "";
$db = "yourdatabase";
// This part sets up the connection to the
// database (so you don't need to reopen the connection
// again on the same page).
$ms = mysql_pconnect($host, $user, $pass);
if ( !$ms )
{
echo "Error connecting to database.\n";
}
// Then you need to make sure the database you want
// is selected.
mysql_select_db($db);
?>
Save all the above code in one file save it as dbConfig.php and include it any where
like
include ("dbConfig.php");
Now in that file you are connected to db you can interact with database.

'ODBC'#'localhost' error while updating

Im getting this error when Im trying to update data in the database.
this is my database.php file
<?php
$db_name = "db";
$db_server = "localhost";
$db_user = "xxxx";
$db_pass = "zzzzzzzzz";
$mysqli = new MySQLi($db_server, $db_user, $db_pass, $db_name)
or die(mysqli_error());
?>
update.php
<?php
require 'database.php';
$title = mysql_real_escape_string($_POST['title']);
$id = mysql_real_escape_string($_POST['id']);
$update_query = "UPDATE photos SET title = '$title' WHERE id='$id'";
$result = $mysqli->query($update_query) or die(mysqli_error($mysqli));
if ($result) {
echo "Success!";
echo "The title of this photo has been changed to: <strong>$title</strong>";
}
?>
The error message:
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'#'localhost' (using password: NO) in C:\wamp\www\myPhotosWebsite\changePhotoTitle.php on line 5
You are mixing procedural and object-oriented style calls.
Try:
$title = $mysqli->escape_string(_POST['title']); /* Call as a method */
instead of:
$title = mysql_real_escape_string($_POST['title']);
real_escape_string requires a valid connection handle, as it needs to know the connection character set.
If you call it as a procedure, you should pass the connection handle as a first param:
mysql_real_escape_string($connection_handle, $string_to_escape)
or just call it as a method as described above.
See mysqli_real_escape_string for more detail
In your mysql connect() it seems that your user name/password combination is being denied access to mysql, you might want to check your details and try again.
mysql_real_escape_string requires a database connection to operate on. Unless you pass one explicitly, that means you have to call mysql_connect() first. But you're using a MySQLi() object to get the connection instead. I don't think using MySQLi() will set the default connection mysql_ family functions. Certainly,
(using password: NO)
implies it is not getting the $db_pass.
It's best to stick to either ‘mysql’ or ‘mysqli’, rather than try to mix both. If you're using MySQLi you might want to take advantage of parameterised statements to avoid having to call $mysqli->escape_string() explicitly.
PS.
echo "The title of this photo has been changed to: <strong>$title</strong>";
$title is SQL-escaped, but not HTML-escaped. It will have unwanted backslashes in whilst not preventing HTML-injection (XSS attacks). Instead:
echo 'The title of this photo has been changed to: <strong>'.htmlspecialchars($_POST['title']).'</strong>';