Solved

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

Posted on 2014-02-20
5
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 24

Expert Comment

by:mankowitz
ID: 39873732
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
ID: 39873746
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 110

Expert Comment

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

Author Comment

by:IT Tech
ID: 39873792
I figured it out myself.
0
 

Author Closing Comment

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

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 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