-4

I have created 2 tables in php myadmin the first table is member with rows user_id - primary key name username password

the second table is blogdata with rows id - primary key author_id -foreign key references user_id in member table Title Content Category

here is my php to select from the database

<?php
session_start();


$_SESSION['author_id']='user_id';
$sql = mysql_query("SELECT * FROM blogdata where user_id = author_id");
while($row = mysql_fetch_array($sql)){
$title = $row['Title'];
$category =$row['Category'];
$content =$row['Content'];
$date =$row['date'];


?>

it keeps given me an error saying Warning: mysql_fetch_array() expects parameter 1 to be resource. any help would be greatly appreciated as i am new at php.

i have my database connection in so is not that.

John Dane
  • 51
  • 1
  • 1
  • 3
  • 2
    Your issue is not with session, but with your mysql query. Did you read the documentation on [PHP.net](http://us2.php.net/manual/en/function.mysql-query.php)? – oomlaut Apr 28 '13 at 13:43

2 Answers2

1

You forgot to use mysql_connect() before running your query.

Denis de Bernardy
  • 72,128
  • 12
  • 122
  • 148
0

You aren't querying your data correctly. Try this:

<?php
session_start();

$_SESSION['author_id'] = $php_user_id;
$sql = mysql_query("SELECT * FROM blogdata where user_id = \"".mysql_real_escape_string($_SESSION[author_id]."\"");
while($row = mysql_fetch_array($sql)){
$title = $row['Title'];
$category =$row['Category'];
$content =$row['Content'];
$date =$row['date'];
}
?>

You also didn't close your while brackets so I updated that for you. All of this assumes you already have a connection with your DB :)

Sven
  • 66,463
  • 10
  • 102
  • 105
user1048676
  • 9,306
  • 25
  • 80
  • 117
  • The query is insecure, SQL injection is possible. Why didn't you add a call to `mysql_real_escape_string()` – Sven Apr 28 '13 at 13:51
  • @Sven Good point. In reality, they shouldn't be using any of the mysql functions anymore either. I should have alerted them to that as well. – user1048676 Apr 28 '13 at 14:14