greens85 Posted April 8, 2009 Share Posted April 8, 2009 Hi All, Im relativley new to both PHP & mySQL, I want to drag out all the data in my database and have it displayed in an array: Lets say for example it the data was jobs, I want to display all the jobs in a list ordered by the job id and where a certain field contains the data 'yes', therefore if a certain row of the data doesnt contain the data yes the job isn't displayed. I currently have the following code: <?php $connection = mysql_connect("localhost", "user", "pass") or die (mysql_error()); $db = mysql_select_db("mydb", $connection) or die (mysql_error()); ?> <?php $query = mysql_query ("SELECT * FROM jobs WHERE CS = 'yes' ORDER BY jobid"); while ($data1 = mysql_fetch_array ($query)) { echo $data1 [country]; } ?> But this just returns the country for every job, where as i want to return the data in a list and it also includes the data for rows that dont have CS set to yes. Hope this makes sense! Can anyone point me in the right direction? ??? Link to comment https://forums.phpfreaks.com/topic/153143-php-array/ Share on other sites More sharing options...
trq Posted April 8, 2009 Share Posted April 8, 2009 The code you have provided should do exactly what you have described. maybe you should better describe what you mean by a 'list'? Link to comment https://forums.phpfreaks.com/topic/153143-php-array/#findComment-804419 Share on other sites More sharing options...
greens85 Posted April 8, 2009 Author Share Posted April 8, 2009 Appologises perhaps i didnt explain myself very well. What i mean is, there may be say 3 jobs in the db with the CS param. set to yes. Each jobs then has different data, i.e. each is a different jobs and needs to be displayed as a seperate entity. At the moment the only output im getting is: Job Title: TEST JOB Where as im trying to achieve something such as: Job Title: Test Job Job Desc: Description Here Comany: Company ABC Address: Address Here --------------------------------- Job Title: Test Job 2 Job Desc: Description 2 Here Comany: Company DEF Address: Address 2 Here And so on for each row that has the CS field set to 'yes'. Hope this clears up what I mean and thanks for your quick reply! Link to comment https://forums.phpfreaks.com/topic/153143-php-array/#findComment-804435 Share on other sites More sharing options...
trq Posted April 8, 2009 Share Posted April 8, 2009 <?php mysql_connect("localhost", "user", "pass") or die (mysql_error()); mysql_select_db("mydb", $connection) or die (mysql_error()); $sql = "SELECT country FROM jobs WHERE CS = 'yes' ORDER BY jobid"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_array ($query)) { echo $row['country']; echo "------------<br />"; } } } ?> Hope that helps as an example. Link to comment https://forums.phpfreaks.com/topic/153143-php-array/#findComment-804442 Share on other sites More sharing options...
greens85 Posted April 8, 2009 Author Share Posted April 8, 2009 Hi Thorpe, Ive now managed to get it to return this: TEST JOB This is a test job 0001 --------------------- Test Job Test Job 1112223 Using this code: <?php //Run Query's To Select The Data $query5 = mysql_query("SELECT * FROM jobs WHERE CS = 'CS' ORDER BY jobid ASC"); while ($data1 = mysql_fetch_array($query5)){ //Show The Data In The Browser echo $data1['position']; echo '<br/>'; echo $data1['description']; echo '<br/>'; echo '<br/>'; echo $data1['jobref']; echo '<br/>'; echo $data['hour']; echo '<br/>'; echo $data['subcounty']; echo '<br/>'; echo $data['contract']; echo '<br/>'; echo $data['salary']; echo '<br/>'; echo $data['deadline']; } ?> But it stops displaying after jobref, when i want it to display all the fields under that! Do you have any idea why this might be occuring? If I could get it to display all the fields i specify in the code, the problem would be solved Link to comment https://forums.phpfreaks.com/topic/153143-php-array/#findComment-804464 Share on other sites More sharing options...
trq Posted April 8, 2009 Share Posted April 8, 2009 After $data1['jobref'] you start using an undefined $data array, you should still be using $data1. You should always have this at the top of your code when developing, it will point out allot of simple errors. <?php error_reporting(E_ALL) ; ini_set('display_errors','1'); ?> Link to comment https://forums.phpfreaks.com/topic/153143-php-array/#findComment-804947 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.