Jump to content

Doesn't Pull Data From Database For Some Reason


Kwright02

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>';
Link to comment
Share on other sites

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;

 

    }
Link to comment
Share on other sites

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

 

    }
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

?>
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.