{ Learn Code. Share Code. Teach Code. Love Code. }

  • MySQLi problem

 #74427  by benji_19994
 Thu Jan 24, 2013 2:29 am
I need help. I'm trying to make a simple blog.i can echo the title and content but when i try add date and aithor in the query and try echo it i get a error
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\MySQL\connect.inc.php on line 15
My php code:
Code: Select all
<?php

$link = mysqli_connect('localhost', 'root', '', 'blog');

if (mysqli_connect_error())
{
 $LogMessage = 'MySQL Error: ' .mysqli_connect_error();

 die('Cannot Connect To Database!');
}
$query = "SELECT `date `,`author`,`title`,`contents` FROM entries";

$result = mysqli_query($link, $query);

while($row = mysqli_fetch_array($result))
{
  echo '<h1>'.$row[0] .'<br></h1>' .$row[1] .$row[2] .$row[3];
}

?>
 #74430  by visualtech
 Thu Jan 24, 2013 8:11 am
What I have done is used a query to fetch each row at once then I assigned them a variable, say $author for the author.
Hope this helps! If you have any difficulties in understanding the code, just PM me.
Code: Select all

<?php

$db_host = '127.0.0.1';  
$db_user = 'root'; 
$db_pass = '';
$db_name = 'blog';
$db_tablename = 'entries';

$db_conn = @mysql_connect($db_host, $db_user, $db_pass); 

if(!$db_conn) 
  {
    echo "Error in the Connecting to  Server\n";    
    die(); 
  }
  
  $db_selected = @mysql_select_db($db_name, $db_conn);
 
  if(!$db_selected)
  {
    echo 'Error in the Selecting Database.';
    die();
  } //Database Selection

  //-----------------------Fetch the Author--------------------------
  
	$q_name = "SELECT `author` FROM `tut1`"; 
   # mysql_real_escape_string($name));
  
    $r = @mysql_query($q_name, $db_conn);
    $ro = @mysql_fetch_array($r);

	$ret = $ro[0];
	
	$author = $ret;
	echo($author);
	
  //-----------------------------------------------------------------
  
  //-----------------------Fetch the Title---------------------------
  
  $q_name = "SELECT `title` FROM `tut1`";  //Change `title` to `contents` to fetch the content. 
   # mysql_real_escape_string($name)); 
  
    $r = @mysql_query($q_name, $db_conn);
    $ro = @mysql_fetch_array($r);

	$ret = $ro[0];
	
	$title = $ret;
	echo($title);

  //-----------------------------------------------------------------
?>

 #74431  by benji_19994
 Thu Jan 24, 2013 8:20 am
I've got it now
Code: Select all
<?php

$link = mysqli_connect('localhost', 'root', '', 'blog');
//Connect MySQLI
if (mysqli_connect_error())
//Check to see for Error and if it doesn't and if there is an error die
{
 $LogMessage = 'MySQL Error: ' .mysqli_connect_error();

 die('Cannot Connect To Database!');
}
$query = "SELECT `title`,`author`,`date`,`contents` FROM `entries`";
//Request the colums from table entries
$result = mysqli_query($link, $query);
//store them in an array
while($row = mysqli_fetch_array($result))
{
//echo array out
  echo '<div id="blog"><h1>'.$row[0] .'</h1>'.'<h6>Author: '.$row[1].' <br>Date: '.$row[2].' </h6><p>' .$row[3].'</p></div>';
}

?>