Forum Moderators: coopster
I am quite new to php, was wondering if anyone could give me a pointer to why this code is not working, I am trying to get each row of my database to be displayed on a different page, e.g example.com/query.php?=2 (3,4,5 etc) I assume this is how you go about this, any help or suggestions would be amazing.
<?php
// Connects to your Database
mysql_connect("localhost", "login", "password") or die(mysql_error());
mysql_select_db("news") or die(mysql_error());
// Query
$id = $_GET['id'];
$data = mysql_query("SELECT * FROM `news` WHERE `id`='" .$id. "'");
or die(mysql_error());
\\
Get parse error code on
$data = mysql_query("SELECT * FROM `news` WHERE `id`='" .$id. "'");
$data = mysql_query("SELECT * FROM `news` WHERE `id`=".$id);
then put this back in
or die(mysql_error());
so it should be this
$data = mysql_query("SELECT * FROM `news` WHERE `id`=".$id)
or die(mysql_error());
show me the code where you use the mysql_fetch_array()
[edited by: Dilly at 7:21 pm (utc) on Sep. 12, 2007]
WHERE `id`=".$id
Try
WHERE `id`=$id"
[edited by: Receptional_Andy at 7:40 pm (utc) on Sep. 12, 2007]
<?php
// Connects to your Database
mysql_connect("localhost", "login", "password") or die(mysql_error());
mysql_select_db("news") or die(mysql_error());
// Query
$id = $_GET['id'];
$data = mysql_query("SELECT * FROM `news` WHERE `id`=$id");
or die(mysql_error());
// Results
while($info = mysql_fetch_array( $data ))
{
Print "";
Print "<center><b>".$info['title'] . " </b> - headline<br><br>";
Print "<b>".$info['story'] . " <br>";
}
Print "</table>";
?>
$data = mysql_query("SELECT * FROM `news` WHERE `id`=$id");
or die(mysql_error());
Looks like there is a misplaced semi-colon in that section:
$data = mysql_query("SELECT * FROM `news` WHERE `id`=$id") or die(mysql_error());
...
if((isset($_GET['n_id'])) && (is_numeric($_GET['n_id'])) ) {
$id=$_GET['n_id'];
include_once ('./includes/mysql_connect.php');
$query="SELECT headline, text, post_date FROM news WHERE n_id=$id";
$result=mysql_query($query);
$num=mysql_num_rows($result);
$row=(mysql_fetch_array($result, MYSQL_ASSOC));
} else {
echo '<h1> Page Error!</h1> <p> This page was accessed in error.</p>';
include ('./footer.html');
exit ();
}
etc.......
This only selects 1 article... but it may be helpful.
I then report it pretty much as you have
is there any better way to write this code?
A much more complex question! It depends what the code is for. If it's for learning PHP, then your code is fine. If it's for deploying onto a website, there's a way to go ;)