Solved

SQL query works in SQL editor but returns no results in PHP

Posted on 2014-02-20
5
296 Views
Last Modified: 2014-02-25
Hi All,

   I'm having an issue with PHP returning results on a query.  It returns results when I use the SSMS but not in PHP.  I'm perplexed...  (ignore the lack of css lol)

		$telno = $_POST['telno'];

	$newtelno = preg_replace("/[^0-9]/", "", $telno);

	$sql = "select [dbo].[MERCHANT].*, replace(replace([MERCHANT].[CONTACT_PHONE], '-', ''), ' ', '') as CTPHONE from [dbo].[MERCHANT] where replace(replace([dbo].[MERCHANT].[CONTACT_PHONE], '-', ''), ' ', '') = '" . $newtelno . "'";

	$stmt = sqlsrv_query( $conn, $sql );
	if( $stmt === false) {
	    die( print_r( sqlsrv_errors(), true) );
	}

	$row_count = sqlsrv_num_rows( $stmt );
	echo "Row Count: " . $row_count . "<br>";

	while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {

 		echo "<tr><td valign=\"top\" bgcolor=$bgcc><font size=4 face=\"Arial\">" . $row[0] . "</td><td bgcolor=$bgcc valign=\"top\"><font size=3 face=\"Arial\">" . $row[1] . "</td><td valign=\"top\" bgcolor=$bgcc><font font size=3 face=\"Arial\">" . $row[2] . "</td><td valign=\"top\" bgcolor=$bgcc><font font size=3 face=\"Arial\">" . $row[3] . "</td><td valign=\"top\" bgcolor=$bgcc><font font size=3 face=\"Arial\">" . $row[4] . "</td><td valign=\"top\" bgcolor=$bgcc><font font size=3 face=\"Arial\">" . $row[5] . "</td><td valign=\"top\" bgcolor=$bgcc><font font size=3 face=\"Arial\">" . $row[6] . "</td><td valign=\"top\" bgcolor=$bgcc><font font size=3 face=\"Arial\">" . $row[7] . "</td></tr>";
		echo "<tr><td>&nbsp;</td></tr>";

	}

	sqlsrv_free_stmt( $stmt);

Open in new window

0
Comment
Question by:IT Tech
  • 3
5 Comments
 
LVL 24

Expert Comment

by:mankowitz
Comment Utility
What error is it giving you? Can you post some sample data and a use-case?
0
 

Accepted Solution

by:
IT Tech earned 0 total points
Comment Utility
It's not giving an error at all, it's just not returning any results which has me confused.  I'm currently just taking an identical script and changing it again in case I screwed up something lol.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
When you say "not returning any results" what is the output from line 13?
0
 

Author Comment

by:IT Tech
Comment Utility
I figured it out myself.
0
 

Author Closing Comment

by:IT Tech
Comment Utility
I figured it out - I was pulling an associative array instead of numeric.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now