Forum Moderators: coopster

Message Too Old, No Replies

Select variable in query - PLEASE HELP!

database results - info display

         

akelly65

6:38 pm on Sep 12, 2007 (gmt 0)

10+ Year Member



hi there,

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. "'");

Dilly

6:43 pm on Sep 12, 2007 (gmt 0)

10+ Year Member



Try this

$data = mysql_query("SELECT * FROM `news` WHERE `id`=".$id);

akelly65

7:09 pm on Sep 12, 2007 (gmt 0)

10+ Year Member



thank you

seems to be working thanks, but it is now telling me there is an error with a simple:

or die(mysql_error());

if I delete that

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

the line below that?

Dilly

7:16 pm on Sep 12, 2007 (gmt 0)

10+ Year Member



take off the semi colon at the end of this

$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]

akelly65

7:25 pm on Sep 12, 2007 (gmt 0)

10+ Year Member



took out the semi colon, returned below.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

haha, sorry to be a pain, it just dosn't seem to be going right, feel like I am banging my head against a wall.

Receptional Andy

7:40 pm on Sep 12, 2007 (gmt 0)



WHERE `id`=".$id

Try

WHERE `id`=$id"

[edited by: Receptional_Andy at 7:40 pm (utc) on Sep. 12, 2007]

akelly65

7:52 pm on Sep 12, 2007 (gmt 0)

10+ Year Member



Can you spot any other errors, have tried all the suggestions, still not parsing properly, is there any better way to write this code? or is this correct for what I am trying to achieve?


<?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>";
?>

Receptional Andy

8:11 pm on Sep 12, 2007 (gmt 0)



$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());

russkern

8:12 pm on Sep 12, 2007 (gmt 0)

10+ Year Member



Here is how I write a similar script

...

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

Receptional Andy

8:17 pm on Sep 12, 2007 (gmt 0)



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 ;)

akelly65

8:23 pm on Sep 12, 2007 (gmt 0)

10+ Year Member



thanks all!

got it working using below, thanks russkern for the tip also.


$data = mysql_query("SELECT * FROM `news` WHERE `id`='.$id'") or die(mysql_error());