0

I'm running this PHP script but I'm get the following error:

PHP Fatal error: Call to a member function bind_param() on a non object in c:/test.php on line 16

Line 16 is:

$load_stmt->bind_param('ss', $filetobeloaded, $filetobeloaded);

Can someone tell me what is wrong with this statement?

Full script:

<?php
$mysqli = new mysqli('localhost', 'root', 'password', 'orangevalleedb');
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$file_query = "select filename from cdr";
$load_query = "LOAD DATA INFILE ? INTO TABLE cdr FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n' ( @col_1, @col_2 )";

$file_stmt = $mysqli->prepare($file_query);
$file_stmt->execute();
$file_stmt->bind_result($filetobeloaded);

$load_stmt = $mysqli->prepare($load_query);
$load_stmt->bind_param('ss', $filetobeloaded, $filetobeloaded);

/* execute prepared statement */
while($file_stmt->fetch()) {
    $load_stmt->execute();
}

/* close statement and connection */
$load_stmt->close();
$file_stmt->close();

/* close connection */
$mysqli->close();
?>
3
  • Probably a copy&paste error. I've fixed that. Commented Jun 12, 2014 at 15:42
  • have you tried var_dump($load_stmt) to see what you get? Commented Jun 12, 2014 at 15:43
  • 1
    As the error message says, $load_stmt is not an object. There's probably an error in your SQL query. Commented Jun 12, 2014 at 15:43

3 Answers 3

4

I don't think you can have a bind marker in that part of the query as explained in the official mysqli_prepare documentation:

The markers are legal only in certain places in SQL statements. For example, they are allowed in the VALUES() list of an INSERT statement (to specify column values for a row), or in a comparison with a column in a WHERE clause to specify a comparison value.`

But I may be wrong.

Sign up to request clarification or add additional context in comments.

1 Comment

@JakeGould Thanks for confirming, wasn't 100% sure really.
1

This is useful information rather than an answer.

According to the MySQL 5.0 documentation these are the only statements that can be 'prepared'.

Sadly, it doesn’t change much until version 6+ which is not commonly available.

Taken from the manual.

The following SQL statements can be used in prepared statements:

ALTER TABLE
CALL
COMMIT
{CREATE | DROP} INDEX
{CREATE | DROP} TABLE
DELETE
DO
INSERT
RENAME TABLE
REPLACE
SELECT
SET
SHOW (most variants)
TRUNCATE TABLE
UPDATE

As of MySQL 5.0.15, the following additional statements are supported:

{CREATE | DROP} VIEW

As of MySQL 5.0.23, the following additional statements are supported:

ANALYZE TABLE
OPTIMIZE TABLE
REPAIR TABLE

Other statements are not supported in MySQL 5.0.

Comments

0

You can’t do this in your query:

$load_query = "LOAD DATA INFILE ? INTO TABLE…"

As explained in the official MySQL documentation on LOAD DATA page; emphasis mine:

The file name must be given as a literal string. On Windows, specify backslashes in path names as forward slashes or doubled backslashes. The character_set_filesystem system variable controls the interpretation of the file name.

So you should change that statement to read as:

$load_query = "LOAD DATA INFILE $filetobeloaded INTO TABLE cdr FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n' ( @col_1, @col_2 )";

Additionally, our bind statement on line 16 has two values:

$load_stmt->bind_param('ss', $filetobeloaded, $filetobeloaded);

But the query string being bound only has one ? in place:

$load_query = "LOAD DATA INFILE ? INTO TABLE cdr FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n' ( @col_1, @col_2 )";

Unclear why you have two values in the bind_param so perhaps just setting it to one param will work:

$load_stmt->bind_param('s', $filetobeloaded);

2 Comments

no it won't as its saying that $load_stmt is not an object.. irrespective of how many parameters there are.
Sorry here is the full statement: $load_query = "LOAD DATA INFILE ? INTO TABLE cdr FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n' SET filename = ?";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.