Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I'm trying to recreate PHPMyAdmin through a website however, I can update data inside newer databases however, for existing databases I have this error going on
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\improject-master\table-browse-edit.php on line 48

I tried echo-ing $mysqli->error and this shows up
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1

I've tried looking through google for the solutions however to no avail they don't seem to work on this.
Here's my php
<?php
session_start();
error_reporting(E_ERROR | E_PARSE);

$tname = $_SESSION['tname'];
$row_id = $_SESSION['row_id'];
$dbname = $_SESSION['dbname'];
$conn = mysqli_connect("localhost", "root", "", $dbname);

$result = mysqli_query($conn, "SHOW COLUMNS FROM $tname");
$fields = array();
$new_fields = array();
$counter = 0;


if(isset($_POST['save'])){
    $id = $_POST['id'];
    $new_fields = $_SESSION['fields'];
    $counter = $_SESSION['counter'];

    $sql .= "UPDATE $tname SET ";
    for($i = 0 ; $i < $counter ; $i++){
        $sql .= $new_fields[$i];
        $sql .= " = '" . $_POST[$new_fields[$i]] . "' ";
        if($i < $counter - 1){
            $sql .= " , ";
        }
    }
    $sql .= " WHERE $row_id = $id";
    $result = mysqli_query($conn, $sql);
        if (!$result) {
            $err = "Error updating data. Please try again.";
        } else {
        header("Location: table-browse.php");
    }
    
}

and the line that the error was referring to is
<?php if(mysqli_num_rows($result) > 0): ?>


How do I make it so that I can edit data inside old databases as well?

What I have tried:

It seems that this error happens when i try to pass a query to a database/table that was created within PHPMyAdmin itself. How do I remedy that?
Posted
Updated 18-Jan-21 2:05am
v3
Comments
[no name] 18-Jan-21 7:50am    
What's with the ":" at the end of the "if"? "Syntax" means there are language rules.

https://www.w3schools.com/php/func_mysqli_num_rows.asp
Kirk Poro 18-Jan-21 7:54am    
If i remove the ":" This shows up.
Parse error: syntax error, unexpected 'endif' (T_ENDIF), expecting end of file in C:\xampp\htdocs\improject-master\table-browse-edit.php on line 83
Richard Deeming 18-Jan-21 8:29am    
$sql .= " = '" . $_POST[$new_fields[$i]] . "' ";

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]

1 solution

1.
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in 

This means your SQL command failed, and the return value is set to "False". You should always check return values; do not just assume that they succeed.

2. The syntax error suggests your SQL command has a blank space where it is expecting a parameter value (field name, command option, or column value etc.). So you need to check the final value of the SQL statement to see what is missing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900