I am using XMAPP and MySQL is running as it should. In phpMyAdmin, I don't really get the point so I tried creating one in PHP. With this code it tells me the database benutzer. benutzer doesn't exists although I created one in phpMyAdmin and in this code:
<?php
$connect = mysql_connect("127.0.0.1","root","") or die ("Alles scheisse!");
$db = mysql_select_db("benutzer") or die ("Keine benutzer!");
$sql = "CREATE TABLE 'benutzer'
id VARCHAR(100),
name VARCHAR(100),
mail VARCHAR(100),
points INT(1000)
)";
$sql = "INSERT INTO benutzer(`id` , `name` , `mail` , `points`)VALUES(NULL , 'Liam', 'liam#mail.com', '100');";
$db_erg = mysql_query($sql)
or die("Anfrage fehlgeschlagen: " . mysql_error());
?>
Have you also created a table "benutzer" inside db "benutzer"? Your code does not execute your first SQL statement "CREATE TABLE.." so there will be no table "benutzer" if you don't have one created in phpmyadmin.
Did you created the database under the root user? Because you are connecting to the root in your code.
Related
So it seems that when I run the file register.php (which contains some basic html forms to send to a database) it cannot find the specific database that it is looking for. All files are with in the same folder, and the spelling is correct.
So I will paste the php code that is above all the html/css code in my register.php
<?php
$link=mysql_connect("localhost","root","");
$database='salesinformation';
if (!$link)
die('Failed to connect to Server'.mysql_error());
$db=mysql_select_db($database, $link);
session_start();
if(!$db)
die('Failed to select Data Base '.mysql_error());
if(isset($_GET['process']))
{
$query = "Insert INTO `sales` (Username, Email, Price, Condition, RegisterDate) values('$_POST[Username]', '$_POST[Email]','$_POST[Price]','$_POST[Condition]','$_POST[RegisteredDate]')";
//echo $query; exit;
$result = mysql_query($query) or die(mysql_error());
if(!$result){
$msg = "not Inserted";
}
else
{
$msg = "Inserted";
header("location:ClientList.php?m=".$msg);
}
}
?>
And I have the database file salesinformation.sql in the same folder which contains this code..
CREATE TABLE IF NOT EXISTS `sales` (
`Username` varchar(25) NOT NULL,
`Email` varchar(25) NOT NULL,
`Price` int(10) NOT NULL,
`Condition` varchar(25) NOT NULL,
`RegisterDate` date NOT NULL,
PRIMARY KEY (`Username`)
);
So this happens when I run xampp "http://localhost/register.php"
Failed to select Data Base Unknown database 'salesinformation'
And I have the database file salesinformation.sql in the same folder which contains this code..
That does not mean you have a MySQL database.
1) Create a MySQL database named salesinformation.
2) Import your SQL file into that database.
3) Now you have one. Now run your code.
Could you write what to type in the terminal (Since I cant find any GUI of the xampp while running it in ubuntu)? How do I create the "MySQL" database? and how to I import a SQL file into it?
Using terminal, type CREATE DATABASE salesinformation;
Using shell cd go to the directory where your salesinformation.sql file resides.
Issue this command mysql -u root -p password salesinformation < salesinformation.sql.
Note: Since I noticed from your code your root password is blank, you can remove the password term from the above command. If you do have a password type it there then
I have database contain table named demnads
table contain 11 cell first one is id auto increasement
I want to add data's with this sql command in php :
<?php
$hostname_mystore = "localhost";
$database_mystore = "mystore";
$username_mystore = "root";
$password_mystore = "";
$mystore = mysql_pconnect($hostname_mystore, $username_mystore, $password_mystore) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_query("set character_set_server='utf8'");
mysql_query("set names 'utf8'");
if (mysqli_connect_errno())
{ echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= "INSERT into demands ( itemname , case , cname , fatora , client , cdate , ctime , prices , cmobile , numbers) values('$item[$i]','$cases[$i]','$customer[$i]','$fatora[$i]','$client[$i]','$dates[$i]','$times[$i]','$price[$i]','$mobile[$i]','$numbers[$i]' )";
?>
but data's didn't be inserted
You have switched from the mysql library (which is deprecated) to mysqli:
mysqli_connect_errno()
You should use one or the other.
Also, embedded array values need to be enclosed in curly brackets{$item[$i]}.
This assumes, of course, that these arrays and $i are defined elsewhere.
And you haven't shown the statement that actually inserts the data.
You forgot to call mysql_query( $sql, $conn )
Enclose your column names with backticks. case is a keyword within MySQL and as such can not be used as a column name without the backticks!
$sql= "INSERT into demands ( `itemname` , `case` , `cname` , `fatora` , `client` , `cdate` , `ctime` , `prices` , `cmobile` , `numbers`) values('$item[$i]','$cases[$i]','$customer[$i]','$fatora[$i]','$client[$i]','$dates[$i]','$times[$i]','$price[$i]','$mobile[$i]','$numbers[$i]' )";
You said your table name is demnads . But in Coding you used demands?
Check again.
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
I'm getting an error 'undeclared variable: temp' when I run this...
<?php
$maketemp = "CREATE TEMPORARY TABLE temp(`itineraryId` int NOT NULL, `live` varchar(1), `shipCode` varchar(10), `description` text, `duration` varchar(10), PRIMARY KEY(itineraryId))";
mysql_query( $maketemp, $connection ) or die ( "Sql error : " . mysql_error ( ) );
$inserttemp = "SELECT live, id AS itineraryId, ship AS shipCode, description AS description, duration AS length FROM cruises WHERE live ='Y' INTO temp";
mysql_query( $inserttemp, $connection ) or die ( "Sql error : " . mysql_error ( ) );
$select = "SELECT intineraryId, shipCode, description, duration FROM temp";
$export = mysql_query ( $select, $connection ) or die ( "Sql error : " . mysql_error( ) );
Any ideas ?
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
This code should work:
<?php
$maketemp = "
CREATE TEMPORARY TABLE temp_table_1 (
`itineraryId` int NOT NULL,
`live` varchar(1),
`shipCode` varchar(10),
`description` text,
`duration` varchar(10),
PRIMARY KEY(itineraryId)
)
";
mysql_query($maketemp, $connection) or die ("Sql error : ".mysql_error());
$inserttemp = "
INSERT INTO temp_table_1
(`itineraryId`, `live`, `shipCode`, `description`, `duration`)
SELECT `id`, `live`, `ship`, `description`, `duration`
FROM `cruises`
WHERE `live` = 'Y'
";
mysql_query($inserttemp, $connection) or die ("Sql error : ".mysql_error());
$select = "
SELECT `itineraryId`, `shipCode`, `description`, `duration`
FROM temp_table_1
";
$export = mysql_query($select, $connection) or die ("Sql error : ".mysql_error());
I guess you are going to do more stuff with the temporary table, or are just playing with it, but if not be aware that the whole code could be summed up with:
<?php
$query = "
SELECT `id` AS 'itineraryId', `ship`, `description`, `duration`
FROM `cruises`
WHERE `live` = 'Y'
";
$export = mysql_query($query, $connection) or die ("Sql error : ".mysql_error());
I like to use heredoc to help me construct embedded sql query (just to help make any subtle error glaring); so your first query would look something like this:
$maketemp =<<<s
CREATE TEMPORARY TABLE temp(
`itineraryId` int NOT NULL,
`live` varchar(1),
`shipCode` varchar(10),
`description` text,
`duration` varchar(10),
PRIMARY KEY(itineraryId));
s;
Then if you want to correct the 2nd query without listing the fields of the table you want to insert the records, you have to list the fields in the same order.
Just the query this time:
INSERT INTO temp
SELECT id, live, ship, description, duration
FROM cruises
WHERE live = 'y';
And last thing about temporary variable is this: Check out the part about its visibility.
You can use the TEMPORARY keyword when creating a table. A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed.
http://dev.mysql.com/doc/refman/5.5/en/create-table.html
What that means is this: when you're connected to MySQL directly, e.g. through a command line interface like this:
mysql> #our query here line-by-line
Then you're essentially on the same connection through all of your multiple queries as long as your session is active.
But in an external script (like PHP, for example), just because it's on the same script file doesn't necessarily mean it's the same connection so that by the time you execute your insert query, your temp table is not visible to that connection session.
Try to concat all the queries, send it all within a single command/query execution.
Good luck.
The second query is not correct.
From the reference -
MySQL Server doesn't support the SELECT ... INTO TABLE Sybase SQL
extension. Instead, MySQL Server supports the INSERT INTO ... SELECT.
standard SQL syntax, which is basically the same thing.
Try this one instead -
INSERT INTO temp
SELECT live
, id AS itineraryId
, ship AS shipCode
, description AS description
, duration AS length
FROM
cruises
WHERE
live = 'Y'
I'm writing a upgrader for a mysql database using PHP. The behavior of the upgrader should be as follows.
If all the queries executed successfully the changes should be committed.
If a sinngle query get faild eveything should be roled back to previouse state.
Part of my program is as follows.
$host = 'localhost';
$user = 'root';
$password = 'root';
$db = 'transaction';
$con = mysqli_connect($host, $user, $password);
mysqli_select_db($con, $db);
mysqli_autocommit($con, FALSE);
$query1 = "create table `status` (
`id` int not null auto_increment,
`name` varchar(60) not null,
primary key (`id`)
) engine=innodb default charset=utf8;";
$result1 = mysqli_query($con, $query1);
$query2 = "ALTER TABLE status
CHANGE name value varchar(512);";
$result2 = mysqli_query($con, $query2);
if(!($result1 && $result2)) {
mysqli_rollback($con);
} else {
mysqli_commit($con);
}
mysqli_close($con);
But if the 'status' table already exists the first create table query is failing. So both queries should be rolled back. But the alter query has executed and not rolled back.
I saw a post which list all the queries which cannot be rolled back in mysql. http://www.sitepoint.com/mysql-transaction-gotchas-good-parts/
Is there any possible way to do this role back in mysql.
No. You would need to run a new alter table query undoing your previous alter statement.
do it manualy
if(!($result1 && $result2)) {
#drop table
$query1 = "drop table `status`";
$result = mysqli_query($con, $query1);
}
Would it be better to just export the data into (say) a collection of CSV files. Then modify any dataif needed to match the new structure. Then just create the database with the new structure and import the data into it.
Seems a simpler solution that trying to make an upgrader.