IWishIUnderstoodItAll Posted February 5, 2010 Share Posted February 5, 2010 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 but being the n00b that I am it felt like it didn't do me any good. Quote Link to comment https://forums.phpfreaks.com/topic/191080-help-the-new-guy-phpmysql/ Share on other sites More sharing options...
schilly Posted February 5, 2010 Share Posted February 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191080-help-the-new-guy-phpmysql/#findComment-1007544 Share on other sites More sharing options...
alhen Posted February 5, 2010 Share Posted February 5, 2010 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/191080-help-the-new-guy-phpmysql/#findComment-1007546 Share on other sites More sharing options...
alhen Posted February 5, 2010 Share Posted February 5, 2010 Oh, and you could add this after that other code to test if your "id" is being passed through: <? echo $id; ?> Quote Link to comment https://forums.phpfreaks.com/topic/191080-help-the-new-guy-phpmysql/#findComment-1007548 Share on other sites More sharing options...
IWishIUnderstoodItAll Posted February 5, 2010 Author Share Posted February 5, 2010 Thx for the tips, I'll try it out as soon as I get home! Quote Link to comment https://forums.phpfreaks.com/topic/191080-help-the-new-guy-phpmysql/#findComment-1007565 Share on other sites More sharing options...
ignace Posted February 5, 2010 Share Posted February 5, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/191080-help-the-new-guy-phpmysql/#findComment-1007647 Share on other sites More sharing options...
IWishIUnderstoodItAll Posted February 7, 2010 Author Share Posted February 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/191080-help-the-new-guy-phpmysql/#findComment-1008520 Share on other sites More sharing options...
IWishIUnderstoodItAll Posted February 8, 2010 Author Share Posted February 8, 2010 *shameless bump* I've exhausted my brain and I'm ready to give up Quote Link to comment https://forums.phpfreaks.com/topic/191080-help-the-new-guy-phpmysql/#findComment-1008777 Share on other sites More sharing options...
schilly Posted February 8, 2010 Share Posted February 8, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191080-help-the-new-guy-phpmysql/#findComment-1008990 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.