HELP PLZ. mysqli->query returns false.
Posted: Sat Nov 06, 2010 3:39 pm
I was trying to create tables and import data into database by PHP code. Here is the simple code:
$db = new mysqli('localhost', 'root', '12345', 'bank_report');
if (mysqli_connect_error()){
echo "Cannot connect to database.";
exit;
}
$crttb = "CREATE TABLE banks_list(
stname VARCHAR(1000))";
$inittb = $db->query($crttb);
if ($inittb !== TRUE){
echo "Error occurs during initializing table.";
exit;
}
The $inittb returned FALSE which indicates the query is not successful. But the table WAS CREATED in database which I could find it via terminal or in phpmyadmin.
Later, I wrote another simple code: (before this I created a table which has same fields as those in banks.csv file, in mysql terminal.)
$db = new mysqli('localhost', 'root', '12345', 'bank_report');
$import = "LOAD DATA LOCAL INFILE 'banks.csv'
INTO TABLE banks_list
FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES";
$banklist = $db->query($import);
The $banklist returned FALSE and the .csv file was not imported to table. But if I copy and paste the command in $import and run it in mysql terminal, the data in .csv DID go into the table.
This is pretty weird for me.
Please help me find out what the problem is. A lot thanks in advance
$db = new mysqli('localhost', 'root', '12345', 'bank_report');
if (mysqli_connect_error()){
echo "Cannot connect to database.";
exit;
}
$crttb = "CREATE TABLE banks_list(
stname VARCHAR(1000))";
$inittb = $db->query($crttb);
if ($inittb !== TRUE){
echo "Error occurs during initializing table.";
exit;
}
The $inittb returned FALSE which indicates the query is not successful. But the table WAS CREATED in database which I could find it via terminal or in phpmyadmin.
Later, I wrote another simple code: (before this I created a table which has same fields as those in banks.csv file, in mysql terminal.)
$db = new mysqli('localhost', 'root', '12345', 'bank_report');
$import = "LOAD DATA LOCAL INFILE 'banks.csv'
INTO TABLE banks_list
FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES";
$banklist = $db->query($import);
The $banklist returned FALSE and the .csv file was not imported to table. But if I copy and paste the command in $import and run it in mysql terminal, the data in .csv DID go into the table.
This is pretty weird for me.