Database is not being selected in php doc - php

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();
}

Related

GoDaddy can't select database

I'm running this code on GoDaddy webhosting and I'm getting 'The database could not be found' echoed.
Obviously the database in question can't be selected, even though I've privileged the user and checked the db name.
I don't get anything out of here mysqli_error()
$db= 'test2' ;
$con = mysqli_connect('whatever','whatever','whatever') or die ('The connection to the database could not be established.');
mysqli_select_db($db , $con) or die ('The database could not be found' . mysqli_error());
As per the mysqli_select_db documentation, it expects the parameters this way:
mysqli_select_db ( mysqli $link , string $dbname ) : bool
So your parameters are put in backwards, change it to this:
mysqli_select_db($con, $db) ...
Or, alternatively, just select the database inside mysqli_connect().
$con = mysqli_connect('whatever','whatever','whatever', $db) ...
Side note, your die() isn't really doing anything, you won't get an actual error code out of that. To use mysqli_error(), you need to pass your database handle:
die('There was an error: ' . mysqli_error($con));
For the die() that is attached to mysqli_connect(), you should do this:
die('There was an error: ' . mysqli_connect_error());

PHP/MySQL query not working with no error

I have written that seems to not be working, but MySQL does not return any error. It is supposed to get data from database1.table to update database2.table.column
<?php
$dbh1 = mysql_connect('localhost', 'tendesig_zink', 'password') or die("Unable to connect to MySQL");
$dbh2 = mysql_connect('localhost', 'tendesig_zink', 'password', true) or die("Unable to connect to MySQL");
mysql_select_db('tendesig_zink_dev', $dbh1);
mysql_select_db('tendesig_zink_production', $dbh2);
$query = " UPDATE
tendesig_zink_dev.euid0_hikashop_product,
tendeig_zink_production.euid0_hikashop_product
SET
tendesig_zink_dev.euid0_hikashop_product.product_quantity = tendesig_zink_production.euid0_hikashop_product.product_quantity
WHERE
tendesig_zink_dev.euid0_hikashop_product.product_id = tendesig_zink_production.euid0_hikashop_product.product_id";
if (mysql_query($query, $dbh1 ))
{
echo "Record inserted";
}
else
{
echo "Error inserting record: " . mysql_error();
}
?>
The manual page for mysql_error() mentions this about the optional parameter you're omitting:
link_identifier
The MySQL connection. If the link identifier is not
specified, the last link opened by mysql_connect() is assumed. If no
such link is found, it will try to create one as if mysql_connect()
was called with no arguments. If no connection is found or
established, an E_WARNING level error is generated.
So it's reading errors from $dbh2, which is the last connection you've opened. However, you never run any query on $dbh2:
mysql_query($query, $dbh1 )
Thus you get no errors because you are reading errors from the wrong connection.
The solution is to be explicit:
mysql_error($dbh1)
As about what you're trying to accomplish, while you can open as many connections as you want, those connections won't merge as you seem to expect: they're independent sessions to all effects.
All your tables are on the same server and you connect with the same users, there's absolutely no need to even use two connections anyway.
You can't just issue a cross-database update statement from PHP like that!
You will need to execute a query to read data from the source db (execute that on the source database connection: $dbh2 in your example) and then separately write and execute a query to insert/update the target database (execute the insert/update query on the target database connection: $dbh1 in your example).
Essentially, you'll end up with a loop that reads data from the source, and executes the update query on each iteration, for each value you're reading from the source.
I appreciate everyone's help/banter, here is what finally worked for me.
<?php
$dba = mysqli_connect('localhost', 'tendesig_zink', 'pswd', 'tendesig_zink_production') or die("Unable to connect to MySQL");
$query = " UPDATE
tendesig_zink_dev.euid0_hikashop_product, tendesig_zink_production.euid0_hikashop_product
SET
tendesig_zink_dev.euid0_hikashop_product.product_quantity = tendesig_zink_production.euid0_hikashop_product.product_quantity
WHERE
tendesig_zink_dev.euid0_hikashop_product.product_id = tendesig_zink_production.euid0_hikashop_product.product_id";
if (mysqli_query($dba, $query))
{
echo "Records inserted";
}
else
{
echo "Error inserting records: " . mysqli_error($dba);
}
?>

My php code is connecting to my mysql database but will not add or access any data

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

PHP - Selecting database from MySQL doesn't work

I have read a lot about this but it still doesn't work.
I'm just trying to select a database to create a new table in, I try:
$db = mysqli_select_db("test");
if(!$db) {
echo "error: " . mysqli_error($db);
}
But I still get an error (and mysqli_error($db) doesn't seem to work).
Of course I have already connected to it:
$con=mysqli_connect("localhost", "administrator", "****");
On phpMyAdmin I have these databases:
Why can't I select "test" ?
And creating a database doesn't work because I don't have the rights, as you can see.
The procedular signature of this function is:
bool mysqli_select_db ( mysqli $link , string $dbname )
So you will have to provide the resource you got back from the mysqli_connect() to make it work. Something like this:
$con = mysqli_connect("localhost", "administrator", "****");
$success = mysqli_select_db($con, "test");
Alternatively you could specify the database on the connect call with a 4th argument:
$con = mysqli_connect("localhost", "administrator", "***", "test");
See the examples on mysqli_connect().
mysqli_select_db function requires two parameters link and dbname. Please refer to the documentation:
http://php.net/manual/en/mysqli.select-db.php
You are only passing link and no database name in your call:
$db = mysqli_select_db("test");

How do I create a database if it doesn't exist, using PHP?

How do I create a database if it doesn't exist, using PHP?
Presuming you're talking about a MySQL database - you want to use mysql_query and mysql_select_db.
Note that mysql_create_db is deprecated.
<?php
// Connect to MySQL
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Make my_db the current database
$db_selected = mysql_select_db('my_db', $link);
if (!$db_selected) {
// If we couldn't, then it either doesn't exist, or we can't see it.
$sql = 'CREATE DATABASE my_db';
if (mysql_query($sql, $link)) {
echo "Database my_db created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
}
mysql_close($link);
?>
Since you mention WAMP I'll assume you're talking about MySQL.
It can be tricky. Assuming that your PHP script runs with all the required credentials (which is by itself a questionable idea), you can run this query:
SHOW DATABASES
If the DB does not show up there, you can assume it doesn't exist and create it with one of these queries:
CREATE DATABASE foo ....
or:
CREATE DATABASE IF NOT EXISTS foo ...
Right after that, you need to check the return value for whatever PHP function you are using (e.g. mysql_query). The above queries will fail if your user is now allowed to see all the existing databases or it's not allowed to create new databases.
In general, I find the whole concept kind of scary. Handle it with care! ;-)

Categories