1

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?

2 Answers 2

5

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'];
  }
?>
Sign up to request clarification or add additional context in comments.

4 Comments

What is the MYTABLE keyword for?
@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.
Ah. The t is solely an alias? Huh. Didn't know you could skip the AS keyword. Good to know!
@Christian Mann: Yep, t is a table alias. The AS keyword is optional on most databases for both table and column aliases.
2

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');

1 Comment

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.

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.