When I run the following query
$sql ="SELECT * FROM user_info JOIN Notifications ON user_info.user_info_id =Notifications.Sender_id AND Notifications.STATE=0 LIMIT 1";
$query=mysqli_query($con,$sql);
$num_rows=mysqli_num_rows($query);
$message=''
if($num_rows > 0){
$con=mysqli_fetch_assoc($query);
switch ($con['Notification_Type']){
case'events':
$var="Notifications".$con['user_info_id'];
$sql_shown="SELECT *FROMevent WHERE event.notification_shown = 0 AND event.user_info_id='$var'LIMIT 1";
$query_vi=mysqli_query($con,$sql_shown);
$num_rows_vi=mysqli_num_rows($query_vi);
$message.=$con['Sender_id']."has created a Event";
echo $message;
break;
}
}
I get the following error:
mysqli_query() expects parameter 1 to be mysqli, array given in
This is my user table:
CREATE TABLE IF NOT EXISTS user_info(
user_info_id INT(11) NOT NULL AUTO_INCREMENT,
u_first_name VARCHAR(255) NOT NULL,
u_last_name VARCHAR(255) NOT NULL,
u_email VARCHAR(255) NOT NULL,
u_mobile VARCHAR(255) NOT NULL,
role ENUM('1yearM','1yearN','1yearR','1yearU','1yearS','2M','2R','2N','2U','2S','3M','3R','3N','3U','3S','4M','4R','4N','4U','4S','professor','librarian','admission_department') NOT NULL,
password VARCHAR(255) NOT NULL,
u_ip VARCHAR(255) NOT NULL,
signup_date DATETIME NOT NULL,
last_login DATETIME NOT NULL,
act_code VARCHAR(255) NOT NULL,
activation enum('1','0') NOT NULL DEFAULT '0',
PRIMARY KEY (user_info_id),
UNIQUE KEY (u_email,u_mobile)
)
This is my event table:
CREATE TABLE IF NOT EXISTS Event(
Event_id INT(11) NOT NULL AUTO_INCREMENT,
Event_Name VARCHAR(255) NOT NULL,
Event_location VARCHAR(255) NOT NULL,
Event_Organizer VARCHAR(255) NOT NULL,
Event_Date_Posted DATETIME NOT NULL,
Event_Starting_timings DATETIME NOT NULL,
Event_Ending_timings DATETIME NOT NULL,
Event_Day VARCHAR(255) NOT NULL,
Event_Description text NOT NULL,
Event_file_name VARCHAR(255) NOT NULL,
Event_file_path VARCHAR(255) NOT NULL,
Event_file_size VARCHAR(255) NOT NULL,
user_info_id INT(11) NOT NULL,
notification_shown ENUM('1','0') NOT NULL DEFAULT '0',
PRIMARY KEY (Event_id),
FOREIGN KEY (user_info_id) REFERENCES user_info(user_info_id)
)
This is my notification table:
CREATE TABLE IF NOT EXISTS Notifications(
Notifications_id INT(11) NOT NULL AUTO_INCREMENT,
Notification_Type VARCHAR(255) NOT NULL,
Notification_Content VARCHAR(255) NOT NULL,
Notification_Created_Date DATETIME NOT NULL,
Notification_State ENUM('read','unread') NOT NULL DEFAULT 'unread',
Is_Notification_Delete ENUM('1','0') NOT NULL DEFAULT '0',
Notification_Deleted_Date DATETIME NOT NULL,
Sender_id INT(11) NOT NULL,
STATE ENUM('1','0') NOT NULL DEFAULT '0',
Recipient_id INT(11) NOT NULL,
PRIMARY KEY (Notifications_id)
)
You overwrite your $con variable with this line:
$con=mysqli_fetch_assoc($query);
Before that line your $con variable was a valid MySQLi connection handle. After that line it is not an array. Therefore your mysqli_query() call complain that the first argument isn't a mysqli connection/object/handle anymore, but instead is now an array (which it shouldn't).
Change your $con=mysqli_fetch_assoc($query); so it doesn't overwrite your $con variable with the MySQLi connection/object/handle, but instead write the result in a different variable (maybe $userinfo?).
Check if your $con it's been created with properly method mysqli_connect('').
Related
I am working on creating table in to my database with mysql and here is my code:
mysqli_query($link, "CREATE TABLE IF NOT EXISTS `review` (
`clients_id` int(15) NOT NULL AUTO_INCREMENT,
`client_id` varchar(150) NOT NULL DEFAULT '',
`rating` tinyint(2) NOT NULL DEFAULT '0',
`proj_date` date NOT NULL DEFAULT '0000-00-00',
`proj_desc` text NOT NULL DEFAULT '',
`photoname` text NOT NULL,
`companyname` text NOT NULL,
`feedback` text NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '0',
`emailid` varchar(100) NOT NULL DEFAULT '',
`customratings` varchar(100) NOT NULL DEFAULT '',
`photo_option` varchar(100) NOT NULL DEFAULT '',
`title` varchar(100) NOT NULL DEFAULT '',
`citation` varchar(100) NOT NULL DEFAULT '',
`date_option` varchar(100) NOT NULL DEFAULT '',
`rating_option` varchar(100) NOT NULL DEFAULT '',
PRIMARY KEY (`clients_id`),
FULLTEXT KEY `feedback` (`feedback`)
) ENGINE=MyISAM AUTO_INCREMENT=1") or mysqli_error($link);
But this is not reflecting into my database ? Why were I may be going wrong ?
but I tried creating other table with the following code
mysqli_query($link, "CREATE TABLE IF NOT EXISTS `setting` (
`id` int(11) NOT NULL auto_increment,
`script_url` text NOT NULL,
`date` varchar(4) NOT NULL,
`rateing` varchar(4) NOT NULL,
`photo` varchar(4) NOT NULL,
`dateformat` varchar(4) NOT NULL,
`page_limit` int(4) NOT NULL,
`proj_desc` varchar(4) NOT NULL,
`companyname` varchar(4) NOT NULL,
`text_color` varchar(255) NOT NULL,
`citation_color` varchar(255) NOT NULL,
`bg_color` varchar(255) NOT NULL,
`border_color` varchar(255) NOT NULL,
`ratingsformat` varchar(250) NOT NULL,
`rating` varchar(250) NOT NULL,
`customratings` varchar(250) NOT NULL,
`speed` varchar(250) NOT NULL,
`pagination` varchar(250) NOT NULL,
`version` varchar(250) NOT NULL,
`global_option` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0;
") or mysqli_error($link);
it is being created correctly and both the tables are in the same file
No you cannot create table using an insert statement.
There are four types of SQL statements:
DML (DATA MANIPULATION LANGUAGE)
DDL (DATA DEFINITION LANGUAGE)
DCL (DATA CONTROL LANGUAGE)
TCL (TRANSACTION CONTROL LANGUAGE)
DML is your SELECT, INSERT, UPDATE and DELETE statements...
DDL is you your CREATE, ALTER and DROP statements.
See more info about types of SQL statements
In order to insert data in your table, first you need to create it.
How to create sql table from php
No. "INSERT INTO" used to insert the data to existing table only.You should create the table with respective column before try to insert the values.
Or you can check the table exist or not before going insert."If exists" u can insert the value else just create and insert the values.
This worked for me but I don't know how?
I just removed DEFAULT '' present while creating review table and table just got created
Here is edited code
mysqli_query($link, "CREATE TABLE IF NOT EXISTS `review` (
`clients_id` int(15) NOT NULL AUTO_INCREMENT,
`client_id` varchar(150) NOT NULL,
`rating` tinyint(2) NOT NULL,
`proj_date` date NOT NULL,
`proj_desc` text NOT NULL,
`photoname` text NOT NULL,
`companyname` text NOT NULL,
`feedback` text NOT NULL,
`status` tinyint(1) NOT NULL,
`emailid` varchar(100) NOT NULL,
`customratings` varchar(100) NOT NULL,
`photo_option` varchar(100) NOT NULL,
`title` varchar(100) NOT NULL,
`citation` varchar(100) NOT NULL,
`date_option` varchar(100) NOT NULL,
`rating_option` varchar(100) NOT NULL,
PRIMARY KEY (`clients_id`),
FULLTEXT KEY `feedback` (`feedback`)
) ENGINE=MyISAM AUTO_INCREMENT=1") or mysqli_error($link);
I have it set up to echo when my tables create but the users table just won't, I've looked at multiple solutions but none of them has helped here my code
<?
phpinclude_once("index.php");
$tbl_users = "CREATE TABLE IF NOT EXISTS users
(
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(16) NOT NULL,
email VARCHAR(64) NOT NULL,
password VARCHAR(64) NOT NULL,
gender ENUM('m','f') NOT NULL,
website VARCHAR(64) NULL,
country VARCHAR(64) NULL,
userlevel ENUM('a','b','c','d') NOT NULL DEFAULT a,
avatar VARCHAR(64) NULL,
ip VARCHAR(64) NOT NULL,
signup DATETIME NOT NULL,
lastlogin DATETIME NOT NULL,
notescheck DATETIME NOT NULL,
activated ENUM('0','1') NOT NULL DEFAULT 0,
PRIMARY KEY (id)),
UNIQUE KEY username (username,email))";
$query = mysqli_query($db_spooky, $tbl_users);
if ($query === TRUE)
{
echo "<h3>user table created OK :) </h3>";
}
else
{
echo "<h3>user table NOT created :( </h3>";
}
?>
CREATE TABLE IF NOT EXISTS users (id INT(11) NOT NULL AUTO_INCREMENT
,username VARCHAR(16) NOT NULL
,email VARCHAR(64) NOT NULL,password VARCHAR(64) NOT NULL
,gender ENUM('m','f') NOT NULL
,website VARCHAR(64) NULL
,country VARCHAR(64) NULL
,userlevel ENUM('a','b','c','d') NOT NULL DEFAULT 'a'
,avatar VARCHAR(64) NULL
,ip VARCHAR(64) NOT NULL
,signup DATETIME NOT NULL
,lastlogin DATETIME NOT NULL
,notescheck DATETIME NOT NULL,activated ENUM('0','1') NOT NULL DEFAULT 0
,PRIMARY KEY (id)
,UNIQUE KEY username (username,email))
Learn to organize your code instead of writing it in one long line for one, and two, ENUM default for a needs to be in single quotes and you had an extra closing bracket. The code above is the corrected version.
I have the following PHP page to create a table using a text file.
table_create.php
<?php
include $db;
$query_file = "sql.txt";
$fp = fopen($query_file, 'r');
$sql = fread($fp, filesize($query_file));
fclose($fp);
$retval = mysql_query($sql);
if(! $retval )
{
die("Could not create the tables<br>");
}
echo "Table created successfully<br>";
?>
sql.txt
CREATE TABLE ht_account (
id int(11) NOT NULL AUTO_INCREMENT,
date date NOT NULL,
type varchar(50) NOT NULL,
mode varchar(50) NOT NULL,
party varchar(50) NOT NULL,
payee varchar(50) NOT NULL,
rate decimal(13,2) NOT NULL,
box int(11) NOT NULL,
amount decimal(13,2) NOT NULL,
token varchar(50) NOT NULL,
remarks varchar(50) NOT NULL,
user varchar(50) NOT NULL,
user_confirm varchar(50) NOT NULL,
status varchar(50) NOT NULL);
CREATE TABLE ht_bank (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
ac_no varchar(50) NOT NULL,
address varchar(50) NOT NULL);
CREATE TABLE ht_user_role (
id int(11) NOT NULL AUTO_INCREMENT,
value varchar(50) NOT NULL);
When I try to create a single table in the sql.txt file, the code works perfectly.
For example:
CREATE TABLE ht_account (
id int(11) NOT NULL AUTO_INCREMENT,
date date NOT NULL,
type varchar(50) NOT NULL,
mode varchar(50) NOT NULL,
party varchar(50) NOT NULL,
payee varchar(50) NOT NULL,
rate decimal(13,2) NOT NULL,
box int(11) NOT NULL,
amount decimal(13,2) NOT NULL,
token varchar(50) NOT NULL,
remarks varchar(50) NOT NULL,
user varchar(50) NOT NULL,
user_confirm varchar(50) NOT NULL,
status varchar(50) NOT NULL);
But when I try to create multiple tables, It does not create any table. I doubt that the format in the sql.txt may be incorrect.
The format is, almost sure, correct but mysql_query doesn't work with multiple queries:
mysql_query() sends a unique query (multiple queries are not
supported) to the currently active database on the server that's
associated with the specified link_identifier.
It's better to use mysqli functions because mysql ones are deprecated for PHP 5.5 and mysqli has the function mysqli_multi_query that you need.
If you still want to use mysql functions you could do something like:
$sql_array=explode(';',$sql);
foreach ($sql_array as $s) {
if(! mysql_query($s)){
echo mysql_error()."<br>";
}
}
My knowledge of MySQL/ SQL apart from the simple CRUD operations is basic.
If I had to use a stored procedure to move certain attributes (not in a specific order) to another table how could it be done?
These are the following tables. I want to move from the 1st to the 2nd table.
As you can see the datatype sizes are different for certain columns.
CREATE TABLE IF NOT EXISTS `source_cdr` (
`callstart` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`src` varchar(80) NOT NULL DEFAULT '',
`dst` varchar(80) NOT NULL DEFAULT '',
`accountcode` varchar(50) NOT NULL,
`uniqueid` varchar(100) NOT NULL,
`ID` int(11) NOT NULL AUTO_INCREMENT,
`callanswer` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`callend` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`disposition` varchar(50) NOT NULL,
`cdr_id` int(11) unsigned NOT NULL DEFAULT '0',
`pin_code` varchar(4) NOT NULL,
`provider` int(11) NOT NULL,
PRIMARY KEY (`ID`),
KEY `calldate_idx` (`callstart`) USING BTREE,
KEY `idx_acc_code_calldate` (`accountcode`,`callstart`) USING BTREE,
KEY `uniqueid` (`uniqueid`),
KEY `cdr_id` (`cdr_id`),
KEY `idx_uniqueid_cdr_id` (`uniqueid`,`cdr_id`)
) ENGINE=MyISAM;
--
CREATE TABLE IF NOT EXISTS `destination_cdr` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`calldate` datetime NOT NULL,
`source` varchar(80) NOT NULL,
`destination` varchar(80) NOT NULL,
`account_code` varchar(30) DEFAULT NULL,
`pincode` varchar(45) NOT NULL,
`duration_call` bigint(20) NOT NULL DEFAULT '0',
`duration_talk` bigint(20) NOT NULL,
`disposition` varchar(255) NOT NULL,
`clid` varchar(80) DEFAULT NULL,
`cdr_id` bigint(20) DEFAULT NULL,
`vxcdr_id` bigint(20) DEFAULT NULL,
`provider` int(11) NOT NULL DEFAULT '0'
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
EDIT 1
An example of a row
('2012-03-18 20:54:49', '5796', '0761100866', '103f0124ad510516f33cab132c0a695b', 'call-F1884808-6753-2F10-181C-3A#10.217.164.33', 308006367, '2012-03-18 20:55:05', '2012-03-18 20:55:51', '200 OK', 2, '', 0),
Thanks
You can use MySQL: INSERT ... SELECT Syntax to copy data from one table to the other.
Decide common fields in both and copy the same.
Example:
INSERT INTO TABLE2( COL1, COLx, ... ) SELECT colM, colY FROM TABLE1;
If the column sizes mismatch, data truncation takes place, and you can't overcome that but redefine the destination table columns.
I want to create some table through PHP but it fails everytime, but the same code excutes perfectly when excuted through MYsql console or PHPMyAdmin
The SQLs are
$sql = <<<SQL_CODE
CREATE TABLE IF NOT EXISTS `bid` (
`aid` int(11) unsigned NOT NULL,
`uid` int(11) unsigned NOT NULL,
`name` varchar(20) NOT NULL,
`amount` smallint(6) unsigned NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS `item` (
`aid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(40) NOT NULL,
`description` varchar(120) NOT NULL,
`img` int(11) unsigned NOT NULL,
`amount` smallint(6) unsigned NOT NULL,
`strtdate` date NOT NULL,
`enddate` date NOT NULL,
`uid` int(11) unsigned NOT NULL,
`uname` varchar(20) NOT NULL,
`uamount` int(11) unsigned NOT NULL,
PRIMARY KEY (`aid`)
);
CREATE TABLE IF NOT EXISTS `user` (
`uid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(40) NOT NULL,
`password` varchar(40) NOT NULL,
`nameF` varchar(20) NOT NULL,
`nameL` varchar(20) NOT NULL,
`sex` varchar(1) NOT NULL,
`img` int(11) unsigned NOT NULL,
`country` varchar(10) NOT NULL,
`state` varchar(20) NOT NULL,
`address` varchar(120) NOT NULL,
`code` varchar(8) NOT NULL,
`isAdmin` varchar(1) NOT NULL,
PRIMARY KEY (`uid`),
UNIQUE KEY `email` (`email`)
);
SQL_CODE;
$sql2 = 'CREATE DATABASE `'.$db.'`;';
$sql3 = 'USE `'.$db.'`; ';
$sql4 = 'drop Database `'.$db.'``;';
if (!mysql_query($sql2)) echo mysql_error();
if (!mysql_query($sql3)) echo mysql_error();
//sleep(1);
if (!mysql_query($sql)) echo mysql_error();
echo $sql2.' '.$sql3.' '.$sql;
Error I'm getting
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS item ( aid int(11) unsigned NOT NULL AUTO_INCREMENT,' at line 1CREATE DATABASE auction; USE auction; CREATE TABLE IF NOT EXISTS bid ( aid int(11) unsigned NOT NULL, uid int(11) unsigned NOT NULL, name varchar(20) NOT NULL, amount smallint(6) unsigned NOT NULL, time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP); CREATE TABLE IF NOT EXISTS item ( aid int(11) unsigned NOT NULL AUTO_INCREMENT, name varchar(40) NOT NULL, description varchar(120) NOT NULL, img int(11) unsigned NOT NULL, amount smallint(6) unsigned NOT NULL, strtdate date NOT NULL, enddate date NOT NULL, uid int(11) unsigned NOT NULL, uname varchar(20) NOT NULL, uamount int(11) unsigned NOT NULL, PRIMARY KEY (aid));CREATE TABLE IF NOT EXISTS user ( uid int(11) unsigned NOT NULL AUTO_INCREMENT, email varchar(40) NOT NULL, password varchar(40) NOT NULL, nameF varchar(20) NOT NULL, nameL varchar(20) NOT NULL, sex varchar(1) NOT NULL, img int(11) unsigned NOT NULL, country varchar(10) NOT NULL, state varchar(20) NOT NULL, address varchar(120) NOT NULL, code varchar(8) NOT NULL, isAdmin varchar(1) NOT NULL, PRIMARY KEY (uid), UNIQUE KEY email (email));
From the mysql_query manual;
mysql_query() sends a unique query (multiple queries are not
supported)...
You need to split your triple CREATE TABLE statement into 3 separate statements and it will work.
I think that is a problem with multiple queries. I think that is solution.
http://php.net/manual/en/function.mysql-query.php
*mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier*
Just split them up and it will work.