I'm also not thrilled with the requirement for a more recent version of Ruby than comes as standard with my Ubuntu 12.04 (Precise Pangolin) laptop or Debian 6.0 (Squeeze) servers. Depending on your application, the YAML approach to specifying the SQL commands might or might not appeal though. While I haven't gone over it thoroughly, mysql2csv looks promising. I thought about writing a script for this, but as soon as I thought about what I'd call it, it occurred to me to search for preexisting work by the same name. However, using a scripting language (one with decent internal data structures that is, not Bash), and libraries where the encoding issues have already been carefully worked out is far safer. The '-s' flag to mysql does do backslash escaping, and might lead to a correct solution. ' because that cannot encode carriage returns and white space in fields. It is not possible to generate reliably correct CSV content from the output of mysql -B -e 'SELECT. They're not so useful for things like automating data export. MySQL Workbench (and similarly phpMyAdmin) provide a formally correct solution, but they are designed for downloading the output to a user's location. It's a bit wordy, I have to admit.All of the solutions here to date, except the MySQL Workbench one, are incorrect and quite possibly unsafe (i.e., security issues) for at least some possible content in the MySQL database. $data = $stmt->get_result()->fetch_all() If any PHP variable is going to be used, you should never use mysqli_query(), but always stick to prepared statements, like this: $stmt = $mysqli->prepare("SELECT * FROM students WHERE class=?") You may use this function only if no variables are going to be used in the query. The most important note: unlike mysql_query(), mysqli_query() has a very limited use. To do so, add this line before mysqli_connect(): mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT) You have to configure mysqli to throw errors automatically instead of checking them for each mysqli statement manually. In such a case, fill the $data array the usual way: $data = Some hosts may have no support for the fetch_all() function. $data = $link->query("DESC students")->fetch_all(MYSQLI_ASSOC) In case you want to print the column titles, you have to select your data into a nested array first and then use keys of the first row: // getting all the rows from the query May be it's because the routine is so routine, known to PHP folks for decades: $result = $link->query("DESCRIBE students") Įcho "",$row,"",$row,"\n" I have to admit, mysqli_query() manual entry doesn't contain a clean example on how to fetch multiple rows. $result = mysqli_query($link,"SHOW CREATE TABLE students") My other question is how to print the query SHOW CREATE TABLE students. | ID | int(10) | NO | PRI | NULL | auto_increment | | Field | Type | Null | Key | Default | Extra | If possible I want to print $result so that it looks like: - - - - - - The documentation says For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object.īut from there I don't know how to print $result so that it shows the query results. $result = mysqli_query($link,"DESCRIBE students") Here's my code: $link = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DATABASE) I'm trying to use the mysqli_query function in PHP to DESCRIBE the table. I have the following mySQL table: CREATE TABLE `students` (
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |