• PHP display data from mysql columns

    LowellFeierabend Member

    I wanna display all data from MySql columns named BOOK and LINK

    $query = mysql_query("select * from books where user ='$myusername'" );
    $column=mysql_fetch_array($query);
    $book=$column ['book'];
    $link=$column ['link'];
    

    And in text

    $book

    Only display One record. How to do that?

  • Abhey Member

    First, you might want to check out the proper MySQL syntax. Secondly, I hope you’re doing some security with $myusername. Thirdly, variables in single quotation marks (”) won’t be parsed. Finally, I’d do some MySQLi research.

  • ShikhaTan Member

    Useful link:

    http://www.w3schools.com/php/php_mysql_select.asp

    Might not be too pretty but this will work:

    $query = mysql_query("SELECT * FROM books WHERE user='$myusername'");
    while($column = mysql_fetch_array($query))
    {
        echo ''.$column['book'].'
    '; }

    You need to make sure that $myusername is safe from SQL injection.

    You stated it only displays one record, that is because in your code you are setting $book to $column[‘book’], which does not tell it to loop thru all of the data and display it.

    Hope this helped. Also I thought I would mention, if this is for a project that will be online for a while, keep in mind that as of PHP 5.5 the mysql_ commands are deprecated.

  • Adan Member

    I suggest using PDO instead of the mysql_* functions, they are not being maintained anymore. PDO offers a FETCH constant to fetch a column – PDO::FETCH_COLUMN. That’s exactly the one you need. Visit the link provided by

    If you don’t want to move to PDO, then this is the mysql_ function you need:

    http://php.net/manual/en/function.mysql-result.php

  • SapnaVishwas Member

    The mysql_fetch_assoc is handy if you extract your data and want access to them by the row-name. eg. Say result is the returned result of your query on a table containing name and age
    data=mysql_fetch_rowresult; will be an array on which your data can be accessed as data0 and data1

    data=mysql_fetch_assocresult; will be an array on which your data can be accessed as dataname and dataage

    I hope this will help you solve the problem.

Viewing 4 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish