Page 1 of 1

Unable to fetch data using PHP MySQL

Posted: Mon Dec 15, 2014 5:01 pm
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!!!

Re: Unable to fetch data using PHP MySQL

Posted: Mon Dec 15, 2014 5:30 pm
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.

Re: Unable to fetch data using PHP MySQL

Posted: Mon Dec 15, 2014 5:45 pm
by sourav.network
Hi,
Even when I use $data, its still the same.

Re: Unable to fetch data using PHP MySQL

Posted: Mon Dec 15, 2014 6:04 pm
by requinix
Have you outputted $data to make sure everything has a User_ID value?