jamesclues Posted March 29, 2018 Share Posted March 29, 2018 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 ?> Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted March 29, 2018 Share Posted March 29, 2018 (edited) 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 March 29, 2018 by mac_gyver Quote Link to comment Share on other sites More sharing options...
jamesclues Posted March 29, 2018 Author Share Posted March 29, 2018 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 29, 2018 Share Posted March 29, 2018 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>"; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.