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 'database.inc.php';
    $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>";
    }

    mysqli_free_result();
}

function all_sidebar_posts() {

    require_once 'database.inc.php';
    $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>";
    }

    mysqli_free_result();
}

Here is the html that I am outputting to.

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

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!

 Answers

41

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

First, get the posts into array:

require_once 'database.inc.php';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$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
 
Fanda
answered 8 Months ago
15

what you delete what you need to return is affected_rows http://www.php.net/manual/en/mysqli.affected-rows.php

What you need to Replace

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


    printf($result->num_rows);
    $result->close();
}

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
 
Hilmi
answered 8 Months ago
67

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

echo $rows->num_rows;

http://www.php.net/manual/en/mysqli-result.num-rows.php

Saturday, May 29, 2021
 
Keat
answered 5 Months ago
28

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

Update:

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
 
pwaring
answered 5 Months ago
39

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/config.inc.php

Tuesday, July 13, 2021
 
ShadowZzz
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 :
 
Share