Jump to content

PHP Array


KingTut

Recommended Posts

I am trying to use a array to display 6 items instead of doing every single one alone. But I can't figure out how to fetch data from another table within the array.

//first part 
$slots = array();
		for($i = 1; $i <= 6; $i++) {
			$query = mysql_query("SELECT * FROM data1 WHERE user_id = $info[id] AND s = $i");
			$slots[$i] = mysql_fetch_assoc($query);
			if($slots[$i]) {
			} else {
				$slots[$i] = "Empty";
			}
		}
                           
                          //second part
                           		for($i = 1; $i <= 6; $i++) {
			if($slots[$i] != "Empty") {
			$pquery[$i] = mysql_query("SELECT * FROM data2 WHERE id = ".$slots[$i]["user_id"]." ");
			$slot2 = mysql_fetch_row($pquery[$i]) or die(mysql_error());

 

Link to comment
Share on other sites

The first and most important thing you need to do is stop using the dangerous and obsolete mysql_* code. You need to use PDO and Prepared Statements. Here is a tutorial to get you going. https://phpdelusions.net/pdo

You can also download my PDO Bumpstart Database from my repo. It was written for folks just like yourself. https://github.com/benanamen/pdo_bumpstart_ver1.6

Link to comment
Share on other sites

and after you switch to use the php PDO extension, your goal should be to execute one single JOIN query that gets the related data that you want in the order that you want it.

you should never execute SELECT queries inside of loops. and while it will probably go away when you convert to use the PDO extension, a fetch() statement doesn't cause a database error, so using or die() logic on the end of a fetch() statement doesn't do anything useful. when you switch to use the PDO extension, you should use exceptions to handle the database errors, and in most cases, let php catch the exception, where it will use it's error_reporting, display_errors, and log_errors settings to control what happens with the actual error information.

Link to comment
Share on other sites

  • 2 weeks later...

I am giving the example to use an array to display 6 items.

<html>
   <body>
   
      <?php
         /* First method to create array. */
         $numbers = array( 1, 2, 3, 4, 5,6);
         
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
         
         /* Second method to create array. */
         $numbers[0] = "one";
         $numbers[1] = "two";
         $numbers[2] = "three";
         $numbers[3] = "four";
         $numbers[4] = "five";
         $numbers[5] = "Six";
         foreach( $numbers as $value ) {
            echo "Value is $value <br />";
         }
      ?>
      
   </body>
</html>

You can get help from the above example. If you have any problem then you may visit my webpage : PHP Training in Noida

Link to comment
Share on other sites

So it should look something like this

/*  PDO CONNECTION *********************************************/
    $host = '?';
    $database = '?';
    $dbuser = '?';
    $dbpass = '?';
    
    $dsn = "mysql:dbname=$database; host=$host; charset=utf8";

    $pdo = new pdo($dsn, $dbuser, $dbpass, 
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_EMULATE_PREPARES => false,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        ]);
/***************************************************************/        

$sql = "SELECT d1.user_id            --
             , d1.col1               --
             , d1.col2               --  specify the cols you need, not '*'
             , d2.col3               --
             , d2.col4               --
        FROM data1 d1
             INNER JOIN
             data2 d2 USING (user_id)
        WHERE d1.user_id = ?
              AND d1.s BETWEEN 1 AND 6
        ORDER BY user_id";

$stmt = $pdo->prepare($sql);
$stmt->execute( [ $info['id'] ] );

foreach ($stmt as $row) {
    // process result rows
}

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.