Jump to content

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
https://forums.phpfreaks.com/topic/307394-php-array/
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

Edited by benanamen
Link to comment
https://forums.phpfreaks.com/topic/307394-php-array/#findComment-1559043
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.

Edited by mac_gyver
more info
Link to comment
https://forums.phpfreaks.com/topic/307394-php-array/#findComment-1559044
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

Edited by Barand
Remove spam link
Link to comment
https://forums.phpfreaks.com/topic/307394-php-array/#findComment-1559358
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
https://forums.phpfreaks.com/topic/307394-php-array/#findComment-1559362
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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