For some reason I am getting the error
No database selected
I have no idea why this is not working, am I being blind?
$con = mysql_connect('localhost','myusername','mypassword');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydatabase');
There are no typos in the actual username, password or database name and I can't see a mistake in my code.
Suggestions are very much appreciated.
Thanks
Do you have username added to the user table? What if you try the same code with root user?
try:
mysql_select_db('mydatabase', $con);
in stead of:
mysql_select_db('mydatabase');
Related
Hi so i have a database 'guestbook' and created it using phpmyadmin however i am struggling with selecting this database within my php doc.
<?php
$link=mysqli_connect('localhost', 'root', 'password');
if(!$link){
die('Not connected: ');
}
$db_selected=mysqli_select_db($link, 'guestbook');
if(!$db_selected) {
die("Can't use guestbook : ");
}
?>
It seems to connect properly however it returns "Can't use guestbook : ". Any thoughts would be greatly appreciated
You only specified the host in mysqli_connect.
$link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));
Can you let me know is 'guestbook' a database or a table. If it is table then it will give you the error. As mysqli_select_db expects a database name not the table name.
Hope this helps.
You can check your connection before set database by adding after $link=mysqli_connect('localhost', 'root', 'password');
:
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Here is the code:
<?php
$con=mysql_connect('localhost', 'itorras', 'passwordhere');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db( "my_db" ) or die( 'Error'. mysql_error() );
$sql="INSERT INTO Brackets(have things here)
VALUES(and here)";
if (!mysqli_query($con,$sql))
{
die('Error with adding row: ' . mysqli_error($con));
}
echo "Thank you, your bracket has been submited.";
echo "<a href='index.html'>Click here to go back to home page</a>";
?>
I am using the username and password that is given the rights to mess with the database.
So when i try to submit something into the file none of the top errors run but then the bottom error prints error with adding row and nothing more. This file worked fine on my local server but has not worked on the web host. I am using godaddyweb hosting, so phpmyadmin and cpanelx.
If you need any more info let me know. Have been at this for a couple hours.
You are using mysql_connect but mysqli_query. You are mixing mysql and mysqli. Use only mysqli_*.
It appears you are using two different libraires at the same time
The original mysql API is deprecated for various reasons and you should not use it in new code
Instead, use PDO or mysqli
In your code, you are using the original mysql for the db connect, and then you use mysqli for the query. Instead you should only use mysqli
Replace the following code :
$con=mysql_connect('localhost', 'itorras', 'passwordhere');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
With :
$db = new mysqli('localhost', 'user', 'pass', 'demo');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
Then you can query the database with something similar :
$sql = <<<SQL
SELECT *
FROM `users`
WHERE `live` = 1
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
For a complete overview, you can take a look at the documentation
http://ca2.php.net/mysqli
My code doesn't insert any records to mysql. What is wrong? I am really confused.
I have designed a form and I want to read data from text box and send to the database.
<?php
if(isset($_post["tfname"]))
{
$id=$_post["tfid"];
$name=$_post["tfname"];
$family=$_post["tffamily"];
$mark=$_post["tfmark"];
$tel=$_post["tftell"];
$link=mysql_connect("localhost","root","");
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("university",$link);
$insert="insert into student (sid,sname,sfamily,smark,stel) values ($id,'$name','$family',$mark,$tel)";
mysql_query($insert,$link);
}
mysql_close($link);
?>
You'd better to put quotation mark for id, mark and tel after values in your query. Also as #Another Code said, you must use $_POST instead of $_post in your code. Try this and tell me the result:
<?php
if(isset($_POST["tfname"])) {
$id=$_POST["tfid"];
$name=$_POST["tfname"];
$family=$_POST["tffamily"];
$mark=$_POST["tfmark"];
$tel=$_POST["tftell"];
$link=mysql_connect("localhost","root","");
if (!$link) {
die('Could not connect: ' . mysql_error());
} else {
mysql_select_db("university",$link);
$insert="insert into student
(sid,sname,sfamily,smark,stel) values
('$id','$name','$family','$mark','$tel')";
mysql_query($insert,$link) or die (mysql_error());
mysql_close($link);
}
} else {
die('tfname did not send');
}
?>
Use mysql_query($insert,$link) or die (mysql_error()); to fetch the error message.
With the code you've provided it could almost be anything - to do some tests... have you echo'd something to confirm you are even getting the tfname in POST? Does it select the database fine? Do the fields $id, $mark, and $tel need single quotes around them as well? We need to know more about where the code is not working to provide more help but that snippet appears as though it should be running, in the interim, please use some echo's to narrow down your problem!
Try to run the generated sql query in the sql query browser. Get the query by "echo $insert" statement.
change the $insert to:
$insert="insert into student (sid,sname,sfamily,smark,stel) values ($id,'".$name."','".$family."','".$mark."','".$tel."')";
further set ini_set('display_errors',1) so that php displays the error messages as required.
Lastly, whenever doing a mysql query, try to use or die(mysql_error()) in the query so that if somethings wrong with mysql or syntax, we are aware.
$q = mysql_query($query) or die(mysql_error());
I've seen a few errors like this, but I found no answer.
Unable to connect to database: Access denied for user ''#'localhost' to database 'socialdb'
socialdb is my database. The "Unable to connect to database:" part is located here.
$db = mysql_select_db("socialdb",$con);
if(!$db) {
die ('Unable to connect to database: ' . mysql_error());
}
I don't know what's causing this. Here are my mysql_connect details
<?php
$con = mysql_connect("localhost");
if(!$con) {
die ('Error: ' . mysql_error());
}
I need to find the root. Thanks.
I DON'T HAVE A USERNAME OR PASSWORD FOR MySQL
Should it be this?
mysql_connect("localhost","","");
The error is pretty self-explanatory, you're not allowed to connect without specifying some credentials.
Change your call to mysql_connect() to something like this:
mysql_connect("localhost", "user", "password");
Your default credentials are most likely:
$con = mysql_connect("localhost","root","");
You're missing username and password parameter, should be:
$con = mysql_connect("localhost","username","password");
You did not specify a user in neither mysql_connect or your PHP configuration.
Either set mysql.default_user and mysql.default_password in your PHP configuration or use the appropriate arguments for mysql_connect.
mysql_connect takes 3 parameters
$con = mysql_connect("localhost", "dbuser", "password");
even if you are not having a user. there exist 'root' user
so use it
$con = mysql_connect('localhost','root','');
I run this function and the value of the post form doesn't insert into the DB. I'm not getting any error messages but I checked and saw that $this->user_id is populating. Am I missing something else?
function senddata () {
$con = mysql_connect("localhost","root","XXXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("user", $con);
$sql="INSERT INTO profile (collegemajor) VALUE('$_POST[major]') WHERE userid=$this->user_id";
mysql_close($con);
}
INSERT statement don't have WHERE clausule,
you need to send query!!!
First of all, I don't see the mysql_query() - the function to execute the query.
It seems like the problem is here:
$sql="INSERT INTO profile (collegemajor) VALUE('$_POST[major]') WHERE userid=$this->user_id";
Use $sql="INSERT INTO profile (collegemajor) VALUE('$_POST[major]'); instead.
The code:
function senddata () {
$con = mysql_connect("localhost","root","XXXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("user", $con);
$sql="INSERT INTO profile (collegemajor) VALUE('$_POST[major]');";
mysql_query( $sql , $con );
mysql_close($con);
}
This should be an update function, it seems, rather than an insert function (if you're trying to update a row that already exists, which is seems you are based on the "Where" clause).
And, as others have mentioned, you never ran mysql_query() or similar function.