I'm having problems with this php code which needs to create a table in the database specified by the user. But whenever I try to execute the SQL it tells me no database selected. My code is as follow
<?php
$con = mysql_connect("127.0.0.1","peter")
or die('Error connecting to mysql'); // Check connection
// Create table
mysql_select_db("USE Ebooks");//Select Database
$foldername = mysql_real_escape_string($_POST['foldername']);//Obtain Folder Name
$sql = sprintf("CREATE TABLE %s (ID CHAR(3) ,Books CHAR(30))", $foldername);
mysql_query($sql) or die(mysql_error());
mysql_close($con);
?>
Use
mysql_select_db( "Ebooks" ) or die( 'Error'. mysql_error() );
Use this code:
mysql_select_db("Ebooks");//Select Database
Related
My PHP code
$handle = mysql_connect();
echo ($handle) ? "Connected to mysql" : "Could not connect";
$db = mysql_select_db("users");
if ($db ==false) {
die(' Could Not select database so Exiting;');
} else {
echo " Database selected;";
}
mysql_close($handle);
This code works with default mysql databases like "test" or "mysql".
My Data Base creation code sequence:
mariadb[none]> create database users;
mariadb[none]> use users;
mariadb[users]> create table registered_users ( user_name varchar(18), user_pass varchar(18) );
You should use mysqli_ functions instead:
$localhost=''; //SQL Host
$dbuser=''; //Database Username
$dbpass=''; //Database Password
$dbname=''; //Database Name
$connect=mysqli_connect($localhost,$dbuser,$dbpass,$dbname);
mysql_ functions are deprecated.
What's basically happening is you are connected to test by default please supply parameters mysql_connect("localhost", "username", "password") ;
Should work with no doubt.
I am trying to delete a record using php from a database. This is supposed to happen when I click a button, no error is displayed and the query appears on the screen but the record remains on the database
phpmyadmin gives me the following code to use: DELETE FROM 'the shop'.'customer' WHERE 'customer'.'CustomerID' = 8
<?php
$host="localhost"; // Host name
$tbl_name="customer"; // Table name
$db_user="root";
$db_pass="";
$connect = mysql_connect("$host", "$db_user", "$db_pass");
$db_name="the_shop"; // Database name
mysql_select_db("$db_name");
if (!$connect)
{
die("MySQL could not connect!");
}
if(isset($_GET['submit2'])){
$db_username = $_GET['username'];
$sql4 = "DELETE FROM 'the_shop'.'customer' WHERE 'customer'.'CustomerID' = 8"
or die('error deleting record');
mysql_query($sql4);
echo $sql4;
}
?>
I know this will only delete the record that has a CustomerID that = 8
my intention is that once this works I will replace CustomerID with Username and the '8' with the relevant variable that will be given a value via a form
any help is appreciated
You are using quotes instead of back tick
$sql4 = "DELETE FROM `the_shop`.`customer` WHERE `customer`.`CustomerID` = 8";
Moreover you don't need back ticks(In this case as you are not using any Reserved keywords here) as well as you are using die() at wrong place
Use this,It is working.
<?php
$host="localhost"; // Host name
$tbl_name="customer"; // Table name
$db_user="root";
$db_pass="";
$connect = mysql_connect("$host", "$db_user", "$db_pass");
$db_name="the_shop"; // Database name
mysql_select_db("$db_name",$connect);
if (!$connect)
{
die("MySQL could not connect!");
}
if(isset($_GET['submit2'])){
$db_username = $_GET['username'];
$sql4 = "DELETE FROM `the_shop`.`customer` WHERE `customer`.`CustomerID` = 8";
mysql_query($sql4,$connect) or die('error deleting record');
echo $sql4;
}
?>
Your statement is not correct. You use quoted instead of back ticks. But you can make your statement easier.
$sql4 = "DELETE FROM customer WHERE CustomerID = 8";
$sql4 = "DELETE FROM `the_shop`.`customer` WHERE `customer`.`CustomerID` = 8"
mysql_query($sql4);or die('error deleting record');
echo $sql4;
You don't need to specify which database to query in your query.
This will suffice:
DELETE FROM customer WHERE CustomerID = 8
The Mysql extension is deprecated. This means that it is no longer supported by PHP and should not be used. Try mysqli or pdo instead.
You can just use this. There is no need for you to specify the database.
delete from customer where CustomerID = 8
Thanks for taking a look:
Here is the php I'm using to insert the data into the table
<?php
session_start();
//sets a variable from a session value
if (isset($_SESSION['sv_01'])) {$sv_01=$_SESSION['sv_01'];} else {$sv_01="";}
//to test that the variable has been set and is not empty
echo $sv_01;
//define database log in stuff
$username="username123";
$password="password123";
$database="database01";
$table="my_table";
$dbaddress="123.123.123.123";
//connect to dbserver
$con=mysql_connect($dbaddress,$username,$password);
if (!$con)
{
die('Could not connect:' .mysql_error());
}
//select the db
mysql_select_db($database) or die( "Unable to select database");
//insert data from variables
mysql_query("INSERT INTO $table
(
$sv_01
)
VALUES
(
'$sv_01'
)");
mysql_close($con);
?>
I run this, and then go to check out the contents of the DB. Using MySQL workbench I open the connection and the database and table in question, select all rows and there is no data contained in the table.
MySQL info:
Collation: latin1 - default
collation Engine: MyISAM
datatype: sv_01 VARCHAR (255)
default: NULL
Any ideas what I am doing incorrectly?
I believe that the name of the field is sv_01 not $sv_01
I would try:
$query = "INSERT INTO $table (sv_01) VALUES ('$sv_01')";
Update (dedicated to tadman):
A small piece of advice: DO NOT use mysql_query
Use localhost insted af your IP (if possible), and make your connection easy to read:
$con=mysql_connect($dbaddress,$username,$password) OR DIE mysql_error();
AND you also have to give you mysql_query a variable:
$mysql = mysql_query("INSERT INTO $table ($sv_01) VALUES ('".$sv_01."');");
:)
I am trying to get the top 10 items from a table:
<?php
include DBConnect.php;
$dbname = 'Telejoke';
mysql_select_db($dbname);
$query = "SELECT * FROM jokes LIMIT 10";
$data = mysql_query($query) or die('Error, insert query failed');
mysql_close($conn);
$info = mysql_fetch_array( $data );
?>
The PHP script keeps executing the die part saying that my query insert failed.
UPDATE:
The error is No connection could be made because the target machine actively refused it.
UPDATE 2:
I think the user I connect to DB is not authorized to use the SELECT command. This would cause the preceding error?
In db connection.php you must use a user name with password who is allowed to do operations on db.
I'm trying to truncate the table named 'comments' Yet every time I run the script nothing happens. What is wrong with my syntax?
<?php
$con = mysql_connect("localhost","user","pass");
if (!$con)
mysql_select_db("dbname", $con);
mysql_query("TRUNCATE TABLE 'comments'");
mysql_close($con);
?>
The quotes, try changing it to:
$result = mysql_query("TRUNCATE TABLE `comments`");
if ( !$result ) print(mysql_error());
This will also tell you what goes wrong.
you seem to select the database "dbname" only if the connection to the mysql server failed ( $con == FALSE ).
you probably need to change your if condition to:
if ( $con )
mysql_select_db("dbname", $con);