hich hich - 7 months ago 12
SQL Question

get values from select query and put them into variables

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?

Answer

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'];
    }