Hi guys, I have stored data in a MySQL database and I've output it in PHP. I want to know how I can output the data in different <span id="text1">` tags from `<tr><td>` tags. <?php define('DB_HOST', 'localhost'); define('DB_USER', 'myusername'); define('DB_PASSWORD', 'mypassword'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $qrytable1="SELECT id, mydata FROM mydb "; $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error()); while ($row = mysql_fetch_array($result1)) { echo "<tr><td>".$row['mydata']."</td></tr>"; } } ?> PHP: On my PHP page it show something like this: <tr><td>my data 1</td></tr><tr><td>my data 2</td></tr><tr><td>my data 3</td></tr><tr><td>my data 4</td></tr> PHP: How can I split the output the data into an array before I could output them in different <span id="text1">` tags from `<tr><td>` tags? any advice would be much appreicated. thanks in advance
I think this is what you are looking for: <?php define('DB_HOST', 'localhost'); define('DB_USER', 'myusername'); define('DB_PASSWORD', 'mypassword'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $qrytable1="SELECT id, mydata FROM mydb "; $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error()); $i =0; while ($row = mysql_fetch_array($result1)) { echo '<span id="text'.$i.'">'.$row['mydata'].'</span>'; $i++; //echo "<tr><td>".$row['mydata']."</td></tr>"; } } ?> PHP: On a side not you shouldn't really be using MYSQL functions it's outdated and insecure. You need to use MySQLi: http://php.net/manual/en/book.mysqli.php
@HuggyStudios: Thanks for your post. I have copied and pasted the code in my php, but there is something not right there. It have output the data altogether without put into array. The data is displaying on my php: mydata1mydata2mydata3mydata4 PHP: I want to split them up by turn into array and output them with each different array to something like this: mydata1 PHP: mydata2 PHP: mydata3 PHP: mydata4 PHP: Please post the fixed code instead of wasting my own time.
There's no need to ask me to stop wasting your time, the best thing you could of done is used the php.net website to lookup the functions you need. http://php.net/manual/en/function.array-push.php I have completed the code for you, please read the comments within the code. <?php define('DB_HOST', 'localhost'); define('DB_USER', 'root'); define('DB_PASSWORD', ''); define('DB_DATABASE', 'testing'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $qrytable1="SELECT id, mydata FROM mydb "; $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error()); $i =0; // create array $stack = array(); while ($row = mysql_fetch_array($result1)) { // add to stack array array_push($stack, '<span id="text'.$i.'">'.$row['mydata'].'</span>'); $i++; //echo "<tr><td>".$row['mydata']."</td></tr>"; } var_dump($stack); // output the array foreach($stack as $value) { echo $value.'<br />'; } } ?> PHP: I have tested this code and this is the output: