Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

mysql fetch array error

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

mysql fetch array error

Postby TechGuy1 » Tue Sep 13, 2011 2:01 pm

I am a php noob. I am following along with a tutorial. I have the same exact database tables, fields, etc and I have the same exact code as the tutorial but yet I am getting an error and his works....I searched for an answer on a few forums and the same question is asked but never really answered. I the info I am selecting from the db is there so that is not it....thanks in advanced

THE ERROR

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\jobolicious\db_fns.php on line 34

THE CODE

function db_connect(){
$connection = mysql_connect('localhost','TechGuy1','121212aa','jobolicious')
or die('Not Connected!');
}

function find_jobs(){
db_connect();

$select ="SELECT
id,
location,
title,
company,
description,
url";

$from = "FROM
jobs";

$where = "WHERE
id > 0";

$query = $select . $from . $where;

$result = mysql_query($query);

while ($row = mysql_fetch_array($result)){
print_r($row);
}


}

find_jobs();
TechGuy1
New php-forum User
New php-forum User
 
Posts: 5
Joined: Tue Sep 13, 2011 2:00 pm

Re: mysql fetch array error

Postby NigelRen » Tue Sep 13, 2011 2:07 pm

I think the problem is caused by not testing if $result is set. I think the query your building up has a few spaces missing. Each of the components looks fine - but when you add them together - you'll probably find that the last column ( url ) and the FROM will appear as one word.

So two things - put some spaces in when building up the full SQL, and most importantly is to check that the values you get back show that it has worked before using them again.
NigelRen
php-forum Active User
php-forum Active User
 
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am

Re: mysql fetch array error

Postby TechGuy1 » Wed Sep 14, 2011 5:45 am

NigelRen wrote:I think the problem is caused by not testing if $result is set. I think the query your building up has a few spaces missing. Each of the components looks fine - but when you add them together - you'll probably find that the last column ( url ) and the FROM will appear as one word.

So two things - put some spaces in when building up the full SQL, and most importantly is to check that the values you get back show that it has worked before using them again.


Thanks for the response. I put the queries all on one line and it didnt work. I tested the $result by replacing the while loop with

if(isset($result)){
echo 'It is set!';
}else{
echo 'Its not set!';
}

and it said 'it is set'

but then I replaced it with
echo $result;
and it was a blank page. On the tutorial when he echoed the $result it said resource 3
TechGuy1
New php-forum User
New php-forum User
 
Posts: 5
Joined: Tue Sep 13, 2011 2:00 pm

Re: mysql fetch array error

Postby egami » Wed Sep 14, 2011 6:38 am

You're kind of crazy with all that code.

make it REALLY simple. Keeping it simple is the best policy.

$query = "ENTIRE SELECT STATEMENT HERE";
$result = mysql_query($query) or die ("ERROR! HERE'S WHY!: ".mysql_error());
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
echo '<pre>'; print_r($row); echo '</pre>';
}
} else {
echo "Didn't find anything with the query. Check the query to make sure it's correct and try again.";
}
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: mysql fetch array error

Postby TechGuy1 » Wed Sep 14, 2011 6:42 am

egami wrote:You're kind of crazy with all that code.

make it REALLY simple. Keeping it simple is the best policy.

$query = "ENTIRE SELECT STATEMENT HERE";
$result = mysql_query($query) or die ("ERROR! HERE'S WHY!: ".mysql_error());
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
echo '<pre>'; print_r($row); echo '</pre>';
}
} else {
echo "Didn't find anything with the query. Check the query to make sure it's correct and try again.";
}


no, I totally agree, putting the variables, etc is too much. Thats what the tutorial did and I was copying that. Ill rewrite it and let you know what happens...thanks
TechGuy1
New php-forum User
New php-forum User
 
Posts: 5
Joined: Tue Sep 13, 2011 2:00 pm

Re: mysql fetch array error

Postby swadeep » Sun Oct 07, 2012 1:35 pm

Hi TechGuy,
I am working on same project and see different issues.
Did it worked for you?
Please let me know.

Thanks
Swadeep
swadeep
New php-forum User
New php-forum User
 
Posts: 1
Joined: Sun Oct 07, 2012 1:31 pm

Re: mysql fetch array error

Postby seandisanti » Mon Oct 08, 2012 2:51 pm

your spaces. you're butting your literal strings right up against each other without trailing or leading spaces. so it's printing out like "select fieldsfrom tablewhere conditions" instead of "select fields from table where conditions". then querying with a bad query string returns false instead of a result set. so your fetch is not being passed a result set but a boolean, as the error says. :) whenever you're getting sql errors, always try to echo out your query string to look for little gotchas like this.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 787
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron