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();
mysql fetch array error
Moderators: egami, macek, gesf
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.
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 withNigelRen 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.
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
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.";
}
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...thanksegami 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.";
}
-
- php-forum Fan User
- Posts: 973
- Joined: Mon Oct 01, 2012 12:32 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.
