Jump to content

Help the new guy, php+MySQL


Recommended Posts

Heya!

I'm trying to make a link that refers to a .php document that executes a MySQL query. I can't however seem to get it to work. This is what the .php document that the link refers to looks like:

 

<?php
$con = mysql_connect("localhost","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("boardtesting", $con);

$q = mysql_query("SELECT * FROM `topic` WHERE `id` = " . intval($_GET["id"])) or die(mysql_error()); 
$arr = mysql_fetch_array($q));

mysql_close($con);
?>

 

 

 

I don't know where I've gone wrong tbh, maybe it's the $_GET-part or maybe it's something else. Plz help :( and I did try to  :rtfm: but being the n00b that I am it felt like it didn't do me any good.

Link to comment
Share on other sites

i would start with this:


<?php
print_r($_GET);

$con = mysql_connect("localhost","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("boardtesting", $con);

$q = mysql_query("SELECT * FROM `topic` WHERE `id` = " . intval($_GET["id"])) or die(mysql_error()); 
echo $q;
$arr = mysql_fetch_array($q));

mysql_close($con);
?>

 

that will print your GET array so you can see if the GET var is being passed or not then echo out your sql statement so you can see if it looks right or not.

Link to comment
Share on other sites

If I understand, right... this seems simpler:

 <?php
$id=$_GET['id'];

MYSQL_CONNECT(localhost, username, password) OR DIE("DB connection unavailable");
@mysql_select_db( "database") or die( "Unable to select database"); 

$query="SELECT * FROM table WHERE id='$id' "; 

mysql_query($query);	
mysql_close();
?>

Link to comment
Share on other sites

If I understand, right... this seems simpler:

 <?php
$id=$_GET['id'];

MYSQL_CONNECT(localhost, username, password) OR DIE("DB connection unavailable");
@mysql_select_db( "database") or die( "Unable to select database"); 

$query="SELECT * FROM table WHERE id='$id' "; 

mysql_query($query);	
mysql_close();
?>

 

Sure and when I pass "?id=1 OR 1=1" as my id I would get all his table records. Try schilly's code though I would advice to use trigger_error() in the future instead of die()

 

You can write:

$con = mysql_connect("localhost","root");
if (!$con)

 

Like:

if (!($con = mysql_connect("localhost","root")) {

 

Depends on what you like alo try to avoid dubble quotes (") if you don't need the extra processing power (eg "hello $world") and use single quotes (') instead 'hello $world' outputs hello $world and does not parse any contained variables.

 

Hope this helps, cheers!

Link to comment
Share on other sites

I decided to scrap it all and start over fresh but I'm stuck again.

This is what I got so far:

<?php


	echo "<strong>All the topics that got reported</strong>";
	echo "<br>";
	function topics_reported(){


	$query  = "SELECT topics.reported AS Number_of_reports, 

	topics.ip AS Users_IP_number, 
	topics.username AS Users_username,
	id
	FROM news_comments 
	WHERE reported > 0 ORDER BY abused DESC";
	$result = db_query($query);

		while($r = db_fetch_array($result)) {

   	 			print_r($r);
			$link = "change.php";
			$linktext = "Click to change reports";


			echo "<a href=\"$link\">$linktext</a>";
			echo "<br>";

		}	




	}

?>

 

 

This query lists all the topics that has gotten reports in them along with the users IP#, username and the id of the topic.

Now in the change.php I want two links, one to set the reports to 0 and one to delete the topic itself, both these links points to different .php files with the MySQL query in them. I already know how the query the database and get the correct results but I don't know how to get the id from the query above to the other queries. Anyone got any ideas? All help is appreciated, even the "Stupid n00b, l2program"-posts  ;)

Link to comment
Share on other sites

basically you want to pass some kind of identifier in your links to the other files, usually through the GET array.

 

so

<?php


	echo "<strong>All the topics that got reported</strong>";
	echo "<br>";
	function topics_reported(){


	$query  = "SELECT topics.reported AS Number_of_reports, 

	topics.ip AS Users_IP_number, 
	topics.username AS Users_username,
	id
	FROM news_comments 
	WHERE reported > 0 ORDER BY abused DESC";
	$result = db_query($query);

		while($r = db_fetch_array($result)) {

   	 			print_r($r);
			$link = "change.php?username=" . $r['Users_username'];
			$linktext = "Click to change reports";


			echo "<a href=\"$link\">$linktext</a>";
			echo "<br>";

		}	




	}

?>

 

look at the link i use. now in my other file use

$username = $_GET['username'];

 

to access that variable. now I can use that in my db query. the variable can be interchanged to whatever you need.

 

hope this helps. let me know if you run into any problems.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.