jipacek Posted March 26, 2008 Share Posted March 26, 2008 Hallo there ! Can you please help me with this simple thing I can't make work or understand: mysql_connect('localhost', 'jipak', 'password'); mysql_select_db('contacts'); mysql_query('SELECT name FROM contacts'); $result = mysql_query('SELECT name FROM contacts'); $row = mysql_fetch_array($result); I would like to load data from sql instead of writing it manually like this: $DataArr = array("Some Notes on Jeff.", "Info on Amy", "Facts on Bob.", "Some more info on Jimmy","Some gossip about Steve."); Quote Link to comment https://forums.phpfreaks.com/topic/98031-help-please-very-simple-i-am-sure-problem-with-array/ Share on other sites More sharing options...
soycharliente Posted March 26, 2008 Share Posted March 26, 2008 <?php mysql_connect('localhost', 'jipak', 'password'); mysql_select_db('contacts'); $result = mysql_query("SELECT `name` FROM `contacts`") OR DIE ("I wrote bad SQL code!"); if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { echo "<p>{$row['name']}</p>"; } } else { echo "<p>No contacts!</p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/98031-help-please-very-simple-i-am-sure-problem-with-array/#findComment-501581 Share on other sites More sharing options...
wildteen88 Posted March 26, 2008 Share Posted March 26, 2008 If your query returns multiple rows you'll need to use a while loop to loop through all the results. Just calling mysql_fetch_array on its own will only return the first row. EDITE: Beaten to it mysql_connect('localhost', 'jipak', 'password'); mysql_select_db('contacts'); mysql_query('SELECT name FROM contacts'); $result = mysql_query('SELECT name FROM contacts'); // loop through result while($row = mysql_fetch_assoc($result)) { // add name to $names array $names[] = $row['name']; } // display $names array echo '<pre>' . print_r($names, true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/98031-help-please-very-simple-i-am-sure-problem-with-array/#findComment-501583 Share on other sites More sharing options...
rhodesa Posted March 26, 2008 Share Posted March 26, 2008 <?php mysql_connect('localhost', 'jipak', 'password'); mysql_select_db('contacts'); $result = mysql_query('SELECT name FROM contacts'); $DataArr = array(); while($row = mysql_fetch_array($result)){ $DataArr[] = $row['name']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/98031-help-please-very-simple-i-am-sure-problem-with-array/#findComment-501603 Share on other sites More sharing options...
jipacek Posted March 27, 2008 Author Share Posted March 27, 2008 God bless you this was it: <?php mysql_connect('localhost', 'jipak', 'password'); mysql_select_db('contacts'); $result = mysql_query('SELECT name FROM contacts'); $DataArr = array(); while($row = mysql_fetch_array($result)){ $DataArr[] = $row['name']; } ?> Thank you very much. kisses !!! George. Quote Link to comment https://forums.phpfreaks.com/topic/98031-help-please-very-simple-i-am-sure-problem-with-array/#findComment-502224 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.