The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 15, 2019, 07:46:10 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: mysqli_fetch_row error  (Read 2692 times)
tomblack
Rookie
**
Offline Offline

Posts: 34


View Profile
« on: August 27, 2009, 03:19:56 PM »

Please can anyone help me with this error? 

I'm calling singleAnswer on Perks dbConnection php class but I'm getting the error message 'Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in /class.dbconnection.php on line 65'.

I've added some echos to the class to trace the error so line 65 is actual '$this->row = mysqli_fetch_row($this->dataSet);' in the fetchRow function.

The calling code is:
$this->__randomAgent = $db->singleAnswer("SELECT name FROM user_agent ORDER BY Rand() LIMIT 1");

Here the user_agent table columns:
     id     int(3)               No     None     auto_increment
   name    varchar(250)    latin1_swedish_ci       No    None

The error seems to only appear with the user_agent select statement, because fetch_row works fine with;

$this->__randomProxy = $db->singleAnswer("SELECT ip_address FROM proxy ORDER BY Rand() LIMIT 1");
$this->__score = $db->singleAnswer("SELECT score FROM proxy WHERE ip_address = '$this->__randomProxy'");

but I can get the same error by removing the single quotes from across  $this->__randomProxy.

Cheers Tom
Logged
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #1 on: August 27, 2009, 03:46:11 PM »

if you run:
SELECT name FROM user_agent ORDER BY Rand() LIMIT 1
right in mysql, do you get any rows back?
The singleanswer method actually assumes it will get a row back so it will error upon no row, or invalid column names for example.

thats my guess
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
tomblack
Rookie
**
Offline Offline

Posts: 34


View Profile
« Reply #2 on: August 27, 2009, 03:53:46 PM »

Hi Nutballs,

Yeah the select statement returns 1 row when I type it into phpmyadmin.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: August 27, 2009, 03:54:49 PM »

Hey Tom!

Usually, when the class reports that kind of error, the SQL is bad.

I'm not so sure what's wrong with the first example, but in your last example, the problem is your dereferencing the __randomProxy variable. Whenever you want to dereference a complex entity, such as the member of a class/object, you need to surround it in curlies. With the quotes around it, you get a viable SQL string, but no results. Without the quotes, you'll get an error message.

The correct string should be, "select score from proxy where ip_address='{$this->__randomProxy}'"

With regards to the first query, I can only assume that there's no table named user_agent, or something. I'd not do the rand() thing, it's hugely hard on the server because it will randomize a value for all rows, then select the top one. You might consider a different technique. Consider adding a field called "randkey" or something. put a random value in every row (update mytable set randkey=rand()) then select the first one that comes up when you do a select, and after using it, update that row with rand() again so it goes to a random place in the listing. I think that would cut down on serverload but quite a significant amount.

<add>I think NBs may be right - there may be a problem with RAND and limit one and singleAnswer thinking there should be only one row, but I'm not sure on that.</add>
« Last Edit: August 27, 2009, 03:56:26 PM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
tomblack
Rookie
**
Offline Offline

Posts: 34


View Profile
« Reply #4 on: August 28, 2009, 12:23:02 AM »

I found the problem, and I think it's caused by creating a table using phpMyAdmin.

The webpage can't see the name column on the user_agent table for some reason.  When I changed the query to 'select id' or 'select *' it worked fine.  But 'select name' still works fine in phpMyAdmin.  Shocked

I think I wrote a create table script for the proxy table but used myPhpAdmin to create the user_agent table, so I'm hoping that if I use a script instead of phpMyAdmin then the problem will go away.  Roll Eyes

Thanks for all your help with this.  Smiley
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: August 28, 2009, 09:32:41 AM »

Weird - I checked here, http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html and "name" is not a reserved word, so I don't know why that would get you, but hey - if it works now then all's well, eh?
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #6 on: August 28, 2009, 09:42:11 AM »

errr does the username you are accessing the table from PHP have permissions to that table/database?
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
tomblack
Rookie
**
Offline Offline

Posts: 34


View Profile
« Reply #7 on: August 28, 2009, 04:13:52 PM »

I've finally worked it out  Smiley

I think phpMyAdmin/Firefox has been playing tricks on me.  Roll Eyes  I usually exit and save my Firefox session before I switch the laptop off, so when I reopen FF I'm guessing it displays a cached version of phpMyAdmin.

Somehow I was able to query on a very old cached session of phpMyAdmin and I forgot that I changed the 'name' column to 'agent'.  D'oh!

So you were both right from the start about the SQL error.  ROFLMAO  D'oh!

Thanks again for your help.
Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!