hi guys,

i need your help. i has been searching through on google as i am looking for a php code where i can just stored the images link in the mysql database table, and then display the images. Most of the search results on google they are for stored the images in mysql which is a pretty bad idea. I just want to store the images link in mysql and display the images in php.

I can't find it, please can you help?

Cheers.

    // First Connect to your database, i prefer myqli extension;
    $db = new mysqli($host,$user,$pass,$dbname);
    
    // Then we get links from the database
    $sql = "SELECT image_url as link FROM image_links";
    $result = $db->query($sql);
    
    // check for errors
    if( !$result ) die($db->errno . ': ' . $db->error);
    if( !$result->num_rows > 0 ) die('Query returned no rows');
    
    // then we start a loop
    while( $row = $result->fetch_assoc() ) {
       // Then we use the loop to echo images with an html line break 
       echo '<img src="' . $row['link'] . '" /><br />';
    // then we close the loop
    }
    
    //finally free the result
    $result->free();
    
    

      Thanks for your quick replied Derokorian and thanks for your help, do you know how I can set the description text next to the images without set them under the images?

      something got to do with this line:

        echo "<img src ='".$image_path."'>",  "<p id='text_description'>", $row['text_description'] . "</p>" ;

      here's the current code:

      <?php
      session_start();
          define('DB_HOST', 'localhost');
          define('DB_USER', 'mydbusername');
          define('DB_PASSWORD', 'mydbpassword');
          define('DB_DATABASE', 'mydbname');
      
      $errmsg_arr = array();
      $errflag = false;
      
      $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
      if(!$link) {
        die('Failed to connect to server: ' . mysql_error());
          }
      
      $db = mysql_select_db(DB_DATABASE);
      if(!$db) {
      
      die("Unable to select database");
          }   
      
        function clean($var){
      
      return mysql_real_escape_string(strip_tags($var));
          }
          $username = clean($_GET['user']);
          $pass = clean($_GET['pass']);
      
      
      if($errflag) {
        $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
        echo implode('<br />',$errmsg_arr);
         }
         else {
      
      $insert = array();
      if(isset($_GET['user'])) {
          $insert[] = 'username = \'' . clean($_GET['user']) .'\'';
      }
      if(isset($_GET['pass'])) {
          $insert[] = 'pass = \'' . clean($_GET['pass']) . '\'';
      }
      
      
      
      if (count($insert)>0) {
         $names = implode(',',$insert);
      
      
      if($username) {
        $qrytable1="SELECT username, images, text_description FROM user_list WHERE username='$username'";
        $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());
      
      while ($row = mysql_fetch_array($result1)) {
        $image_path = $row['images'];
        echo "<img src ='".$image_path."'>",  "<p id='text_description'>", $row['text_description'] . "</p>" ;
        }
       }
      }
      }
      ?>

      Any help would be much appreciated! 🙂

        If i understand you're just looking to format the displayed outcome?

        echo '
           <div>
              <img style="float:left;" src="'. $image_path .'">
              <p class="text_description">'. $row['text_description'] .'</p>
           </div>
        ' ;
        

        Remember get it to look like what you want, with hard coded values, then put that formatting into php and exchange hard coded values for what should be dynamic.

        HTH

          Thanks, i have pasted the code into my php script and uploaded on my website.

          When I take a test, there is a error:

          Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/myusername/public_html/link.php on line 59

          here's the code:

          echo '
             <div>
                <img style="float:left;" src="'. $image_path .'">
                <p class="text_description">'. $row['text_description'] .'</p>
             </div>
          ';
            Write a Reply...