#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    56
    Rep Power
    3

    Coverting MySQL to MySQLi but failing


    Hi everyone here,

    I am trying to re-write my image gallery application into mysqli. However, when I launch the browser I am getting an error and I am not getting images printed:

    PHP Code:
    Parse errorsyntax errorunexpected T_ELSE in C:\Program Files\Abyss Web Server\htdocs\Lusaka-MUSEUM SEC MIN 16th march\gallery_sec.php on line 177 
    Here is the mysqli code I have ended up so far:
    PHP Code:
    <?php

    //set connection variables
    $host "localhost";
    $username "joseph";
    $password "";
    $db_name "collectionsdb"//database name

    //connect to mysql server
    $mysqli = new mysqli($host$username$password$db_name);

    /* check connection */
    if (mysqli_connect_errno()) {
        
    printf("Connect failed: %s\n"mysqli_connect_error());
        exit();
    }

        
    /* Create the prepared statement */
        
    if ($stmt $mysqli->prepare("SELECT c_id,ctitle,cfilename FROM collections WHERE c_id>0")) {    
        
        
    /* Execute the prepared Statement */
        
    $stmt->execute();
        
        
    /* Bind results to variables */
        
    $stmt->bind_result($c_id,$ctitle,$cfilename);

        
    /* fetch values */
        
    while ($rows $stmt->fetch()) { 
        
    $c_id=$rows['c_id'];
        
        
    // display records in a table

    ?>
        <td valign="top"> 
        
        <table width="150" border="1" align="left" class="allborder"> 
        <tr> 
        <td width="65" bgcolor="#eeeeee"><a href="details_sec.php?c_id=<?php echo $c_id?>" > <?php echo $rows['ctitle']; ?> </a></td>
        </tr>
        <tr> 
            <td><a href=details_sec.php?c_id=<?php echo $c_id ?> ><img src="./images/<?php echo $rows['cfilename']; ?>" width="95" height="120" alt="" /></a>
            </td> 
        </tr> 
             
        </table>

     <tr>

     </tr>
     </td>

    <?php
        
    }

        
    /* Close the statement */
        
    $stmt->close();

    else {
        
    /* Error */
        
    printf("Prepared Statement Error: %s\n"$mysqli->error);
    }

    /* close our connection */
    $mysqli->close();

    ?>
    Line 177 error refers to:

    else {
    May you kindly advise me on this?

    Joseph
    Last edited by josephbupe; March 16th, 2013 at 03:24 AM.
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,555
    Rep Power
    2338
    Well a big clue is the error message itself, as usual. You have an "unexpected else". Now, put on your thinking cap and put some effort into deducing how you might end up with an "else" that is "unexpected".
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    well, the error may not be that easy to find: The $stmt->close(); must be moved one block up, after the end of the loop.

    In order to avoid such errors, you should use the long syntax for control structures:

    PHP Code:
    if ($some_cond):
        while (
    $another_cond):
        
        
    // a lotta code
        
        
    endwhile;
    endif; 
    This makes it much easier to identify the beginning and the end of a block.

    Or even better: Untangle the PHP/HTML mixture. Either by using an external template engine or by putting the main PHP code on top and the HTML below (simply use the $stmt variable later).

    By the way, when you have a constant query without any external values, you don't need a prepared statement. Do a simple query().
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo