No PostgreSQL link opened yet - php

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.

Related

PHP data not being received by database

I run this code, and it does not add data to the table. (Also I get no errors).
I think perhaps in someway it can't be sending the data to the table, is there a way to check whats happening in that regard or what do you guys think I should do to narrow down the problem?
I am using netbeans 8 if thats relevant.
<?php
$servername="localhost";
$username="test";
$password="";
$mysqli = new mysqli($servername, $username, $password);
if (!$mysqli){
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$sql = ("INSERT INTO test_table (column_test) VALUES ('boo')");
?>
You need to execute query, put code below under your $sql
mysqli_query($mysqli, $sql);

error: No database selected when displaying data on website

My name is Anna and I'm building a website with a festival calendar! People can add festivals when they're logged in. But now is my question, how can I display what people put in my database. I found the following code on this website, but when I put it in my script, I get the error that there's no database selected. But I do have a database selected (Festivals) ?
$sql = "SELECT festival_name, festival_organisator, festival_begin FROM Festivals WHERE festival_id=3";
$result = mysql_query($sql) or die(mysql_error());
$festival_name = $festival_organisator = $festival_begin = array();
while($row = mysql_fetch_assoc($result)) {
$festival_name[] = $row['festival_name'];
$festival_organisator[] = $row['festival_organisator'];
$festival_begin[] = $row['festival_begin'];
}
I hope anybody can help me! Thanks in advance
You have not yet connected to the database itself
Query selecting from the table Festivals does not mean that you've selected the Festivals database, those are two very different things.
The database Festivals contains all the tables (table Festivals, table x, etc).
The table Festivals contains data which you can query (festival_name, festival_organisator, etc).
Now you must connect
Taking a look at the documentation is always a good choice, let's see how you can connect to and select a database:
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>
You should be connected to the database, having access to the selected database through $link.
mysql_query($sql, $link)
But don't do it!
Unless you must, but cute puppies will die :(
You shouldn't be using mysql_* in the first place though since it's no longer supported. Instead you should be using PDO or mysqli to achieve your task. It may sound or appear trickier at first but it's actually rather simple:
Related answer on how to query with PDO.
The PDO documentation provide nice examples on how to query.
The mysqli_* documentation provide nice examples on how to query.

sql query not inserting into table?

<?php
$link = mysql_connect('localhost', 'sc2broad_testing', '1BananA2');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_query("INSERT INTO Persons (re) VALUES ('Peter')");
mysql_close($link);
?>
This code isnt taking the value 'peter' and inserting it into persons row 're'?? should i attempt to tell it which database somewhere? thanks . it is saying it connects successfully even if i am not telling it which database to connect to? only the server and user? i am confused.
I think you may need to specify the database that you are querying to?
mysql_select_db('db_name', $link)
If not try changing the mysql_query to:
print("INSERT INTO Persons (re) VALUES ('Peter')");
You can then check the query is correct and test it works outside of the php.

Whats wrong with my connection statement/Insert Query?

I can get it to work from home which I coded using mysqli but my school doesn't have that enabled so now it wont execute the query what am I doing wrong or is there a better way of doing this?
!!!Update!!!
So I connected to my website at home the one that works and redid the connection by just removing the i form the connections and the extra field (database name) in $dbc and it started getting errors. this happened even before I removed the database name from the connection. So I realized that the script doesn't know what database I'm connecting to. How can I fix this?
Home (works)
/* database connection */
$dbc = mysqli_connect('localhost', 'user', '9890667', 'zombies')
or die('Error Connecting to the MySQL Server');
/* insert query */
$query = "INSERT INTO zombie_sighted (first_name,last_name, when_it_happened, how_long, how_many, zombie_description, what_they_were_doing, fang_spotted, other, email) " .
"VALUES ('$first_name','$last_name','$when_it_happened','$how_long','$how_many','$zombie_description','$what_they_did','$fang_spotted','$other','$email')";
/* execute query */
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
mysqli_close($dbc);
School(connects but query doesn't work now) Updated
$dbc = mysql_connect('shcool url', 'username, 'password')
or die('Error Connecting to the MySQL Server');
/* insert query */
$query = "INSERT INTO zombie_sighted (first_name,last_name, when_it_happened, how_long, how_many, zombie_description, what_they_were_doing, fang_spotted, other, email) " .
"VALUES ('$first_name','$last_name','$when_it_happened','$how_long','$how_many','$zombie_description','$what_they_did','$fang_spotted','$other','$email')";
/* execute query */
$result = mysql_query($dbc, $query)
or die('Error querying database.');
mysql_close($dbc);
Are you certain localhost is the correct MySQL path at your school location? If this is a shared server that doesn't allow things like shell access I bet the db path is not localhost as it would be on a dedicated box.
$result = mysql_query($query) try this.
mysql_query
you pass the resource in as first param when the query string is supposed to be. also, make sure your mysql_connection fields are correct.

trying to backup mysql database using php

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.

Categories