Jump to content

[SOLVED] mysql error.. can't figure it out.


Dragen

Recommended Posts

okay, I've been staring at this for a bit and can't figure it out.

I'm using the code below to read from a table.. it's not complete yet, but it should show at least one row from my table.

		<?php
	$pro_id = "sites";
	$sql = "SELECT * FROM portfolio WHERE pro_id=$pro_id ORDER BY date DESC LIMIT 2";
	$result = mysql_query($sql);

	while ($row = mysql_fetch_assoc($result)) {
	if ($row == false) break;
		echo $row['pro_id'] . "<br />" . $row['date'] . "<br />" . $row['name'];
	}
	?>

all I'm getting from it is:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/fhlinux181/g/gimppro.co.uk/user/htdocs/2portfolio.php on line 12

What have I done wrong??

 

 

EDIT: I forgot to say... line 12 is this one:

		while ($row = mysql_fetch_assoc($result)) {

Link to comment
https://forums.phpfreaks.com/topic/47466-solved-mysql-error-cant-figure-it-out/
Share on other sites

Try this and edit from here:

 

This will display you a record. You need to

get some output first.

<?php

$sql = "SELECT * FROM portfolio";
$result = mysql_query($sql);
  while ($row = mysql_fetch_assoc($result)) {
   
    echo"<tr>\n";
	//look at each field
	foreach($row as $col=>$val){
		echo "<td>$val</td>\n";
	}

      }// End of For loop
?>

Get in the habit of checking for errors before trying to use results.

 

<?php

  $pro_id = "sites";
  // fixed the error in your query. You where missing the quotes around $pro_id.
  $sql = "SELECT * FROM portfolio WHERE pro_id = '$pro_id' ORDER BY date DESC LIMIT 2";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        // $row will never equal false, explain what your trying to do in this part.
      }
    } else {
      echo "No results found";
    }
  } else {
    echo "Query failed<br />$sql<br />".mysql_error();
  }

?>

the

if ($row == false) break;

was suppossed to cancel writing the data from the table when there's no more.. I was looking at some other code I've got for something similar which needed a statement like that, so I thought I'd need it here.. obviously not.

 

Thanks though! it's working great.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.