samtwilliams Posted July 17, 2009 Share Posted July 17, 2009 Morning All, I have a settings table in my database which contains various settings for my website which i need to echo on certain pages but i am stuck on how to create the array of results so i can echo certain values. My table looks like this; paramvalue index_titleMy Website. index_introThis is the introduction of my page. And my code like this; <?PHP include('inc/flat_settings.inc.php'); // DB CONNECTIONS HERE mysql_connect('localhost',$dbusername,$dbpassword) or die( "Unable to connect to database server."); @mysql_select_db($database) or die( "Unable to select database."); // DB QUERY HERE $query_globals = "SELECT param, value FROM `globals`"; $result_globals = mysql_query($query_globals); while ($row_globals = mysql_fetch_array($result_globals)) { $arr = array($row_globals['param'] => $row_globals['value']); } ?> I want to be able to echo my value like this; echo $arr['index_title'] which should return "My Website." At the moment it seems nothing is being captured in my array. Hope someone can help Sam Quote Link to comment https://forums.phpfreaks.com/topic/166292-solved-create-an-array-and-echo-certain-rows/ Share on other sites More sharing options...
BillyBoB Posted July 17, 2009 Share Posted July 17, 2009 <?PHP include('inc/flat_settings.inc.php'); // DB CONNECTIONS HERE mysql_connect('localhost',$dbusername,$dbpassword) or die( "Unable to connect to database server."); @mysql_select_db($database) or die( "Unable to select database."); // DB QUERY HERE $query_globals = "SELECT param, value FROM `globals`"; $result_globals = mysql_query($query_globals); $arr = mysql_fetch_array($result_globals); } ?> try that just echo out $arr['index_title']; Quote Link to comment https://forums.phpfreaks.com/topic/166292-solved-create-an-array-and-echo-certain-rows/#findComment-876932 Share on other sites More sharing options...
samtwilliams Posted July 17, 2009 Author Share Posted July 17, 2009 Comes out blank? Quote Link to comment https://forums.phpfreaks.com/topic/166292-solved-create-an-array-and-echo-certain-rows/#findComment-876934 Share on other sites More sharing options...
samtwilliams Posted July 17, 2009 Author Share Posted July 17, 2009 When i run this; <?PHP include('inc/flat_settings.inc.php'); // DB CONNECTIONS HERE mysql_connect('localhost',$dbusername,$dbpassword) or die( "Unable to connect to database server."); @mysql_select_db($database) or die( "Unable to select database."); // DB QUERY HERE $query_globals = "SELECT param, value FROM `globals`"; $result_globals = mysql_query($query_globals); while ($row_globals = mysql_fetch_assoc($result_globals)) { $arr = array($row_globals['param'] => $row_globals['value']); echo '<pre>'; echo htmlspecialchars(print_r($arr, true)); echo '</pre>'; } echo $arr['global_title_index']; echo $arr['global_logo_title']; ?> I get the following results but only echos global_logo_title; Array ( [global_title_index] => Title of my page ) Array ( [global_logo_title] => My logo title ) My logo title Quote Link to comment https://forums.phpfreaks.com/topic/166292-solved-create-an-array-and-echo-certain-rows/#findComment-876941 Share on other sites More sharing options...
BillyBoB Posted July 17, 2009 Share Posted July 17, 2009 What I don't understand is what you are trying to accomplish. You are simply calling for an array and then adding it into another array why is this? Can this not work: <?PHP include('inc/flat_settings.inc.php'); // DB CONNECTIONS HERE mysql_connect('localhost',$dbusername,$dbpassword) or die( "Unable to connect to database server."); @mysql_select_db($database) or die( "Unable to select database."); // DB QUERY HERE $query_globals = "SELECT param, value FROM `globals`"; $result_globals = mysql_query($query_globals); $row_globals = mysql_fetch_array($result_globals); echo $row_globals['global_title_index']; echo $row_globals['global_logo_title']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/166292-solved-create-an-array-and-echo-certain-rows/#findComment-876944 Share on other sites More sharing options...
samtwilliams Posted July 17, 2009 Author Share Posted July 17, 2009 This seems to echo no results though? My database looks exactly like this; id param value help 1 global_title_index Title of my page Title bar of the index page. 2 global_logo_title My logo title Logo title text. Using the code you supplied surley only allows me to echo the first row calling it by the increment number the array sets? I tried this with your code; echo $row_globals['1']; and the result that was displayed was "Title of my page" Surely i need to create the array with a loop? Quote Link to comment https://forums.phpfreaks.com/topic/166292-solved-create-an-array-and-echo-certain-rows/#findComment-876946 Share on other sites More sharing options...
samtwilliams Posted July 17, 2009 Author Share Posted July 17, 2009 FYI <?PHP include('inc/flat_settings.inc.php'); // DB CONNECTIONS HERE mysql_connect('localhost',$dbusername,$dbpassword) or die( "Unable to connect to database server."); @mysql_select_db($database) or die( "Unable to select database."); // DB QUERY HERE $query_globals = "SELECT param, value FROM `globals`"; $result_globals = mysql_query($query_globals); $arr = array(); while ($row_globals = mysql_fetch_array($result_globals)) { $arr[$row_globals['param']] = $row_globals['value']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/166292-solved-create-an-array-and-echo-certain-rows/#findComment-876961 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.