I usually connect php to mysql with localhost in my PC..
now i'm trying to put my project in cloud https://c9.io ,but i can't connect to mysql. i already have mysql database in cloud and put my project in same place...
mysql_connect("/lib/mysql/socket/mysql.sock","myUser","") or die(mysql_error());
i use script above to connect but i get Unknown MySQL server host '/lib/mysql/socket/mysql.sock' (1)
what shoul i do ?
Okay, so none of the above answers had worked for me, but fortunately I was able to setup a database and get it up and running my own way and I can now make queries and run them successfully, so I will share my method with you in hopes that anyone else scouring the internet can stumble across this and not have to go through the same head scratching that I did.
If you want the quick rundown, just scroll to Step 3 and read on from there. If you're a complete beginner, keep reading as I'll walk you through it in detail.
Couple things to mention:
You will have to setup a database via a Terminal in Cloud 9. I had no experience prior doing it in a Terminal before, but it's very simple to learn.
You can not use mysql functions, you have to use mysqli, since mysql functions are deprecated and Cloud 9 will not run them.
Step 1: Setup MySQL on Cloud 9 (in Terminal)
In your project, open up a New Terminal (click the plus-sign tab above the text editor space, select "New Terminal"). In the terminal, type mysql-ctl start and hit Enter. MySQL will start up in the back, but you won't get any response back in the terminal.
Next, type mysql-ctl cli and hit Enter. You should see some text that starts off as Welcome to the MySQL monitor.... Congrats, you've setup MySQL on your Cloud 9 project.
Step 2: Create a test database (in Terminal)
You can actually go ahead and create your official database if you like, but for this sake I'll just make a database that holds a table that holds an ID and a username. So here's the steps to setting up a database and a table. If you've used MySQL and databases before, then this should be cake, but I'll explain it in detail for those who might not fully understand MySQL .
Type SHOW DATABASES; and hit Enter. This will show a list of current databases within your project. You can enter this any time you want to see a list of your databases on the current project.
Type in CREATE DATABASE sample_db; and hit Enter. You should get a Query OK, 1 Row affected. which means the query was successful. You can name the database whatever you like, but for this little walk-through, I named it sample_db.
Type in USE sample_db; and hit Enter. This selects sample_db from the list of databases.
Type in CREATE TABLE users (id INT(11), username VARCHAR(20));, and hit Enter. This creates a table named users with two columns: id and username. The number in parentheses represents the character limit the column will store in the database. In this case for example, username won't hold a string longer than 20 characters in length.
Type in INSERT INTO users (id, username) VALUES (1, "graham12");, and hit Enter. This will add the id of 1 and a username graham12 in the table. Since the id column is an INT, we do not put quotes around it.
Type in SELECT * FROM users;, and hit Enter. This will show everything that is in the users table. The only entry in there should be what we inserted from the last step we just did.
Step 3: Get the credentials you'll need to connect to the database from PHP. (in Terminal)
Now we have some data in our table that we can test our mysqli connection with. But first, we have to get the credentials we will need to connect to the database in PHP. In Cloud 9, we will need 5 credentials to connect:
Host name
Username
Password
Database name
Port #
Username, password, database name, and port #, are practically already known to you by now. I'll explain:
Host name - Type in SHOW VARIABLES WHERE Variable_name = 'hostname';, and hit Enter. You'll get a table that has 2 columns: Variable_name and Value. In the Value column you should see something like yourUsername-yourProjectName-XXXXXXX, where the X's are a 7 digit number. Write this number down or save it some where. This is your host name. (If you're getting the quick rundown on this walkthrough, just start a new terminal and start up your mysql and select the database you want to use, then type in SHOW VARIABLES WHERE Variable_name = 'hostname';. Re-read this step from the beginning if you're confused.)
Username - Your username that you use to log in to Cloud 9.
Password - There is NO password for your database in Cloud 9.
Database name - This would be sample_db or whatever you named your database;
Port # - is 3306. In Cloud 9, all of your projects are wired to 3306. This is a universal constant of Cloud 9. It will not be anything else. Write this as you would an integer, not as a string. mysqli_connect() will interpret the port # as a long data type.
Last Step: Connect to the database with PHP! (using PHP)
Open up a PHP file and name it whatever you like.
I'll pretend that my host name is graham12-sample_db-1234567 for this example and that this is what my data looks like:
Host name: "graham12-sample_db-1234567"
Username: "graham12"
Password: ""
Database name: "sample_db"
Port #: 3306
So in PHP, insert your credentials accordingly:
<?php
//Connect to the database
$host = "grahamsutt12-sample_db-1234567"; //See Step 3 about how to get host name
$user = "grahamsutt12"; //Your Cloud 9 username
$pass = ""; //Remember, there is NO password!
$db = "sample_db"; //Your database name you want to connect to
$port = 3306; //The port #. It is always 3306
$connection = mysqli_connect($host, $user, $pass, $db, $port)or die(mysql_error());
//And now to perform a simple query to make sure it's working
$query = "SELECT * FROM users";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
echo "The ID is: " . $row['id'] . " and the Username is: " . $row['username'];
}
?>
If you get a result and no error then you have successfully setup a database and formed a connection to it with PHP in Cloud 9. You should now be able to make all the queries you can normally make.
Note: I demonstrated the last part without using parameterized queries for the sake of being simple. You should always use parameterized queries when working with real web applications. You can get more info on that here: MySQLi Prepared Statements.
For starters, the mysql_* functions are deprecated so you shouldn't be using them. Look at PDO or mysqli instead. Next, you'll want to try this per the example docs:
$link = mysql_connect('localhost:/lib/mysql/socket/mysql.sock', 'myUser', '') or die(mysql_error());
To find the ip running you project, create a test file with the code below, run it and put the result as host.
<?php
$ip = getenv("REMOTE_ADDR") ;
Echo "Your IP is " . $ip;
?>
You are using Cloud9 so it's a little different to use. To connect to MySQL you have to first create the MySQL server in C9. Type this in C9's command line:
mysql-ctl start
C9 will create your mysql server.
MySQL 5.1 database added. Please make note of these credentials:
Root User: <username>
Database Name: c9
Next to find your IP address type:
echo $IP
Now use this code with your username, the ip address, no password and the 'c9' database to access MySQL:
mysql_connect("<$IP>","<username>","") or die(mysql_error());
mysql_select_db("c9")
Hope this helps
The documentation show how start, stop, and run the mysql environment.
Start the MySQL shell mysql-ctl start then in yor file.php:
$ip = getenv("REMOTE_ADDR");
$port = "3306";
$user = "YorUsername";
$DB = "c9";
$conn = mysql_connect('$ip', '$user', '', '$db', '$port')or die(mysql_error());
mysql_select_db('$db','$conn')or die(mysql_error());
mysql_query("select * from YourTableName",'$conn')or die(mysql_error());
The line getenv("REMOTE_ADDR") return the same local IP as the application you run on Cloud9.
I have a very simple INSERT statement that I can run using SSMS. When I run the same INSERT statement using PHP mssql_execute or mssql_query SQL profiler shows me that the 'TextData' is identical when sent in by PHP as it is when sent in by SSMS. The problem is when executed via the PHP code, the INSERT never actually happens, whereas when run from within SSMS, it inserts just fine. Any ideas?
MS SQL: 2008 R2
PHP: 5.4, freetds 8.0
PHP error:
PHP Warning: mssql_execute(): message: Internal Query Processor Error: The query processor could not produce a query plan. For more information, contact Customer Support Services. (severity 16) in /home/...
Edit: PHP relevant PHP added
<?php
putenv('FREETDS=/home/###/freetds.conf');
putenv('FREETDSCONF=/home/###/freetds.conf');
$link = mssql_connect('mssqlserver', '###','###'); // connect
if ( !$link ) {
if ( function_exists('error_get_last') ) {
var_dump(error_get_last());
}
die('connection failed');
}
// Create a new statement
$stmt = mssql_init('Db.dbo.SprocName');
// Some values
$a = 1;
$b = 'test';
$c = 'myname';
// Bind values
mssql_bind($stmt,'#a',$a,SQLINT2,false,false);
mssql_bind($stmt,'#b',$b,SQLVARCHAR,false,false,8000);
mssql_bind($stmt,'#c',$c,SQLVARCHAR,false,false,50);
// Execute the statement
mssql_execute($stmt);
// And we can free it like so:
mssql_free_statement($stmt);
?>
The stored procedure (Db.dbo.SprocName) did a simple insert on a table, lets call it (Db.dbo.TableName). When I removed the foreign key from Db.dbo.TableName then the PHP mssql INSERT calls started working.
I realize this technically resolves the issue, but in practice its not acceptable. Those FK's are there for a reason :). Any thoughts?
After a quick search, I haven't find solution of my problem so I post a new one.
So I have to create view in a MySQL database from a Web interface (using PHP).
I use a PEAR framework for the connection with MySQL(5.0.26)
I have the SQL request :
CREATE VIEW test AS SELECT cswc.code_de_la_copie, cswc.service_de_reference, cswc.libelle_de_la_copie, cswc.direction_de_securite_logique
FROM pressi_copiesServiceWithCibles cswc LEFT OUTER JOIN pressi_servicesReferenceWithCibles srwc ON cswc.service_de_reference = srwc.code_du_service
WHERE cswc.cible is null
AND (srwc.cible LIKE '%£DOMAIN£%' OR srwc.cible LIKE '%$DOMAIN$%');
When I execute this request directly on mon local MySQL Database, I obtain a result with 470 lines.
However, when I execute this request in my PHP code, I have a different result (I have 386 line), and I don't know why !
$values['request'] = "SELECT cswc.code_de_la_copie, cswc.service_de_reference, cswc.libelle_de_la_copie, cswc.direction_de_securite_logique
FROM pressi_copiesServiceWithCibles cswc LEFT OUTER JOIN pressi_servicesReferenceWithCibles srwc ON cswc.service_de_reference = srwc.code_du_service
WHERE cswc.cible is null
AND (srwc.cible LIKE '%£DOMAIN£%' OR srwc.cible LIKE '%$DOMAIN$%');";
$baseView = "test";
$sqlView = 'CREATE VIEW '.$baseView.' AS '.$values['request'];
$res =& $this->mdb2->query($sqlView);
if (PEAR::isError($res)) {
return false;
}
Moreover, I have already create 6 views before this one without any problem (same result in PHP and in MySQL)
Thank you for your help
Note that your connection to the database also has a CHARSET and a COLLATION for any string values you include in your query. Although your query looks the same to you in both situations, it must not from the MySQL servers point of view.
Maybe the client CHARSET (and/or COLLATION) differ when you connect via PHP from when you connect via MySQL console.
See the MySQL manual for more information on the client charset and collation.
For comparison, you can use this query:
SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';
I am trying to write a PHP script that plugs into an existing Access Database. If I would be starting from scratch, I would have used MySQL for the job, but because there is an existing MS Access application I am stuck with the database as it is.
As of right now, I am trying to get the following PHP Code to work.
$conn=odbc_connect('buju','','');
if (!$conn)
{
exit("Connection Failed: " . $conn);
}
$sql="SELECT *
FROM Teilnehmer
INNER JOIN TeilnWerte ON Teilnehmer.LfdNr = TeilnWerte.Teilnehmer
WHERE Teilnehmer.Klasse = '$_POST[klasse]'";
$rs=odbc_exec($conn,$sql);
echo "\nErrorCode:\n".odbc_error($conn);
echo "\nErrorMessage:\n".odbc_errormsg($conn);
I am pretty sure, that the problem is in the SQL Query, since it all works fine if I only do
SELECT * FROM Teilnehmer WHERE Klasse = '$_POST[klasse]'
without trying to join the second table.
I am using odbc and the Microsoft Access Driver. The Error Code that I get is 07001. The Error Message is
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
I have also tried
SELECT *
FROM Teilnehmer, TeilnWerte
WHERE Teilnehmer.LfdNr = TeilnWerte.Teilnehmer
AND Teilnehmer.Klasse = '$_POST[klasse]'
which did not work either.
Is there anything that I am doing wrong? Are there certain SQL commands that don't work
Since echo $sql gives you ...
SELECT *
FROM
Teilnehmer
INNER JOIN TeilnWerte
ON Teilnehmer.LfdNr = TeilnWerte.Teilnehmer
WHERE Teilnehmer.Klasse = '06A'
Test that same statement as a new query in Access. Create a query in the query designer, switch to SQL View, paste in the statement and see what happens when you run it.
Most often the cause of "Too few parameters" is a misspelled item (object name, function or SQL keyword). Since the db engine can't find that item, it assumes the item is a parameter. Access will pop up a parameter dialog asking you to supply a parameter value, and that dialog also contains the parameter's name. So it tells you which is the misspelled item.
I have been learning how to program websites lately and the time has come for me to add a database. I have in fact already successfully created a MySQL database and interacted with it with PHP.
My problem is I can't seem to access a SQLite database file with it. I am using MAMP to host locally for now. Here is a snippet of the code I am using to access the db and find and print out a value stored on it.
<?php
$dbhandle = sqlite_open('/Applications/MAMP/db/sqlite/Users');
if ($dbhandle == false) die ('Unable to open database');
$dbquery = "SELECT * FROM usernames WHERE username=trevor";
$dbresult = sqlite_query($dbhandle, $dbquery);
echo sqlite_fetch_single($dbresult);
sqlite_close($dbhandle);
?>
As you have access to the database (your code doesn't die), I'd say there's got to be an error later ;-)
Looking at your SQL query, I see this :
SELECT * FROM usernames WHERE username=trevor
Are you sure you don't need to put quotes arround that string ?
Like this :
SELECT * FROM usernames WHERE username='trevor'
Also, notice that sqlite_fetch_single will only fetch the first row of your data -- which means you might need to use sqlite_fetch_array or sqlite_fetch_object if you want to access all the fields of your resulset.