Asked  8 Months ago    Answers:  5   Viewed   34 times

I cannot get my Mysqli queries to both work. If I comment out one function in my html, the other function is properly executed and vice versa.

function all_posts() {
    require_once '';
    $mysqli = mysqli_connect($host, $username, $password, $database);
    $query = mysqli_query($mysqli, "SELECT variable_name, post_name, post_date, post_display FROM blog_posts ORDER BY id DESC LIMIT 5");

    if (!$query)
        echo mysqli_error();

    while ($results = mysqli_fetch_assoc($query)) {

        $post_name = $results['post_name'];
        $post_date = $results['post_date'];
        $post_display = $results['post_display'];
        $variable_name = $results['variable_name'];

        echo "<a href='posts.php?post={$variable_name}'>";
        echo "<div class='entry'>";
        echo "<div class='entry_header'>";
        echo "<h2>{$post_name}</h2>";
        echo "<h3>{$post_date}</h3>";
        echo "</div>";
        echo "<p>{$post_display}</p>";
        echo "</div>";
        echo "</a>";


function all_sidebar_posts() {

    require_once '';
    $mysqli = mysqli_connect($host, $username, $password, $database);
    $query = mysqli_query($mysqli, "SELECT variable_name, post_name FROM blog_posts ORDER BY id DESC LIMIT 5");

    while ($results = mysqli_fetch_assoc($query)) {

        $post_name = $results['post_name'];
        $variable_name = $results['variable_name'];
        echo "<li><a href='posts.php?post=$variable_name'>$post_name</a></li>";


Here is the html that I am outputting to.

    <?php all_sidebar_posts(); ?>
<div class="content_container">
    <?php all_posts(); ?>

I have tried using mysqli_data_seek(); but haven't had luck. Perhaps I am not using it right? I have browsed many questions and found similar ones but I have tried them all to no avail. I am new to programming so I may be overlooking something basic. Thank you all for the help!



You are doing it wrong way.
Never mix your data manipulation code with presentation code.

First, get the posts into array:

require_once '';
$mysqli = mysqli_connect($host, $username, $password, $database);

$sql = "SELECT variable_name, post_name, post_date, post_display 
        FROM blog_posts ORDER BY id DESC LIMIT 5"
$result = mysqli_query($mysqli, $sql);
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;

and then use this $data array to display posts any times you need, simply using foreach()

Wednesday, March 31, 2021
answered 8 Months ago

what you delete what you need to return is affected_rows

What you need to Replace

if ($result = $mysqli->query("DELETE FROM ktable WHERE code='value'")) {
    printf("Select returned %d rows.n", $result->num_rows);


Working Code

$value = ""; // Set To any Value
$mysqli = new mysqli ( "SQLHOST.COM", "CLIENT", "PASSWORD", "DNAME", 1234 );
if ($mysqli->connect_errno) {
    printf ( "Connect failed: %sn", $mysqli->connect_error );
    exit ();
} else {
    printf ( "cONN Sucees" );
    if ($mysqli->query (sprintf ( "DELETE FROM ktable WHERE code='%s'", mysqli_real_escape_string ( $mysqli, $value ) ) )) {
        printf ( "Affected Rows  %d rows.n", $mysqli->affected_rows );

You should have a working output

Wednesday, March 31, 2021
answered 8 Months ago

There is no method numRows, so you can't call it.

echo $rows->num_rows;

Saturday, May 29, 2021
answered 5 Months ago

Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

The internal data point is still at the end when you try to use your second while loop. You have done nothing to reset it.

You can move it back to the start with mysqli_data_seek($result, 0);


when first while loop run the internal data pointer move ahead and reach the last data pointer . so while your trying to get data from that using second while loop . so there is no more data because .it's already reach last data pointer .

Saturday, May 29, 2021
answered 5 Months ago

yo need create the user "pma" in mysql or change this lines(user and password for mysql):

/* User for advanced features */
$cfg['Servers'][$i]['controluser'] = 'pma'; 
$cfg['Servers'][$i]['controlpass'] = '';

Linux: /etc/phpmyadmin/

Tuesday, July 13, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :