Jump to content

Recommended Posts

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

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;

 

    }

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.

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 by ginerjm

@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();

?>

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 by benanamen
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.