Sotiris
Sotiris

Reputation: 40096

mysql and php - export specific row

Hello I have a test table with name mytable and with following data

id  name   surname
==================
1   sotos  val
2   john   rik
3   peter  ask

How can id export for example the second row in mysql using php knowing the id?

Upvotes: 1

Views: 2284

Answers (2)

dev-null-dweller
dev-null-dweller

Reputation: 29482

If by export you mean dump data into ready-to-use SQL query try this one:

$sql_query = shell_exec('x:\path\to\mysqldump.exe -t --compact -u DB_USERNAME --password=DB_PASSWORD DB_NAME mytable --where="id = 2"');

Will produce something like:

INSERT INTO `mytable` VALUES(2,'john','rik');

Upvotes: 2

OMG Ponies
OMG Ponies

Reputation: 332731

Use:

SELECT t.id,
       t.name,
       t.surname
  FROM MYTABLE t
 WHERE t.id = mysql_real_escape_string($id)

Reference:

PHP

<?php

  $query = "SELECT t.id,
                   t.name,
                   t.surname
              FROM MYTABLE t
             WHERE t.id = mysql_real_escape_string($id)";
  $result = mysql_query($query);

  while ($row = mysql_fetch_assoc($result)) {
    echo $row['id'];
    echo $row['name'];
    echo $row['surname'];
  }
?>

Upvotes: 5

Related Questions