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

    Join Date
    Nov 2012
    Rep Power

    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:

    //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());

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

    /* fetch values */
    while ($rows $stmt->fetch()) { 
    // display records in a table

        <td valign="top"> 
        <table width="150" border="1" align="left" class="allborder"> 
        <td width="65" bgcolor="#eeeeee"><a href="details_sec.php?c_id=<?php echo $c_id?>" > <?php echo $rows['ctitle']; ?> </a></td>
            <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>




    /* Close the statement */

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

    /* close our connection */

    Line 177 error refers to:

    else {
    May you kindly advise me on this?

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

    Join Date
    Dec 2003
    Rep Power
    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
    Rep Power

    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 (
    // a lotta code
    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