Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to count number of rows within a ms sql server to find an amount of rows to specific values
    primarykey
    data
    text
    <p>I know there have been lots of questions about this but I need some specific help, I'm new to sql and php, so sorry for bad coding and poor indenting. What I am trying to do is count the number of rows to a specific column then when I find out how many have this id from this column then I would like it to send this number back to the php page. ATM I am using a like query I don't want this effected.</p> <p>My layout is 3 pages the index the part choices and the part location, the user inputs a part number into the index page which is then searched against the database which then displays the parts that match or are like the users input on the part choices page. then you click on the part you want and then it takes you to the locations page and showing the closest locations for this part. </p> <p>What I am trying to do is when the user puts in the part number a query runs and searches the database counting the number of rows that have or are like the input, then outputs the parts like it usually does, but what i want to happen is if there is only one row with that part number i want it to say row = 1 so i can then run an if statement using this value. i have looked into different code and cant quiet find what I'm looking for these are the examples I have found and have tryied to modify for what I need. but i have had no luck.</p> <pre><code>$query = "SELECT (column) FROM table WHERE column = value"; 2.$results = mysql_query($query); 3.$rows = mysql_num_rows($results); 4.echo $rows ; </code></pre> <p>seperate code below </p> <pre><code>&lt;?php $serverName = "serverName\instanceName"; $connectionInfo = array( "Database"=&gt;"dbName", "UID"=&gt;"username", "PWD"=&gt;"password"); $conn = sqlsrv_connect( $serverName, $connectionInfo ); if( $conn === false ) { die( print_r( sqlsrv_errors(), true)); } $sql = "SELECT FirstName, LastName FROM SomeTable"; $stmt = sqlsrv_query( $conn, $sql ); if( $stmt === false) { die( print_r( sqlsrv_errors(), true) ); } while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) { echo $row['LastName'].", ".$row['FirstName']."&lt;br /&gt;"; } sqlsrv_free_stmt( $stmt); ?&gt; </code></pre> <p>i would like the code or query to be within the php that i have, any ideas would be great or examples sorry about poor english many thanks </p> <p>my code for my page is below:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;head&gt; &lt;meta name="viewport" content="width=device-width, user-scalable=false;"&gt; &lt;script type="text/javascript"&gt; function submit() { document.getElementById("start").click(); // Simulates button click document.submitForm.submit(); // Submits the form without the button } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;?php try { $serverName = "188.64.188.89"; $connectionInfo = array( "Database"=&gt;"tdStoreLocator", "UID"=&gt;"odbcAdmin", "PWD"=&gt;"Midnight1Midnight1"); $conn = sqlsrv_connect( $serverName, $connectionInfo ); if( $conn === false ) { die( print_r( sqlsrv_errors(), true)); } $sql = "SELECT TOP 10 [company] ,[partnum] ,[description] FROM [tdStoreLocator].[odbcadmin].[Part] WHERE Part.partnum LIKE ? or Part.description LIKE ?"; /* Set parameter values. */ $params = array( "%" . str_replace(" ","%",$_POST["part"] ). "%", "%" . str_replace(" ","%",$_POST["part"] ) . "%"); $i = 0; $x = true; /*echo print_r($params, true);*/ $stmt = sqlsrv_query( $conn, $sql, $params ); if( $stmt === false) { die( print_r( sqlsrv_errors(), true) ); } while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) { if($x == true) { echo"&lt;form id=\"submitForm\" action=\"locations.php\" method=\"post\"&gt;"; echo"&lt;input type=\"hidden\" name=\"part\" id=\"3\" value=\"".$row['partnum']."\"&gt;"; echo"&lt;input type=\"hidden\" name=\"lon1\" id=\"1\" value=\"".$_POST["lon1"]."\"&gt;"; echo"&lt;input type=\"hidden\" name=\"lat1\" id=\"2\" value=\"".$_POST["lat1"]."\"&gt;"; echo"&lt;button id=\"start\" type=\"submit\"&gt;"; echo "&lt;div style=\"font-family:verdana;font-weight:bold;color:#3083FF;font-size:20px;width:100%;text-align:center;margin:0; padding:0;\"&gt;"; echo $row['partnum']."&lt;br/&gt;"; echo "&lt;/div&gt;"; echo"&lt;img style=\"width:50%;\"; src=\"productimages/".$row['partnum'].".jpg\" alt=\"Save icon\" onError=\"this.src='productimages/noimage.jpg'\"/&gt;"; echo "&lt;div style=\"font-family:verdana;color:#3083FF;font-size:20px;width:100%;text-align:center;margin:0; padding:0;\"&gt;"; echo $row['description']; echo "&lt;/div&gt;"; echo"&lt;/button&gt;"; echo"&lt;/form&gt;"; } $i++; } sqlsrv_free_stmt( $stmt); } catch (Exception $e) { echo 'Caught exception: ', $e-&gt;getMessage(), "\n"; } if($i &lt; 1) { echo "&lt;div style=\"font-family:verdana;font-weight:bold;color:#3083FF;font-size:20px;width:100%;text-align:center;\"&gt;"; echo "No results found, Please check your spelling of the part number or description."; echo "&lt;/div&gt;"; } if($i == 1 ) { echo"&lt;img onload=\"setTimeout(submit(),00001);\" src=\"index.jpg\" onError=\"this.src='productimages/noimage.jpg'\"/&gt;"; } ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload