Warning: mysql_select_db() expects parameter 1 to be string, resource given in C:\xampp\htdocs\my_php_files\php4.php on line 25
Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\my_php_files\php4.php on line 26 Could not create table:
The following code is to create a simple database and a table. I'm not able to execute this properly .I've searched many sites to fix this,nothing worked. I'm assuming there are quite a few mistakes .
<?php
//initialization
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '**********';
$dbname = 'data_base';
//check connection
$conn = mysql_connect($dbhost, $dbuser, $dbpass,$dbname);
if(! $conn )
{
die('Could not connect: '. mysql_error());
}
echo 'Connected successfully';
//create table
$sql = 'CREATE TABLE employee( '.
'emp_id INT NOT NULL AUTO_INCREMENT, '.
'emp_name VARCHAR(20) NOT NULL, '.
'emp_address VARCHAR(20) NOT NULL, '.
'emp_salary INT NOT NULL, '.
'join_date timestamp(14) NOT NULL, '.
'primary key ( emp_id ))';
mysql_select_db($conn,$sql);
$retval = mysqli_query("SELECT * FROM employee");
if(! $retval )
{
die('Could not create table: ' . mysql_error());
}
echo "Table employee created successfully
";
mysql_close($conn);
?>
I'm getting errors in the line 25 and line 26 of the program.
It looks like you're using the Database select command with incorrect information, this commands allows you to choose the database:
mysql_select_db('my_database',$conn); // or the name of the DB you want to use
Also you are mixing mysqli and mysql, you'll need to fix that.
$retval = mysql_query("SELECT * FROM employee");
Change your connection code like this
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname,$conn);
Do not mix mysql with mysqli
$retval = mysql_query("SELECT * FROM employee");