Question

I'm building a simple to-do site just to help me learn mySQL a bit better. I'm having a bit of trouble getting the values to return. When i use "SELECT * FROM pages" as my query, all the data comes back fine. (pages is the table name)

However, what I want to do is only pull back a row of data for one particular user. In order to do this, I'm using a unique URL field in the database (previously created with another function) to select which row to return data for.

I'm missing something simple here that I was hoping someone could spot for me. With the following code, I get a series of this error that corresponds to the 7 variables I'm trying to output: (i'm aware of SQL injection issues, this is only for simplicity's sake. Also, please forgive the over-commenting, I'm learning, and that's the only way I can keep up with my work)

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /test/preview.php on line 16

<?php
include './includes/dbcon.php';

//declare $preview_url as the data submitted in the form under the 'preview_url' field
$preview_url = ($_POST['preview_url']);

//access db settings included in ./includes/dbcon.php
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

//select the row in the table that matches the passed variable from the form submission
$query="SELECT * FROM 'pages' WHERE 'url' = '".$preview_url."'";
$result=mysql_query($query);

// assign mySQL values from table to php variables
$email = mysql_result($result,1,'email');
$date=mysql_result($result,1,"date");
$title=mysql_result($result,1,"title");
$id=mysql_result($result,1,"id");
$items=mysql_result($result,1,"items");
$url=mysql_result($result,1,"url");
$expiry=mysql_result($result,1,"expiry");

//close the mySQL connection        
mysql_close();

?>

<html>
<head><title>Preview for <? echo $email; ?></title></head>
<body>
<strong>Email: </strong><a href="mailto:<? echo $email; ?>"><? echo $email; ?></a>        
<br />
<strong>Title: </strong><? echo $title; ?><br />
<strong>Date: </strong><? echo $date; ?><br />
<strong>Entry ID: </strong><? echo $id; ?><br />
<strong>Unique URL: </strong><a href="<? echo $url; ?>" target="_blank"><? echo          
$url; ?></a><br />
<strong>Link Expiration Date: </strong><? echo $expiry; ?><br />
<hr />
<strong>List:</strong><br />
<? echo $items; ?>
</body>
</html>
Was it helpful?

Solution

Change to this code portion Edwin

//select the row in the table that matches the passed variable from the form submission
$query="SELECT * FROM `pages` WHERE `url` = '".$preview_url."'";
$result=mysql_query($query);

if (!$result) {
    die(mysql_error());
}

// assign mySQL values from table to php variables
$email = mysql_result($result,1,'email');
$date=mysql_result($result,1,"date");
$title=mysql_result($result,1,"title");
$id=mysql_result($result,1,"id");
$items=mysql_result($result,1,"items");
$url=mysql_result($result,1,"url");
$expiry=mysql_result($result,1,"expiry");

//close the mySQL connection        
mysql_close();

Also, if you are truly hungry to be learning, then know that mysql_ functions are deprecated and will dissapear soon. Learn to use the mysqli_ functionality and PDO.

http://us1.php.net/manual/en/book.mysqli.php

http://us1.php.net/manual/en/class.pdo.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top