Page 1 of 1

HELP PLZ. mysqli->query returns false.

Posted: Sat Nov 06, 2010 3:39 pm
by BreadPaPa
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. :confused: Please help me find out what the problem is. A lot thanks in advance

Re: HELP PLZ. mysqli->query returns false.

Posted: Sat Nov 06, 2010 4:46 pm
by McInfo
If this condition is true, it does not necessarily mean that $inittb is false. It just means that $inittb is not true.

Code: Select all

$inittb !== TRUE
In this case, though, $inittb probably is false. If the table already exists, the CREATE query will result in mysqli::query() returning false.

I'm not sure what could be the cause of you LOAD query problem. Try

Code: Select all

var_dump($db->error);
Try using "\\n" instead of "\n" in the query.

Is banks.csv in the same directory as your PHP script?

Re: HELP PLZ. mysqli->query returns false.

Posted: Sat Nov 06, 2010 10:51 pm
by BreadPaPa
McInfo wrote:If this condition is true, it does not necessarily mean that $inittb is false. It just means that $inittb is not true.

Code: Select all

$inittb !== TRUE
In this case, though, $inittb probably is false. If the table already exists, the CREATE query will result in mysqli::query() returning false.

I'm not sure what could be the cause of you LOAD query problem. Try

Code: Select all

var_dump($db->error);
Try using "\\n" instead of "\n" in the query.

Is banks.csv in the same directory as your PHP script?
Thank you Mc.

For the first problem, the $inittb is false which I got from Eclipse debugger. Actually I kind of solve this problem by changing varchar(1000) to char(100). Then the table was created in database. I read from book that in varchar(M) M can be 1 to 65535, then I don't know why here varchar(1000) seems too big for php code running in Eclipse.

For the second problem, the banks.csv is in the same directory as PHP script and actually I used absolute routine. As I said, I copied this query to run in mysql terminal, it works. I will try var_dump later to see what happens.

Thank you again!