I need help in using a variable passed in a hyperlink query string to do PHP query of a MySQL database. I want use the variable to query the database and return the unique row field data to display in a table
The hyperlink is:

http://www.site.com/bios.php?pc=ab

After connecting to the database sucessfully do query like this maybe? need help writing code:need help setting guery string into variable

$per = $_GET['pc'];

Need help with query statement

$query = "select name,title,bio,picture from the_table where per=ab";

then use info to print table:

{
 Print "<tr><td class='ParaText' align='left' valign='top'>name</td><td class='ParaText'>".$name."</td><td class='ParaText' align='left' valign='top'>" .$bio."</td></tr>"; 
}

Recommended Answers

All 17 Replies

I need help in using a variable passed in a hyperlink query string to do PHP query of a MySQL database. I want use the variable to query the database and return the unique row field data to display in a table
The hyperlink is:

http://www.site.com/bios.php?pc=ab

After connecting to the database sucessfully do query like this maybe? need help writing code:need help setting guery string into variable

$per = $_GET['pc'];

Need help with query statement

$query = "select name,title,bio,picture from the_table where per=ab";

then use info to print table:

{
 Print "<tr><td class='ParaText' align='left' valign='top'>name</td><td class='ParaText'>".$name."</td><td class='ParaText' align='left' valign='top'>" .$bio."</td></tr>"; 
}

Your query should be like this

$per = $_GET['pc'];
$query = "select name,title,bio,picture from the_table where per=".$_GET['pc']."";

also dont forget to sanitize your database query, else you could be hit by sql injection.

Also as your parameter is not numeric, your query should be something like this

$query = "select name,title,bio,picture from the_table where per = '".mysql_real_escape_string($_GET['pc'])."'";

http://php.net/manual/en/function.mysql-real-escape-string.php

Many thanks.
Do I need another line of code to define the result or can I just go to the print table row part?

Try this, i wrote this top of my head so excuse me for errors

$query = "select name,title,bio,picture from the_table where per = '".mysql_real_escape_string($_GET['pc'])."'";

$recordset = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($recordset);
if($num_rows > 0)
{
while($result = mysql_fetch_array($recordset))
{
echo $result['column_name'];
}
}

Thanks for the extra code!
This query is for a distinct row. That is only one row has unique "pc" value in a field.
So would your code also enable me to print the following?:

{
echo $result['name','title','bio','picture'];
}
$recordset = mysql_query($query) or die(mysql_error());
$result = mysql_fetch_assoc($recordset))
$num_rows = mysql_num_rows($recordset);
if($num_rows > 0)
{
		echo $result['name'];
		echo $result['title'];
		echo $result['bio'];
		echo $result['picture'];
}

Thanks:
Can I also incorporate the field results in a table like this

{
      Print "<tr><td class='ParaText' align='left' valign='top'>name</td><td class='ParaText'>".$name."</td><td class='ParaText' align='left' valign='top'>" .$bio."</td></tr>";
      }

You can, if you assign the variables first. And for readablity I think it's easier to integrate the variables in the string (at least if you're using a decent PHP editor with syntax highlighting).

$name = $result['name'];
$bio = $result['bio'];
Print "<tr><td class='ParaText' align='left' valign='top'>name</td><td class='ParaText'>$name</td><td class='ParaText' align='left' valign='top'>$bio</td></tr>";

Thanks so let me try to put the parts together please review:

$query = "select name,title,bio,picture from the_table where per = '".mysql_real_escape_string($_GET['pc'])."'";
 
      $recordset = mysql_query($query) or die(mysql_error());

      $num_rows = mysql_num_rows($recordset);
   
      if($num_rows > 0)
 
      {
      while($result = mysql_fetch_array($recordset))
       $name = $result['name'];
       $bio = $result['bio'];
      {
      Print "<tr><td class='ParaText' align='left' valign='top'>name</td><td class='ParaText'>$name</td><td class='ParaText' align='left' valign='top'>$bio</td></tr>";
      }

      }

try this:

Print "<tr>
<td class='ParaText' align='left' valign='top'>name</td>
<td class='ParaText'>".$name."</td>
<td class='ParaText' align='left' valign='top'>".$bio."</td>
</tr>";

Thanks!
So now the code looks like this. Are the two lines of code under the while statement in the right place? Please review

$query = "select name,title,bio,picture from the_table where per =  '".mysql_real_escape_string($_GET['pc'])."'";
   
      $recordset = mysql_query($query) or die(mysql_error());
      $num_rows = mysql_num_rows($recordset);
      if($num_rows > 0)
    
      {
      while($result = mysql_fetch_array($recordset))
      $name = $result['name'];
      $bio = $result['bio'];
      {
      Print "<tr>
      <td class='ParaText' align='left' valign='top'>name</td>
      <td class='ParaText'>".$name."</td>
      <td class='ParaText' align='left' valign='top'>".$bio."</td>
      </tr>";
        }
        }

No it goes above the Print line (line 12).

Are you not testing this code?

Thanks so it would be like this:

$query = "select name,title,bio,picture from the_table where per = '".mysql_real_escape_string($_GET['pc'])."'";
       $recordset = mysql_query($query) or die(mysql_error());
      $num_rows = mysql_num_rows($recordset);
        if($num_rows > 0)
      {
      while($result = mysql_fetch_array($recordset))
     
        {
        $name = $result['name'];
        $bio = $result['bio'];
      Print "<tr>
      <td class='ParaText' align='left' valign='top'>name</td>
       <td class='ParaText'>".$name."</td>
        <td class='ParaText' align='left' valign='top'>".$bio."</td>
       </tr>";
       }
       }

Yep that should be fine

commented: very helpful, thanks +1

Many thanks to all!!!

if you are happy with the response, can you rate and mark it answered :-)

How to fetch data from the mysql database with querstring???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.