0

I want to save my following data into MySQL Database.

following code is showing me two warnings

1- Illegal string offset.

2- Only variables can be passed by reference in.

Please help.

$db = new PDO('mysql:host=localhost;dbname=whois;charset=utf8mb4', 'root', '');
$items = array(
        'Domain Name:' => 'domain.name',
        'Domain ID:' => 'domain.handle',
        'Sponsoring Registrar:' => 'domain.sponsor',
        'Registrar ID:' => 'domain.sponsor',
        'Domain Status:' => 'domain.status.',
        'Status:' => 'domain.status.',
        'Name Server:' => 'domain.nserver.',
        'Nameservers:' => 'domain.nserver.',
        'Maintainer:' => 'domain.referer',
);

$stmt = $db->prepare("INSERT INTO temdata VALUES(?,?,?,?,?,?,?,?,?)");

foreach ($items as $row) {

    $stmt->bindParam(1, $row['domainname']);
    $stmt->bindParam(2, $row['domainid']);
    $stmt->bindParam(3, $row['registrar']);
    $stmt->bindParam(4, $row['registrarid']);
    $stmt->bindParam(5, $row['domainstatus']);
    $stmt->bindParam(6, $row['status']);
    $stmt->bindParam(7, $row['server']);
    $stmt->bindParam(8, $row['nameservers']);
    $stmt->bindParam(9, $row['maintainer']);
    $stmt->execute();
}
19
  • 2
    Let's start with this one. $row is not an array, it's a value. So $row['domainname'], $row['domainid'], ... are all wrong. Commented Feb 10, 2017 at 17:55
  • If you used named parameter, after bothering to load an array of data, this might well be a lot easier Commented Feb 10, 2017 at 17:55
  • now its showing me Fatal error: Call to undefined method PDOStatement::bind_param() Commented Feb 10, 2017 at 18:01
  • Lets start at the beginning. Are you using PDO or MYSQLI_ datbase extension Commented Feb 10, 2017 at 18:01
  • $db = new PDO('mysql:host=localhost;dbname=whois;charset=utf8mb4', 'root', ''); Commented Feb 10, 2017 at 18:02

2 Answers 2

1

I think this will work, but I personally would want something more robust before I released it.

$items = array(
        'Domain Name:' => 'domain.name',
        'Domain ID:' => 'domain.handle',
        'Sponsoring Registrar:' => 'domain.sponsor',
        'Registrar ID:' => 'domain.sponsor',
        'Domain Status:' => 'domain.status.',
        'Status:' => 'domain.status.',
        'Name Server:' => 'domain.nserver.',
        'Nameservers:' => 'domain.nserver.',
        'Maintainer:' => 'domain.referer',
);

$stmt = $db->prepare("INSERT INTO temdata VALUES(?,?,?,?,?,?,?,?,?)");
$col=1;
foreach ($items as $item) {
    $stmt->bindParam($col, $item);
    $col++;
}
$stmt->execute();
Sign up to request clarification or add additional context in comments.

2 Comments

That worked. Thank you soo much. you are a lifesaver :)
@Siddhesh Please accept the answer to close the question, otherwise it'd be floating around as open question. How to accept answer on Stack Overflow?
0

First of all remove trailing dot (.) from array values

$items = array(
    'Domain Name:' => 'domain.name',
    'Domain ID:' => 'domain.handle',
    'Sponsoring Registrar:' => 'domain.sponsor',
    'Registrar ID:' => 'domain.sponsor',
    'Domain Status:' => 'domain.status',
    'Status:' => 'domain.status',
    'Name Server:' => 'domain.nserver',
    'Nameservers:' => 'domain.nserver',
    'Maintainer:' => 'domain.referer',
);
$stmt = $db->prepare("INSERT INTO temdata VALUES(?,?,?,?,?,?,?,?,?)");

$i = 1;
foreach ($items as $row) {
    $stmt->bindParam($i, $row);
    $i++;
}

$stmt->execute();
`

Comments

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.