Jump to content

PHP mysql display row of data one single page


jamesclues

Recommended Posts

When I run this I am getting no errors and no data back :( please can someone tell me what i am doing wrong
 
 
<?php
include 'conn.php';


    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);


    //Query


    $sql = $conn->prepare("SELECT hotelid, hotelname FROM Hotels WHERE hotelid = '1'"); 


    $sql->execute() ;
     
?>
<div id="container">
<header>
     <div id="header">
         <div class="h1">
             <?php while( $row = $sql->fetch()) : ?>
                <h1><span><?php echo $row['hotelname']; ?></span>
                <?php endwhile ?>

 

Link to comment
Share on other sites

the mysqli stmt fetch method doesn't return the row of data. it fetches the data into the bound result variables. you can find examples in the php.net documentation. if you had php's error_reporting set to E_ALL and display_errors set to ON, you would be getting an undefined index error at the $row['hotelname'] reference.

 

the php mysqli extension is overly complicated. it's biggest problem is that everything to do with prepared queries requires more statements and is a different programming interface then for non-prepared queries.

 

i/we recommend that you use the much simpler and more consistent php PDO extension. it requires less statements and it has the same programming interface for prepared and non-prepared queries. an added benefit of the PDO extension is that once you learn the php statements, you can use those same php statements with any of the supported database types. you would only have to make any necessary sql syntax changes, not learn a whole new php extension for each different database type.

 

even some of the things that were added to the mysqli extension to make it more usable are dependent on the mysqlnd driver being used, which you cannot guarantee unless you manage your own servers, and so results in non-portable code.

Edited by mac_gyver
Link to comment
Share on other sites

do you have an example a simple way ?

 

the mysqli stmt fetch method doesn't return the row of data. it fetches the data into the bound result variables. you can find examples in the php.net documentation. if you had php's error_reporting set to E_ALL and display_errors set to ON, you would be getting an undefined index error at the $row['hotelname'] reference.

 

the php mysqli extension is overly complicated. it's biggest problem is that everything to do with prepared queries requires more statements and is a different programming interface then for non-prepared queries.

 

i/we recommend that you use the much simpler and more consistent php PDO extension. it requires less statements and it has the same programming interface for prepared and non-prepared queries. an added benefit of the PDO extension is that once you learn the php statements, you can use those same php statements with any of the supported database types. you would only have to make any necessary sql syntax changes, not learn a whole new php extension for each different database type.

Link to comment
Share on other sites

Assuming $conn is a PDO connection

//  your query doesn't need preparing as there are no parameters
$res = $conn->query("SELECT hotelname 
                     FROM hotels 
                     WHERE hotelid = 1");
// as you ony fetch one row it is pointless using a while() loop
$hotelname = $res->fetchColumn();  // as we only want a single column
echo $hotelname;

To do the same with a PDO prepared query

// provide a parameter
$id = 1;
$res = $conn->prepare("SELECT hotelname 
                       FROM hotels
                       WHERE hotelid = ?");
$res->execute( [$id] );
$hotelname = $res->fetchColumn();
echo $hotelname;

If selecting several records

$res = $conn->query("SELECT hotelid
                        , hotelname
                     FROM hotels");
while ($row = $res->fetch()) {
    echo "{$row['hotelid']} | {$row['hotelname']} <br>";
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.