LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Reviews Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-03-2020, 03:33 AM   #1
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 88

Rep: Reputation: 1
php mariadb mysqli_result


I have a program which should read data from one table and update data in another table but I keep getting php warnings:-
Code:
PHP Warning:  mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in /home/pi/Develop/php/doty.php on line 16
the code is:-
Code:
<?php
require_once('includes/config.inc');     // database connect script etc.
DBConnect();
/* check connection */
$sql = "SELECT id, class, dogkusa, points FROM results;";
// echo $sql;
$Reply = mysqli_query($link, $sql);
while ($row = mysqli_fetch_array($Reply))
    {
    $ID       = intval($row['id']);
    $Class    = $row['class'];
    $DogKusa  = $row['dogkusa'];
    $CurrentPoints   = floatval($row['points']);
    $SQL = "SELECT ownerid, class, kusa, points FROM dotyFinal WHERE kusa = dogkusa;";
    $Result = mysqli_query($link, $SQL);
    if (($row_cnt = mysqli_num_rows($Result)) > 1)
        {
        sprintf($Message, "At %d in %s Duplicate records found in dotyFinal %s at %s as %s %s",__LINE__, __FILE__, $dbname, $servername, $dbusername);
        trigger_error(E_USER_ERROR, $Message);
        exit;
        }

    if ($row_cnt == 1)
// If we already have a record for this dog
        {
        mysqli_fetch_array($Result);
        $WorkingPoints = $CurrentPoints + floatval($row["points"]); 
        $SQL = "UPDATE dotyFinal SET points = $WorkingPoints";
        }
    else
        {
        $SQL = "INSERT INTO dotyFinal (ownerid, class, kusa, points) VALUES ($ID,$Class,$DogKusa,$CurrentPoints)";
        }
    mysqli_query($link, $SQL);
    }
and the line with the issue is:-
if (($row_cnt = mysqli_num_rows($Result)) > 1)

I am doing this on Linux Debian with php7 and mariadb client version: 10.3.17
 
Old 02-03-2020, 05:49 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,984

Rep: Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439Reputation: 1439
Add error check after every operation: if an unsuccessful mysqli_query returns 'false', there is no point in fething from it.

Edit: old example:
http://lzsiga.users.sourceforge.net/ekezet.html#Q0057

Last edited by NevemTeve; 02-03-2020 at 11:25 AM.
 
1 members found this post helpful.
Old 02-03-2020, 09:38 AM   #3
urbanwks
Member
 
Registered: Sep 2003
Distribution: Slackware64-Current, FreeBSD 11.0
Posts: 193

Rep: Reputation: 205Reputation: 205Reputation: 205
I get this occasionally as well - your query likely has an error in it. I'd run the query directly against your database (mysql workbench/phpmyadmin/mysql console/etc) to find out where the error is.
 
1 members found this post helpful.
Old 02-03-2020, 11:47 AM   #4
boughtonp
Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 167

Rep: Reputation: 85
Quote:
Originally Posted by stockton View Post
I have a program which should read data from one table and update data in another table
Sounds like something that can be done entirely within SQL, see https://mariadb.com/kb/en/insert-on-...te-key-update/


On a more general / security-oriented note: You know how every now and then you hear of those huge data breaches? Well a lot of the time that's a result of SQL injection, caused by developers putting variables directly into the query, which allows attackers to modify the SQL (in various creative ways).

The easiest way to avoid that vulnerability is to always use parameterised queries (even when it might not be strictly necessary), and - in a situation when you're looping through repeating the same query with different data - it also has the advantage of being more efficient.

Relevant PHP docs: https://www.php.net/manual/en/mysqli...statements.php
 
Old 02-03-2020, 12:29 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,476

Rep: Reputation: 3120Reputation: 3120Reputation: 3120Reputation: 3120Reputation: 3120Reputation: 3120Reputation: 3120Reputation: 3120Reputation: 3120Reputation: 3120Reputation: 3120
Code:
$SQL = "SELECT ownerid, class, kusa, points FROM dotyFinal WHERE kusa = dogkusa;";
Sometimes its hard to see the forest for the trees... As a hint there are obvious syntax errors in your query statement i.e. WHERE kusa = dogkusa;
 
Old 02-04-2020, 01:30 AM   #6
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 88

Original Poster
Rep: Reputation: 1
Taking NevemTeve message to heart I wrote the following function
Code:
function finish_with_error($link, $line)
    {
    printf( "%s %d\n", mysqli_error($link),$line);
    mysqli_close($link);
    exit(1);
    }
and altered all mysqli_query to
Code:
    $Result = mysqli_query($link, $SQL);
    if (!$Result) finish_with_error($link,__LINE__);
which showed me where my coding issues were and by printf-ing the corresponding sql statement showed me the error of my ways.
Thanks guys.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: KDE Participating in Google Summer of Code 2019, MariaDB Releasing New Open-Source MariaDB Enterprise Server, CentOS Celebrates 15th B LXer Syndicated Linux News 0 02-28-2019 07:50 AM
LXer: Installing Nginx with PHP (as PHP-FPM) and MariaDB (LEMP) on Debian 8 LXer Syndicated Linux News 0 03-16-2016 09:33 PM
LXer: Installing Lighttpd with PHP (PHP-FPM mode) and MySQL or MariaDB on Ubuntu 15.04 LXer Syndicated Linux News 0 09-29-2015 08:15 PM
LXer: Install LAMP Server (Apache, MySQL or MariaDB, PHP) On Ubuntu 13.10 Server LXer Syndicated Linux News 0 10-22-2013 04:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration