mySQL Error

duyen

New Member
Reaction score
214
I get this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home2/stliuq/public_html/guestbook.php on line 48
PHP:
<?php
$con = mysql_connect("localhost","personremoved","passremoved");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbremoved", $con);

/*THIS IS LINE 48*/ $result = mysql_query("SELECT * FROM posts");

while($row = mysql_fetch_array($result))
  {
  echo '<div class="name">';
  echo $row['name'];
  echo '</div>';
  }

mysql_close($con);
?>

I double checked the DB and table names.
 

TFlan

I could change this in my User CP.
Reaction score
64
PHP:
echo mysql_error(); // after the query

Should tell you whats the problem.

But try this:
PHP:
$con = mysql_connect("localhost","personremoved","passremoved");
if(!$con){
	die('Could not connect: '.mysql_error());
}

mysql_select_db("dbremoved", $con);

$result = mysql_query("SELECT `name` FROM `posts`");

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
	echo '<div class="name">'
		. $row['name']
		. '</div>';
}

mysql_close($con);
 

ertaboy356b

Old School Gamer
Reaction score
86
in every mysql function you make.. put this code at the end

"or die(mysql_error())"

it will generate a mysql error report...

for example

$con = mysql_connect("localhost","personremoved","passremoved") or die(mysql_error());

mysql_select_db("dbremoved", $con) or die(mysql_error());

$result = mysql_query("SELECT `name` FROM `posts`") or die(mysql_error());
 

TFlan

I could change this in my User CP.
Reaction score
64
don't use die. you want to see your page and how it's affected because of the error.

Use echo.
 

JerseyFoo

1/g = g-1
Reaction score
40
According to the error...
while($row = mysql_fetch_array($result))
... is line 48, so go with the error on this.

It is probably because it didn't return anything, so you could do this...
Code:
if ( mysql_num_rows($result) > 0 ){
  while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  ...
  }
} else {
   echo '<div class="name"><i>Nothing to show</i></div>';
}

And as my OCD is kicking in, you may also use...
PHP:
$q = "SELECT `name` FROM `posts` LIMIT 0,100"; // Limiting is usually nice
if ( $r = mysql_unbuffered_query($q) ){
  while ( $v = mysql_fetch_row($r) ){
    echo '<div class="name">' . $v[0] . '</div>';
  }
} else {
  // no results
}
mysql_free_result($r);
 

duyen

New Member
Reaction score
214
According to the error...

... is line 48, so go with the error on this.

It is probably because it didn't return anything, so you could do this...
Code:
if ( mysql_num_rows($result) > 0 ){
  while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  ...
  }
} else {
   echo '<div class="name"><i>Nothing to show</i></div>';
}

And as my OCD is kicking in, you may also use...
PHP:
$q = "SELECT `name` FROM `posts` LIMIT 0,100"; // Limiting is usually nice
if ( $r = mysql_unbuffered_query($q) ){
  while ( $v = mysql_fetch_row($r) ){
    echo '<div class="name">' . $v[0] . '</div>';
  }
} else {
  // no results
}
mysql_free_result($r);

The table is not empty.
 

TFlan

I could change this in my User CP.
Reaction score
64
anything that you think is relevant.

PHP:
<?php
$con = mysql_connect("localhost","personremoved","passremoved");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbremoved", $con);

$result = mysql_query("SELECT * FROM posts");
if($result==FALSE){ echo mysql_error(); echo "<br>".$result."<br>"; } // Test if result is even there.
while($row = mysql_fetch_array($result))
  {
  echo '<div class="name">';
  echo $row['name'];
  echo '</div>';
  }

mysql_close($con);
?>

Use that, give us the result of it.
 

TFlan

I could change this in my User CP.
Reaction score
64
Then thats your error.

No DB is connected.

to confim this use this:

PHP:
<?php
$con = mysql_connect("localhost","personremoved","passremoved");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$db = mysql_select_db("dbremoved", $con);
if (!$db)
  {
  die('Could not connect: ' . mysql_error());
  }

$result = mysql_query("SELECT * FROM posts");
if($result==FALSE){ echo mysql_error(); echo "<br>".$result."<br>"; } // Test if result is even there.
while($row = mysql_fetch_array($result))
  {
  echo '<div class="name">';
  echo $row['name'];
  echo '</div>';
  }

mysql_close($con);
?>

And if that gets confirmed, then we have no control over it. Check your spelling and make sure the DB is in that MySQL connection.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top