简体   繁体   中英

Querying the database through PHP (some trouble)

Okay so I'm trying to query the database to find the value in the name column that corresponds to the value in the id column. So I have the id, and I use that to get the value. For some reason, this does not seem to work. Echoing name doesn't print out the name that corresponds to that id, instead it prints out just 1 , and I don't get any errors either.

    $con = new mysqli('localhost', 'username', 'pass', 'database_name');
    $id = htmlspecialchars($_GET["id"]);

    $query = 'SELECT name FROM files 
        WHERE id=' .$id;

    $query_p = $con->prepare($query);

    $name = $query_p->execute();


    $con->close;

echo $name;
$name = $query_p->execute(); //  This won't give you the name back

execute() doesn't return you those values.

Returns TRUE on success or FALSE on failure.

Don't you think you have to fetch the result?

$query_p = $con->prepare($query);
$query_p->execute();
$query_p->bind_result($name);

while ($stmt->fetch()) {
    printf ("%s \n", $name);
}
$db = new mysqli('localhost', 'username', 'pass', 'database_name');
$sql = 'SELECT name FROM files WHERE id=' . $_GET["id"];
$result = $db->query($sql);
//...
$row = $result->fetch_assoc();
$name = $row['name'];

Can you try this,

 $id = $_GET["id"];
 $query = 'SELECT name FROM files  WHERE id="'.$id.'"' ;
 $query_p = $con->prepare($query);
 $sth = $query_p->execute();
  while( $row = $sth->fetchAll()){
     echo $row['name'];
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM