Unable to fetch data using PHP MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sourav.network
Forum Newbie
Posts: 2
Joined: Mon Dec 15, 2014 4:47 pm

Unable to fetch data using PHP MySQL

Post by sourav.network »

Hi,

Issue: Unable to fetch record (User_ID) which then needs to be passed over to new PHP page (information.php).
Error: When I place the mouse over the records, it displays: localhost/phptest/information.php?id=

Scriptpage.php (Problem is here):

Code: Select all

<?php
	include('conn.php');

	$strlen = strlen($_GET['content']);
	$display_count = $_GET['count'];

	$select = "select * from markets_info where User_ID like '%".$_GET['content']."%' OR Market_Name like '%".$_GET['content']."%'";
	$res = mysql_query($select);
	$rec_count = mysql_num_rows($res);
    if($display_count)
	{
	  echo "There are <font color='red' size='3'>".$rec_count."</font> matching records found. Click Search to view result.";
	}
	else
	{
?>
    <center>   
	<table class="sofT" cellspacing="0" border="1">
	<tr>
		<td colspan="10" class="helpHed" align="center">Search Result</td>
	</tr>
	<tr>
	   <td class='helpHed'>Market Name</td>
	   <td class='helpHed'>Days</td>
	   <td class='helpHed'>Time</td>
    </tr>
<?php
	if($rec_count > 0)
	{
		while($data=mysql_fetch_array($res))
		{ 
			echo "<tr>
			<td><a href=information.php?id=$res[User_ID]>".$data['Market_Name']."</td>
			<td>".$data['Days']."</td>
		    <td>".$data['Time']."</td>
			</tr>";

				 
		}
	}
	else
		echo '<td colspan="5" align="center"><FONT SIZE="2" COLOR="red">No records found</FONT></td>';
  }
?>
</center>
When this script runs, it displays all records correctly but when I click on the Market_Name field, it doesn't direct to right page with User_ID.

Please help!!!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Unable to fetch data using PHP MySQL

Post by requinix »

Using the wrong variable: $res is a resource describing the query, $data is an actual row of data from it.

Separately,
1. Your code is vulnerable to SQL injection. If I were a malicious user I could do Bad Things to your website and your database.
2. You're using the mysql_* functions which are old, slow, and no longer supported. You should try to move to the mysqli functions or PDO. They offer prepared statements which can protect you from SQL injection.
sourav.network
Forum Newbie
Posts: 2
Joined: Mon Dec 15, 2014 4:47 pm

Re: Unable to fetch data using PHP MySQL

Post by sourav.network »

Hi,
Even when I use $data, its still the same.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Unable to fetch data using PHP MySQL

Post by requinix »

Have you outputted $data to make sure everything has a User_ID value?
Post Reply