Jump to content

Simple issue


onthespot

Recommended Posts

<?php
$comp = mysql_real_escape_string($_GET['comp'];
$champion=mysql_query("SELECT champion FROM competitions WHERE comp_name = '$_GET[comp]' LIMIT 1");
$row = mysql_fetch_assoc($champion);
$champion2 = $row['champion'];
echo "<td>The reigning champion is <a href=\"userprofile.php?user=$champion\">$champion2</a></td>";

I escaped the $_GET (so you don't get injection attacks), and you weren't parsing the results.  I fixed those. If you want further explanation, let me know.

Link to comment
Share on other sites

This wont work...

 

$comp = mysql_real_escape_string($_GET['comp']);
$champion=mysql_query("SELECT * FROM competitions WHERE comp_name = $comp");
$row = mysql_fetch_assoc($champion);
$champion2 = $row['champion'];

 

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

 

Thats the error

Link to comment
Share on other sites

it doesn't seem to like the value of $champion

 

two things

1) add single 'quotes' to the $comp var (unless the value is an integer):

$champion=mysql_query("SELECT * FROM competitions WHERE comp_name = '$comp' ");

 

2) check for errors in your sql statement:

$champion=mysql_query("SELECT * FROM competitions WHERE comp_name = '$comp' ") or die(mysql_error() );

Link to comment
Share on other sites

try it this way

$cxn = mysqli_connect($host,$user,$passwd,$dbname)
          or die ("Couldn't connect");
$comp = mysql_real_escape_string($_GET['comp']);
$query = "SELECT * FROM competitions WHERE comp_name = $comp";

$result = mysqli_query($cxn,$query)
          or die ("Couldn't execute");
$row = mysql_fetch_assoc($champion);
$champion2 = $row['champion'];

Link to comment
Share on other sites

the or die is only for error handling.  you can take it out once you've solved the problem.

 

if you leave it in, if there is an error with the query it will break your page.  (probably a bad thing)

 

also, if someone does attempt a sql-injection, allowing them to see the error in your die() statement will just help them figure out how to improve their injection attack.  I vote take it out.

 

Link to comment
Share on other sites

That is why "...or die()" has no place at all. It is always always a bad idea, because there are better options that are as easy to use. Read the related blog on this site

 

...or trigger_error('myMsg', E_USER_ERROR);

 

will achieve the same in informing you, and it doesn't break the site, because you can control it by setting the error reporting levels (and there are more neat options, that you can figure out yourself in the php manual ;))

 

Bjom

 

 

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.