views:

29

answers:

2

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?

+5  A: 

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'];
  }
?>
OMG Ponies
What is the `MYTABLE` keyword for?
Christian Mann
@Christian Mann: That's the OP stated the table name is - "...I have a test table with name mytable...", but you can change it to suit.
OMG Ponies
Ah. The `t` is solely an alias? Huh. Didn't know you could skip the `AS` keyword. Good to know!
Christian Mann
@Christian Mann: Yep, `t` is a table alias. The `AS` keyword is optional on most databases for both table and column aliases.
OMG Ponies
@OMG Ponies thanks!
Sotiris
A: 

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');
dev-null-dweller
Did we read the same question?
OMG Ponies
Judging from OP reputation, previous questions and website provided in profile, he has basic knowledge how to get data from mysql in php. OP has not specified what does he expects as output, so I provided my interpretation of mysql **export** using php.
dev-null-dweller