0

I'm a beginner in php and mysql, I just want to know how can I put values that I got from a select query into variables.

For example I used this mysql query :

$req="SELECT type, titre, auteur, abstract, keywords FROM manusrit WHERE file='$name';";
$req1=mysql_query($req);

I want to put the value of the column type in $type variable and the value of auteur in a variable called $auteur and the same for abstract, and keywords.

How can I do this?

Smern
  • 18,746
  • 21
  • 72
  • 90
hich
  • 3
  • 1
  • http://php.net/mysql_fetch_assoc, and note that you're probably vulnerable to [sql injection attacks](http://bobby-tables.com), and the mysql_*() functions are obsolete/deprecated and should **NOT** be used in any new code. Do yourself a favor and switch to mysqli (note the `i`) or PDO immediately, before you learn too many bad habits. – Marc B May 15 '15 at 14:38
  • This previous questions looks like it is what you want: http://stackoverflow.com/questions/5157905/mysql-query-result-in-php-variable – Dane May 15 '15 at 14:42

1 Answers1

0

FIRST of all use PDO instead of mysqli. Example :

$link = mysql_pconnect($hostname, $username, $password) or die('Could not connect');
    //echo 'Connected successfully';
    mysql_select_db($dbname, $link);
    $name = $_POST['Name'];
    $query ="SELECT type, titre, auteur, abstract, keywords FROM manusrit WHERE file='$name';";
    mysql_query("SET NAMES 'utf8'", $link);
    $result = mysql_query($query, $link);

    //Now You FETCH result and read one by one
    while ($row = mysql_fetch_assoc($result)) {

        //Access COLMN like this
        print $row['abstract'];
        print $row['keywords'];
    }
Hosseini
  • 547
  • 3
  • 15