You are viewing our Forum Archives. To view or take place in current topics click here.
[PHP]mysql_fetch_array() expects parameter 1 to be resource
Posted:

[PHP]mysql_fetch_array() expects parameter 1 to be resourcePosted:

Strobe
  • TTG Contender
Status: Offline
Joined: Sep 11, 201014Year Member
Posts: 3,410
Reputation Power: 140
Status: Offline
Joined: Sep 11, 201014Year Member
Posts: 3,410
Reputation Power: 140
Ok, so as you can see in the title, I'm having a tough time with a mySQL Query.
Here is my code:
$query = mysql_query("SELECT * FROM posts WHERE user=\"$post\" ORDER BY id DESC");
while($row = mysql_fetch_array($query)){
$title=$row['title'];
$poster=$row['user'];
$id=$row['id'];
$data=$row['data'];
echo '<div><h3><a href="?post='.$id.'">'.$title.'</a></h3> By ';
echo $poster;
echo '<p><br>' .$data. '</p></div><br>';
}
}

}
else{
// if post is not submitted than display up to 8 last blog posts, Adjust LIMIT 8 to however many you want to show
$query = mysql_query("SELECT * FROM posts ORDER BY id DESC LIMIT 8");
/********************************
THIS LINE BELOW IS WHERE THE ERROR IS OCCURING.
********************************/
while($row = mysql_fetch_array($query)){
$title=$row['title'];
$poster=$row['user'];
$id=$row['id'];
$data=$row['data'];
echo '<div><h3><a href="?post='.$id.'">'.$title.'</a></h3> By ';
echo ' <a href="?post=' .$poster. '">' .$poster. '</a><p><br>' .$data. '</p></div><br>';
}
}

I commented out where the error is occurring.
In case you are wondering, I'm attempting to display posts just made. I'm very frustrated with this, and if you fixed the code I would rep you 25 points!
Thanks
#2. Posted:
7en
  • Wise One
Status: Offline
Joined: Aug 16, 201212Year Member
Posts: 598
Reputation Power: 29
Status: Offline
Joined: Aug 16, 201212Year Member
Posts: 598
Reputation Power: 29
It means the data it's extracting is not what it was expecting - in English, you spelled something wrong.

Double and triple check the spelling of your tables & columns.

$title=$row['title'];
$poster=$row['user'];
$id=$row['id'];
$data=$row['data'];

Check title,user,id,data
I'm almost positive one of them is inconsistent with the database.

I did the same thing yesterday, spent a half hour trying to isolate the problem. My script said $username = $row['user'] when the actual table was called "users".
#3. Posted:
Strobe
  • TTG Contender
Status: Offline
Joined: Sep 11, 201014Year Member
Posts: 3,410
Reputation Power: 140
Status: Offline
Joined: Sep 11, 201014Year Member
Posts: 3,410
Reputation Power: 140
Abbreviate wrote It means the data it's extracting is not what it was expecting - in English, you spelled something wrong.

Double and triple check the spelling of your tables & columns.

$title=$row['title'];
$poster=$row['user'];
$id=$row['id'];
$data=$row['data'];

Check title,user,id,data
I'm almost positive one of them is inconsistent with the database.

I did the same thing yesterday, spent a half hour trying to isolate the problem. My script said $username = $row['user'] when the actual table was called "users".

okay thanks a lot ill try it now
EDIT:
No luck. Here is my whole page.

<?php


require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
require_once("models/header.php");

echo "
<body class='test'>";

include("left-nav.php");


// connecting to host
mysql_connect("localhost", "not_important", "dontstealmypassyoitsfake"); // replace this with your data
mysql_select_db("dbname"); // database name here

$user=$loggedInUser->displayname;

// HTML here for form; Edit as needed
echo "
</div>
<div id='main'>
<body>";
echo 'Welcome '.$user. '<br><br><br><br>';


