Jump to content

ilkist

Members
  • Posts

    13
  • Joined

  • Last visited

    Never

Posts posted by ilkist

  1. I don't want to confuse anyone, so here's a 100% working example of connecting to a MS SQL server using PDO. This method has detailed error reporting enabled.

     

    $servername = "xxxxx";
    $database = "xxxxx";
    $user = "xxxxx";
    $pass = "xxxxx";
    
    try {
    $DBH = new PDO('sqlsrv:Server='.$servername.';Database='.$database, $user, $pass);
    $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    }
    catch(PDOException $e) {
    echo $e->getMessage();
    }
    

  2. **EDIT**

     

    I just solved my own problem...

     

    instead of

    try {
    $DBH = new PDO("mssql:host=xxxx;dbname=xxxx", 'xxxx', 'xxxx');
    }
    catch(PDOException $e) {
    echo $e->getMessage();
    }
    

     

    I did

    try {
    $DBH = new PDO("sqlsrv:host=xxxx;dbname=xxxx", 'xxxx', 'xxxx');
    }
    catch(PDOException $e) {
    echo $e->getMessage();
    }
    

     

    I was using the old method of connecting to the DB...

  3. I'm using PHP Version 5.3.8 that was installed via XAMPP along with Microsoft SQL Server 2008 R2 (SQLEXPRESS). I have the drivers installed correctly (I think) and have added the correct line into php.ini (extension=php_pdo_sqlsrv_53_ts_vc9.dll to be exact). I'm trying to connect to the server like so:

    try {
    $DBH = new PDO("mssql:host=xxxx;dbname=xxxx", 'xxxx', 'xxxx');
    }
    catch(PDOException $e) {
    echo $e->getMessage();
    }
    

     

    I get the "could not find driver" error, and I've tweaked it all kinds of ways to solve the problem. I've tried all other kinds of drivers, but this is the only one that Apache doesn't give me an error on startup. When I run phpinfo(), the pdo_sqlsrv fields are all blank except pdo_sqlsrv.log_severity which is set to 0.

     

    I DL'd my drivers from http://www.microsoft.com/en-us/download/details.aspx?id=20098, and I've tried both 2.0 and 3.0

     

    Any advice would be awesome!!

  4. I tweaked my original function a bit, implemented some of your ideas, and here's what I wrote:

     

    <?php
    function newstalkq(){
    	extract($GLOBALS);
    
    	$data = $DBH->query('SELECT ClipNo, Title FROM Clippings ORDER BY PubDate DESC LIMIT 6');
    	$data->setFetchMode(PDO::FETCH_ASSOC);
    
    	while($row = $data->fetch()) {
    		echo '<li><a href="/newstalk/#'.$row['ClipNo'].'">' . $row['Title'] . '</a></li>';
    	}
    
    	$DBH = null;
    }
    ?>
    
    <html>
    <body>
    			<div class="col380r"> <!-- may not need class here -->
    				<a href="/newstalk/"><img src="images/ntt_home_logo.jpg"  class="logo" alt="NTT logo" /></a>
    				<ul id="newsTalk"><?php newstalkq() ?></ul>
    			</div>
    </body>
    </html>
    

     

    It's simple, and it works exactly the way I want it to without me having to change anything but the database to update the list =)

    Thanks for your help! I really do appreciate it.

  5. oh dear how silly of me, I forgot this part of the code after the html <li> tags ><

     

    <%
    rstMN.MoveNext
    Loop
    rstMN.Close
    Set rstMN = nothing
    %>
    

     

    Wasn't trying to frustrate anyone =P

     

     

    So the whole snippet looks like this:

     

    <%
    Set rstMN = Server.CreateObject("ADODB.RecordSet")
    rstMN.ActiveConnection = SQLSrvConn2
    
    rstMN.Source = "SELECT TOP 6 Clippings.ClipNo, Clippings.Title FROM Clippings WHERE Clippings.Edited=1 AND Clippings.InActive=0 ORDER BY Clippings.PubDate DESC"
    
    rstMN.CursorType=adOpenStatic
    rstMN.LockType=adLockReadOnly
    rstMN.Open
    %>
    
    <html>
    </body>
    <div class="col380r"> <!-- may not need class here -->
    	<a href="/newstalk/"><img src="images/ntt_home_logo.jpg"  class="logo" alt="NTT logo" /></a>
    	<ul id="newsTalk">
    <%
    Do While Not rstMN.EOF
    %>
    		<li><a href="/newstalk/#<%= rstMN("ClipNo") %>"><%= rstMN("Title") %></a></li>
    <%	
    rstMN.MoveNext
    Loop
    rstMN.Close
    Set rstMN = nothing
    %>
    	</ul>
    </div>
    </body>
    </html>
    

     

    So you are correct about me having a loop in there, thanks for reminding me =) I guess I skimmed over that part yesterday and it slipped my mind.

  6. I know, as of now, it doesn't accept any parameters, but don't worry because I'll change that as soon as I get this figured out. It's just mind boggling since my site is currently in the old .asp, and I could it to work easily with that. Since trying to pickup on php, i've run into tons of problems.

     

    Here's what I had

    <%
    Set rstMN = Server.CreateObject("ADODB.RecordSet")
    rstMN.ActiveConnection = SQLSrvConn2
    
    rstMN.Source = "SELECT TOP 6 Clippings.ClipNo, Clippings.Title FROM Clippings WHERE Clippings.Edited=1 AND Clippings.InActive=0 ORDER BY Clippings.PubDate DESC"
    
    rstMN.CursorType=adOpenStatic
    rstMN.LockType=adLockReadOnly
    rstMN.Open
    %>
    
    <html>
    <li><a href="/newstalk/#<%= rstMN("ClipNo") %>"><%= rstMN("Title") %></a></li>
    </html>
    

  7. So why not put the anchor in the loop too?

    while($row = $data->fetch()) {
    echo '<li><a href="#">' . $row['Title'] . '</a></li>';
    }

     

    I'm sure that will work for now, but ultimately I want to have the function setup to where I can use it like this:

    <li><a href="/newstalk/#<?php newstalkq('ClipNo)' ?>"><?php newstalkq('Title') ?></a></li>
    

     

    So that each Title is linked to its designated ClipNo. If I could figure out how to get the listing done correctly,

    then I'll move on to the next step.

  8. I'm trying to figure out how to do this correctly.

     

    Here's the  function I've come up with:

     

    <?php
    function newstalkq(){
    	extract($GLOBALS);
    
    	$data = $DBH->query('SELECT Title FROM Clippings ORDER BY PubDate DESC');
    	$data->setFetchMode(PDO::FETCH_ASSOC);
    
    	while($row = $data->fetch()) {
    		echo $row['Title'];
    	}
    }
    ?>
    
    <html>
    <li><a href="/newstalk/#>"><?php newstalkq() ?></a></li>
    </html>
    

     

    The way that snippet looks is something like this:

     

     

    I've been trying with no luck to get it to look like this:

     

    Although, I can get it to list out like that by adding the <li></li> tags around the $row['Title'] line inside the while loop,

    but that just makes the whole list into one giant hyperlink. What I want to happen is have each Title on their own line

    and each having a separate hyperlink.

     

     

  9. I'm trying to convert this ASP function into a working PHP function with no luck =(

     

    I want to ask if anyone out here knows how to do this, and I would love you forever if you did.

     

    Private Function getdate(beg, en)
        beginYear = Year(beg)
        endYear = Year(en)
        months = Array("0", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
        If (beginYear < 1000) Then beginYear = beginYear + 1900 End If
        If (endYear < 1000) Then endYear = endYear + 1900 End If    
        
        getdate = months(Month(beg)) & " " & day(beg)
        If Not Year(beg) = Year(en) Then
          getdate = getdate& (", " & beginYear)
        End If
        If Not Month(beg) = Month(en) Or Not Day(beg) = Day(en) Then
          getdate = getdate& (" - ")
        End If
        If Not Month(beg) = Month(en) Then
          getdate = getdate& (" " & months(Month(en)))
          If Day(beg) = Day(en) Then
            getdate = getdate& (" " & Day(en))
          End If
        End If
        If Not Day(beg) = Day(en) Then
          getdate = getdate& (" " & Day(en))
        End If
        getdate = getdate& (", " & endYear)
    End Function
    

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.