Kwright02 Posted April 18, 2017 Share Posted April 18, 2017 The following code below is used to pull text from a database to fill in a paragraph spot but doesn't do that for some reason. Can you please explain to me why? I'm fairly new to php coding and So I'm not quite sure what i've done wrong. $host = "localhost"; $dbname = "xxx"; $username = "xxx"; $password = "xxx"; $conn = new mysqli($host, $username, $password, $dbname); // Check connection if ($conn->connect_error) { echo $conn->connect_error; } $sql = "SELECT paragraphtext FROM mainpage WHERE paragraphnum='1'"; $result = $conn->query($sql); if($result == null){ echo 'Error retreving paragraph text'; } echo '<p>' . $conn->query($sql) . '</p>'; Quote Link to comment Share on other sites More sharing options...
dkub Posted April 18, 2017 Share Posted April 18, 2017 mysqli::query returns a mysqi_result object. You need to fetch results before you can output them. Quote Link to comment Share on other sites More sharing options...
Kwright02 Posted April 18, 2017 Author Share Posted April 18, 2017 i've looked throuhg some documentations and this is the new code, but it still doesn't work: $sql = "SELECT paragraphtext FROM mainpage WHERE paragraphnum='1'"; if ($result = $conn->query($query)) { $text = mysql_fetch($result); echo $text; } Quote Link to comment Share on other sites More sharing options...
benanamen Posted April 18, 2017 Share Posted April 18, 2017 (edited) Did the documentation tell you there is something called mysql_fetch? Edited April 18, 2017 by benanamen Quote Link to comment Share on other sites More sharing options...
Kwright02 Posted April 18, 2017 Author Share Posted April 18, 2017 I miss typed it that time. i'm actuall doing: $sql = "SELECT paragraphtext FROM mainpage WHERE paragraphnum='1'"; if ($result = $conn->query($query)) { $text = mysql_fetch_row($result); echo $text[0]; } Quote Link to comment Share on other sites More sharing options...
dkub Posted April 18, 2017 Share Posted April 18, 2017 The mysql extension has been deprecated since approximately forever and was removed in PHP 7. Perhaps even more importantly is that mysqli is a separate extension. You can't mix and match functions. Read the links I provided earlier (well, read the ones I've provided here also) and you pretty much have a step by step guide to get your code working. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 18, 2017 Share Posted April 18, 2017 (edited) You mis-typed it. Why don't you cut and paste your actual code so that can't happen. Besides you corrected the wrong line, typing it incorrectly as well. Also - if you read the manual page on this function you would learn what the result looks like and handle it with proper code. An array can't just be echo-ed out. Edited April 18, 2017 by ginerjm Quote Link to comment Share on other sites More sharing options...
mlukac89 Posted April 18, 2017 Share Posted April 18, 2017 @Kwright02 try this if works, http://php.net/manual/en/mysqli-result.fetch-array.php <?php $host = "localhost"; $username = "xxx"; $password = "xxx"; $dbname = "xxx"; $mysqli = new mysqli($host, $username, $password, $dbname); // check connection if ($mysqli->connect_errno) { echo "Connect failed: ", $mysqli->connect_error); exit(); } $query = "SELECT paragraphtext FROM mainpage WHERE paragraphnum='1'"; $result = $mysqli->query($query); // if query runs if ($result) { // loop through results and echo them in paragraph while ($row = $result->fetch_array(MYSQLI_ASSOC)) { echo '<p>'.$row["paragraphtext"] .'</p>'; } } else { echo 'There was a problem with query.'; } // close connection $mysqli->close(); ?> Quote Link to comment Share on other sites More sharing options...
benanamen Posted April 18, 2017 Share Posted April 18, 2017 (edited) Outputting internal errors is always a bad idea. The info is only good to a hacker and of no use to the user. There is no need to manually close the connection. Php will do it automatically when the script finishes running. Edited April 18, 2017 by benanamen 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.