Can't seem to find any solution to this, no matter how hard I google. I have a deadline tomorrow and need this to be resolved asap, hope that someone here can give me a hint :)
Basically I'm connecting to a MSSQL-server from PHP. Like so:
$db_server = 'servername';
$db_user = 'user';
$db_pwd = 'pwd';
$db_db = 'dbname';
$link = mssql_connect($db_server, $db_user, $db_pwd);
if (!$link || !mssql_select_db($db_db, $link)) {
die('Something went wrong while connecting to MSSQL');
}
After that I try to run this code:
mssql_query("CREATE TABLE ##TempUserTable (UserID VARCHAR(30))", $link);
And then I get this error:
Warning: mssql_query() [function.mssql-query]: Query failed in /var/www/test.php on line 4
And yes, I've fixed the serverstuff in freetds.conf and it should be able to create a temporary table. It worked before and it's working from Eclipse / SQLExplorer.
I'm getting frustrated!
Thanks in advance! :)
Related
I'm relatively new to any type of programming or coding. I'm not quite understanding why no matter what adjustments I make to my php file I can't seem to pull any data from a table.
Here is a link to the table: https://i.gyazo.com/4ad5e860895014c49dbe0539c38cdec2.png
Above is the test table I have been trying to use. From what I can understand I'm connecting to the database okay, but all of my problems come after the connection. Also, I'm using php 7.0 so a lot of the information I'm finding online has not been helpful.
If there is something glaringly wrong with my table or in my code, please let me know.
Here is my code:
'''
//Set Variables
$serverName = "localhost";
$userName = "root";
$password = "";
$databaseName = "test";
//Create Connection
$connection = mysqli_connect($serverName,$userName,$password,$databaseName);
//Check Connection
if(!$connection){
die("Connection failed: ".mysqli_connect_error());
}
echo "Connected successfully <br>";
//Fetch Data
$query = "SELECT * from table1";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_array($result, MYSQLI_NUM);
printf ($row[1], $row[2]);
mysqli_free_result($result);
mysqli_close($connection);
I figured out the issue a few hours ago. The code I had posted would have worked perfectly fine if I was connecting to the port for MySQL rather than MariaDB. Didn't realize that the port that MariaDB was connected to was the default.
MariaDB by default was port 3306, but MySQL was 3308. After specifying 'localhost:3308' I was able to start properly pulling rows from my tables.
I have a PHP Code to insert some information from a large text file
in the beginning of the file i have this code:
<?php
$host = "localhost";
$username = "user";
$password = "pass";
$db = "dbname";
mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_close;
set_time_limit(0);
$fh = fopen("file.txt", "r");
while ($row = fgets($fh)) {
//// CODE ....
mysql_query("INSERT IGNORE INTO `TABLE` VALUES (NULL, '$ETC', '$ETC', '$ETC')");
}
fclose($fh);
?>
but after some iserts i got this error :
Warning: mysql_connect(): User co_p already has more than 'max_user_connections' active connections in /home/username/public_html/file.php on line 7
User co_p already has more than 'max_user_connections' active connections
any solution for this problem ?
I'm the OWNER of the SERVER !
mysql_close;
should be generating an error.
This command should be
mysql_close();
Add error reporting to the
top of your file(s) while testing right after your opening PHP tag for example
<?php error_reporting(E_ALL); ini_set('display_errors', 1); to see if it yields anything.
But I have to ask, why are you connecting to the database and then closing the connection straight after?
After more code added.
As you are actually trying to access the database, you dont want to do a mysql_close(); where you have coded it.
But I have to add:
Every time you use the mysql_
database extension in new code
this happens
it is deprecated and has been for years and is gone for ever in PHP7.
If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions and prepared statements.
Start here
Try to close mysql connection:
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
....
mysql_close($link);
I am trying to connect to a simple database on XAMPP using php- I know the database exists as I can see it on PHPMyAdmin and have created a table called students and added some data.
I have tested that I can run a simple test.php file ( from the htdocs folder on the XAMPP drive) and get a response. I cannot spot what is stopping me connecting to my database- can anyone help?
<?php
// connect to the database
$user_name = "root";
$password = "";
$database = "computing";
$host_name ="localhost";
$con=mysql_connect($host_name,$user_name,$password);
mysql_select_db($database);
//check connection
echo "Connection opened";
mysql_close($con);
?>
Could you please try the following code if it works?
<?php
// connect to the database
$user_name = "root";
$password = "";
$database = "computing";
$host_name = "localhost";
$con = mysqli_connect($host_name ,$user_name ,$password,$database) or die("Error " . mysqli_error($con));
//check connection
echo "Connection opened";
mysql_close($con);
?>
mysql commands are not going to be supported in future releases, so it would be best perhaps to use mysqli or PDO connections.
Also PDO uses parameters (the syntax might take a bit to make sense), so it is great to reduce risk from SQL Injections.
Mysqli: http://php.net/manual/en/function.mysqli-connect.php
PDO: http://php.net/manual/en/class.pdo.php
The code above should work. Maybe try mysql_select_db($database, $con);
For some reason, the following code inside the query works in my MySQL command console, yet when I try to run it as a Query in PHP, something keeps going wrong and I'm not sure what. Here is the code I've done so far.
//2. Perform database query
$query = "SELECT skills.element_id, content_model_reference.element_id, element_name FROM skills, content_model_reference WHERE (skills.element_id = content_model_reference.element_id)";
$result = mysql_query($query);
//Tests if there was a query error
if(!$result){
die("Database query failed.");
}
Is there something preventing the code that worked in MySQL (The line with SELECT) from working, or is my syntax somehow wrong?
EDIT: So it's saying I didn't select a database. Yet I thought I had. Here is the code above it:
//1. Create a database connection
$dbhost = "host"; //Host: Can be either an IP address, or a domain (like google.com).
$dbuser = "user";//User: The user that is connecting to the database.
$dbpass = "pass";//Password: This is the password that the user is using.
$dbname = "db";//Name: This is the name of the database.
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);//The value, 'handle,' is the connection.
//Test if connection occurred. Die ends the program/php, and in this case, also prints a message
if(mysqli_connect_errno()){
die("Database connection failed: ".
mysqli_connect_error().
" (". mysqli_connect_errno() . ")"
);
}
Like I said, the error message I am getting is pertaining only to the query, the server is fine with my database connection.
You're using mysqli_* for the connection, but you're using mysql_* for the QUERY... don't think you can do that, has to be one or the other (MYSQLI_ preffered). Also the query should be:
$result = mysqli_query($connection,$query);
Forgive me if this is a dumb question but this is really frustrating.
Im having issues trying to connect to my MySQL database. The details are correct as i can login via phpmyadmin and terminal, the database was created with the user whose details i used in this script but i keep getting 'Cannot connect to database' Its been bugging me and frustrating me for hours now lol.
<?php
$host = "localhost";
$username = "root";
$pass = "password";
$name = "database";
$connect = mysqli_connect($host,$user,$pass);
if ($connect){
$select = mysqli_select_db($name);
if ($select){
echo "Connected successfully.";
} else {
echo "Could not connect to database";
}
} else {
echo "Could not connect to MySQL";
}
?>
The script this is for is working but its not progressing because it cant insert the information to the database to continue with the rest of the script.
I have looked at other posts on here and none of the solutions provided worked, so i am posting this.
Please help!
you are missing a parameter in mysqli_select_db it takes 2 parameters like this mysqli_select_db($connect,$name);
http://php.net/manual/en/mysqli.select-db.php
it can't work because it's not correct drop the little (i) of mysqli_connect
and make sure details are correct and you have a database named "database"
and try again
googluck