I got this code from this site: http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx
But I'm just a beginner so I don't know what the config.php and opendb.php suppose to mean. Do I have to create those 2 files in order for this code to work?
If yes, then how do I create it, it isn't included in the site how to create it.
<?php
include 'config.php';
include 'opendb.php';
$tableName = 'mypet';
$backupFile = 'backup/mypet.sql';
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);
include 'closedb.php';
?>
can I just include these lines on the top code so that I will not be putting the include 'opendb.php' anymore:
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Hospital", $con);
Yes you need those two files and the other closedb.php included in your script. As the name suggests, config.php most likely contains the DB parameters: user, pass, host, dbname while opendb.php and closedb.php will contain the routines to connect and close your DB.
Related
BIt of a php/mysql noob here, hope someone can help.
Ok so i have a URL which has an id in the querystring like so: wwww.mysite.com/page1.php?id=1
What i want to do is connect to a table in the database and get the data from the columns on one row where the first column named ID equals the id number held in the querystring.
I then want to print the data from each column in different div's elsewhere on the page.
There's also the additional issue of what to do if there's no row in the table with the same id as the querystring, i'd want it to change the id in the querystring to 1 and load that rows data.
I had a little go, i know it connects ok but i have no idea if the rest is what i want:
<?php
$link = mysql_connect('Address', 'Database', 'Password');
if (!$link) {
die('Could not connect to MYSQL database: ' . mysql_error());
}
$per = $_GET['id'];
$query = "select A,B,C,D,E,F,G,H,I,J,K,L from table_name where per=".$_GET['ID']."";
echo $result['A'];
mysql_close($link);
?>
And then put this in the div's to print the data.
<?php echo $result['A']; ?>
Am i along the right lines or completely wrong?
$dbConnection = mysql_connect('Address', 'Database', 'Password');
if (!$dbConnection) {
die('Could not connect to MYSQL database: ' . mysql_error());
}
$per = $_GET['id'];
$query = $dbConnection->prepare("select A,B,C,D,E,F,G,H,I,J,K,L from table_name where per = ?");
$query->bind_param('s', $per);
$query->execute();
$result = $query->get_result();
<?php echo $result; ?>
use this code first to avoid SQL Injection second that's the way it should work in PHP first prepare the query second execute and only then show it.
Use mysql_query function in your code.
mysql_* functions is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used.
<?php
$link = mysql_connect('Address', 'Database', 'Password');
if (!$link) {
die('Could not connect to MYSQL database: ' . mysql_error());
}
$per = $_GET['id'];
$query = "select A,B,C,D,E,F,G,H,I,J,K,L from table_name where per=$per";
$result = mysql_query($query, $link) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo $row['A'];
mysql_close($link);
?>
I'm trying to SELECT elements from a database. This database uses the UTF-8 encoding.
For example, in one column named title, I have a row with this text: Crăciunul. It appears fine in phpMyAdmin as Crăciunul.
Now, I'm trying to connect to database in PHP and SELECT this database element. The problem is that it appears as Cr?ciunul on my website, which has the UTF-8 encoding setted, too.
This problem appears only if I get elements form database. I have made a string in PHP = Crăciunul, and it is shown fine on the website.
Thanks a lot!
EDIT 1:
$con = mysql_connect("HOST","USER","PASS");
mysql_select_db("DB", $con);
if (mysql_errno($con) !== 0)
{
die("Failed to connect to MySQL: " . mysql_error());
}
if (!mysql_select_db("DB"))
{
die('Could not select database: ' . mysql_error());
}
$query = mysql_query("SELECT * FROM table LIMIT 1");
while($row = mysql_fetch_array($query)) {
echo $row['title'];
}
Set the character encoding of the DB connection.
$con = mysql_connect("HOST","USER","PASS");
mysql_set_charset('utf8', $con);
Also, please don't use this deprecated mysql API for new development.
try this ...
place this code before executing query ...
mysqli_set_charset ($dbconnection, "utf8");
try this
mysql_query('SET NAMES UTF8');
and
mysqli_set_charset($conn,"UTF8");
I am completly new to databases and am trying to make a simple query to a postgreSQL database. Now the problem is that I don't really understand how things work. In my index.php I do
$dbconn = pg_connect("host=myhost port=5432 dbname=mydbname user=myuser password=mypass sslmode=require options='--client_encoding=UTF8'") or die('Could not connect: ' . pg_last_error());
And I am connected to the database. When I try to query it from another myphp.php
$query = "INSERT INTO public.account_recover_users VALUES ($mykey,$email,$name)";
$result = pg_query($query);
I get the error in the title description. What is the proper way to connect to the database so that I can access it from other files than index.php as well?
P.S. $mykey,$email,$name are 3 variables containing a key (randomly-generated), an email and a name.
I have this query:
$res=mysql_query("SELECT * FROM `t_modules` WHERE nPageM='61'") or die(mysql_error());
In phpmyadmin it returns (as expected) 2 rows but on page it returns 0.
If i use
$res=mysql_query("SELECT * FROM `t_modules` WHERE nPageM<>'61'") or die(mysql_error());
or
$res=mysql_query("SELECT * FROM `t_modules`") or die(mysql_error());
it runs on the page correctly it's just the WHERE and = combination that doesn't work
I also checked that the type for nPageM is int(11)
UPDATE
I can run comparisons on other columns in the table but not on nPageM
$res=mysql_query("SELECT * FROM `t_modules` WHERE id_md='5'") or die(mysql_error());
Is working. But i still don't have a clue about why it's not working on the nPageM column
have you ensured that you have included a connection to the databse in your php script before running this code?
<?php
$con = mysql_connect('sqluser', 'sqlpassword', 'sqlserver');
$db = mysql_select_db('dbame', $con);
//now, make sure it's connecting
if (!$con) {
die('mysql connection error' . mysql_error());
}
if (!$db) {
die('mysql Database error' . mysql_error());
}
?>
Instead try putting brackets around it:
$res=mysql_query("SELECT * FROM `t_modules` WHERE (nPageM<>'61') ") or die(mysql_error());
After I upload a photo to a server, I want to save it in the user's database in MySQL, but for some reason, it is not working. Below is the code for uploader.php:
session_start();
if(!$_SESSION['userid']) {
header("Location: index.php");
exit;
}
$con = mysql_connect("host","db","pw");
if (!$con)
{
die('Could not connect: ' .mysql_error());
}
mysql_select_db("db", $con);
$sess_userid = mysql_real_escape_string($_SESSION['userid']);
$query = "SELECT * FROM Members WHERE fldID='$sess_userid' UPDATE Members SET PortraitPath = 'profileportraits/' . '$_FILES[file][name]'");
$result = mysql_query($query) or trigger_error(mysql_error().$query);
$row = mysql_fetch_assoc($result);
I'm sure there is something very wrong with my query, but I can't figure out what it is. The photo is definitely being saved into the folder. But I simply want to update its path in the user database for later use. Thank you!
as it was mentioned already, you cannot use these two queries at once.
but there is also weird syntax: you're trying to use PHP's concatenation operator inside of mysql query.
And you did not escape a string parameter - very bad!
So, looks like you need something like
$sess_userid = mysql_real_escape_string($_SESSION['userid']);
$PortraitPath = mysql_real_escape_string('profileportraits/' . $_FILES['file']['name']);
$query = "UPDATE Members SET PortraitPath = '$PortraitPath' WHERE fldID='$sess_userid'";
You can do two separate queries:
UPDATE Members SET PortraitPath = 'profileportraits/' . '$_FILES[file][name]'
WHERE fldID='$sess_userid';
And:
SELECT * FROM Members WHERE fldID='$sess_userid'
It seems you tried to put two queries(SELECT and UPDATE) into one query which will result in invalid query error.
Just wondering why you need two queries since you already know the userid and all you want is to update. All you need is to update the file path
UPDATE Members SET PortraitPath = 'profileportraits/' . '$_FILES[file][name]' WHERE fldID='$sess_userid';