KingTut Posted June 23, 2018 Share Posted June 23, 2018 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()); Quote Link to comment https://forums.phpfreaks.com/topic/307394-php-array/ Share on other sites More sharing options...
benanamen Posted June 23, 2018 Share Posted June 23, 2018 (edited) 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 June 23, 2018 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/307394-php-array/#findComment-1559043 Share on other sites More sharing options...
mac_gyver Posted June 23, 2018 Share Posted June 23, 2018 (edited) 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 June 23, 2018 by mac_gyver more info Quote Link to comment https://forums.phpfreaks.com/topic/307394-php-array/#findComment-1559044 Share on other sites More sharing options...
rajsharma Posted July 7, 2018 Share Posted July 7, 2018 (edited) 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 July 7, 2018 by Barand Remove spam link Quote Link to comment https://forums.phpfreaks.com/topic/307394-php-array/#findComment-1559358 Share on other sites More sharing options...
Barand Posted July 7, 2018 Share Posted July 7, 2018 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 } Quote Link to comment https://forums.phpfreaks.com/topic/307394-php-array/#findComment-1559362 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.