MySQLi problem
3 posts
Page 1 of 1
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 15My 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];
}
?>
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.
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);
//-----------------------------------------------------------------
?>
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>';
}
?>
3 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023