//Beginning of blog
if(isset($_GET['post'])){
$post = $_GET['post'];

// now we will make sure that the post id is numeric if not it is recognized as a name
if(is_numeric($post)){ // is numeric allows numbers only
$post = (int)$post; // and the int function, which replace every
// string to its correspoing number

// but is not really needed in this case
$post = mysql_real_escape_string($post); // final sqli defense

// final part
$query = mysql_query("SELECT * FROM blog_post WHERE id=$post LIMIT 1");
while($row = mysql_fetch_array($query)){
$title=$row['title'];
$poster=$row['user'];
$id=$row['id'];
$data=$row['data'];
echo '<div><h3>';
echo $title;
echo '</a></h3>';
echo ' by <a href="?post=' .$poster. '">' .$poster. '</a><p><br>' .$data. '</p></div><br>';
}
}
else{ // if post is not numeric than it assumes it is the display name
$query = mysql_query("SELECT * FROM posts WHERE user=\"$post\" ORDER BY id DESC");
while($row = mysql_fetch_array($query)){
$title=$row['title'];
$poster=$row['user'];
$id=$row['id'];
$data=$row['data'];
echo '<div><h3><a href="?post='.$id.'">'.$title.'</a></h3> By ';
echo $poster;
echo '<p><br>' .$data. '</p></div><br>';
}
}

}
else{
// if post is not submitted than display up to 8 last blog posts, Adjust LIMIT 8 to however many you want to show
$query = mysql_query("SELECT * FROM posts ORDER BY id DESC LIMIT 8");
while($row = mysql_fetch_array($query)){
$title=$row['title'];
$poster=$row['user'];
$id=$row['id'];
$data=$row['data'];
echo '<div><h3><a href="?post='.$id.'">'.$title.'</a></h3> By ';
echo ' <a href="?post=' .$poster. '">' .$poster. '</a><p><br>' .$data. '</p></div><br>';
}
}

//Basic HTML for posting blogs below; Currently shows on all blog webpages
echo "
<br><br>
<form action='blogpost.php' method='post'>
<input type='text' name='title' placeholder='Title'><br>
<textarea name='post' cols='60' rows='10' placeholder='Post'></textarea>";
echo '<input type="hidden" name="user" value="' .$user. '">';
echo "
<br><input type='submit'>
</form>
</body>
</html>";
?>
#4. Posted:
iyop45
  • Prospect
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
Can you not check whether $query is an array or not:

var_dump($array);

Though you should always have a failsafe for when querying a database:

 while($row = mysql_fetch_array($query) or die(mysql_error()){ /* etc etc */

Often having php return these mysql errors is a lot more helpful in identifying the problem, though I assume there is an error with running the query itself and hence $query is null.
#5. Posted:
7en
  • Wise One
Status: Offline
Joined: Aug 16, 201212Year Member
Posts: 598
Reputation Power: 29
Status: Offline
Joined: Aug 16, 201212Year Member
Posts: 598
Reputation Power: 29
You're using ORDER BY on a field that doesn't exist in the table you're SELECTing from.

As iyop said, the query is failing and or die(mysql_error()) will tell you exactly what's going wrong.
#6. Posted:
Cyimking
  • V5 Launch
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Why are you doing this:

 user=\"$post\"


Shouldn't it be:

user = "$post"
#7. Posted:
7en
  • V5 Launch
Status: Offline
Joined: Aug 16, 201212Year Member
Posts: 598
Reputation Power: 29
Status: Offline
Joined: Aug 16, 201212Year Member
Posts: 598
Reputation Power: 29
Cyimking wrote Why are you doing this:

 user=\"$post\"


Shouldn't it be:

user = "$post"


He's escaping the previous inverted comma. Ideally he should be using '$post'
#8. Posted:
iyop45
  • Prospect
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
I'd of though this'd be more legible and appropriate but as Abbreviate said '/' is just an escape character and of course that query isn't having any issues anyway?:


"SELECT * FROM posts WHERE user=" . $post . " ORDER BY id DESC"


Usually an error like the one you have mentioned is followed by "... 'x' given in" of which may determine what's going wrong with the script and what is actually getting passed through. (If it's a boolean chances are it's 0 of which equates to false and so the query is failing)

Though just curious why not resort to PDO? It's safer and, correct me if I'm wrong, but functions such as mysql_fetch_array() are basically deprecated:

$stmt = $mysqli->prepare("SELECT id, user, title, data FROM posts ORDER BY id DESC LIMIT 8");
$stmt->execute();
$stmt->store_result();               
$stmt->bind_result($id,$user,$title,$data);
     if($stmt->num_rows > 0){
          while($stmt->fetch(){
           echo $user; /* etc etc */
        }
     }else{
        echo "no results";
     }

Just an idea maybe.
#9. Posted:
Cyimking
  • E3 2016
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Ah i think i figured out the problem!

You have to check the query to see if it fails or not

So try...
$query = mysql_query("SELECT * FROM posts WHERE user=\"$post\" ORDER BY id DESC");
if(!$query)
die(mysql_error());


This should solve your problem. [/code]
#10. Posted:
CoNdEmR
  • Retired Staff
Status: Offline
Joined: Apr 29, 200915Year Member
Posts: 4,420
Reputation Power: 1211
Status: Offline
Joined: Apr 29, 200915Year Member
Posts: 4,420
Reputation Power: 1211
"MYSQL_FETCH_ARRAY() EXPECTS PARAMETER 1 TO BE RESOURCE"

Means that there is something wrong with your query, as you have probably ascertained at this stage. Although, looking at it now it seems fine.

Try using "mysql_fetch_assoc" in your while loop instead of "mysql_fetch_array".
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.