0

Good day,

I'm a noob at PHP and MySQl. Looking to be pointed in the right direction. I have a MySql table with 5 columns. Each column represents a specific set of data. All numerical.

I want to write PHP code which takes each value in a single column and puts it into an array that I can then modify.

I don't know how to get each column as a separate array. Should I get an array of all the rows and then do something extra to separate each of the 5 numbers in each row into 5 separate arrays? **

require_once 'login.php';

    echo $db;
    $conn = mysqli_connect($hn,$un,$pw,$db);
    if (!$conn) {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
      }

$query = "SELECT xox FROM tablex WHERE id = '1'";
$result = $conn->query($query);
if(!$result) die ("Database access failed: " . $conn->error);

$rows = mysqli_fetch_row($result);



while($rows){
    echo $rows['index'];

}

echo $rows[0];

?>

David
  • 9
  • 1
  • 3
  • Can you post the `CREATE` statement of your table or maybe a sample of the data in it? Also, what exactly do you want to do with the data? – Vilx- Jan 22 '18 at 18:45
  • Hey David, if you can show us some of the code you are trying with that would be helpful. Your question as it stands now is pretty general. If you need a starting point I'd recommend googling some resources to get you going that cover pulling data from a database, and doing things with it in PHP. A good starting point might be: https://www.w3schools.com/php/php_mysql_intro.asp –  Jan 22 '18 at 18:48
  • @Jared Please, **DO NOT** link to the w3schools site, especially not their astonishingly bad MySQL material. – tadman Jan 22 '18 at 19:37
  • Note: Try and get out of the habit of declaring SQL statements in throw-away variables that are used only once. It's a lot easier to follow code where the query is supplied directly to the function, and there's no longer a chance of messing up and sending in `$sql3` instead of the visually similar `$sql8`. – tadman Jan 22 '18 at 19:37
  • A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so mistakes aren't easily ignored. – tadman Jan 22 '18 at 19:38
  • @tadman I would have agreed with you three or four years ago, but at this point there is no denying it's validity as a generic, entry level programming resource. –  Jan 22 '18 at 19:50
  • @Jared The link you've provided is just as bad as it's ever been. There are significantly better sources of material to learn from. It's like the "WikiHow" of learning, written by people with no clue for those that don't know better. We've spent enormous amounts of time repairing the damage that site has done and continues to do when it keeps getting linked. – tadman Jan 22 '18 at 19:55
  • @tadman This isn't the place for it, but I'm really curious what specifically you think is so bad about that link in particular. It's providing generic examples not too dissimilar to what you would find in other less established guides. It's emphasis is on using PDO, which is a known safe way to manage database connections with PHP... It's not at all like `WikiHow` which is a verifiable mess, you're just making baseless comparisons now. –  Jan 22 '18 at 19:58
  • @Jared It barely scratches the surface of what's actually an unavoidably complicated thing to get into. It also wastes a lot of time trying to teach three different approaches at once, `mysqli` in both object-oriented and procedural style, and PDO, when it should be PDO and just PDO. Their first example table has a whole mess of problems, like `INT(6)` declared for no reason, an email address with an arbitrary limit of 50 characters, a `TIMESTAMP` field, plus it's all wrapped up inside a mess of a PHP script with zero organizational strategy in place. – tadman Jan 22 '18 at 20:03
  • @Jared It's not okay to just dump all your code into one file, upload it to the server, and call it done. That's unsustainable as a development model. Today if you're not using a framework you're just going to make a mess of things. There's a number of them, all high quality, to pick from. Doing everything with core PHP is how you create an unmaintainable tangle of garbage, and w3schools is the first step on the road to that disastrous outcome. – tadman Jan 22 '18 at 20:06
  • @tadman I guess I can't disagree that they could do a better job, and have more clarity with what and why they make the choices they make. For quick and dirty, let's call it pseudocode, I see it as a good resource still. Best practice for a seasoned engineer? No. –  Jan 22 '18 at 20:06
  • @Jared There's a lot of good material out there that deserves to be linked to first, like [the PDO manual](http://php.net/manual/en/intro.pdo.php), or introductions like [Why you should be using PDO](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and [PDO demystified](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). For database-oriented concerns it's probably better for people to get started with an ORM which gives them some training wheels, some structure, than let them loose on raw MySQL. – tadman Jan 22 '18 at 20:08
  • 1
    @tadman That's fair man. I'll take that in to consideration next time I'm tempted to paste a w3 link. –  Jan 22 '18 at 20:09
  • Your question is too light on specifics; it is incomplete and Unclear what your are asking. You are mixing object-oriented with procedural syntax. Please improve your question. – mickmackusa Jan 22 '18 at 22:00

2 Answers2

0
$statement = **yourDatabaseConnection**->query('SELECT column1 FROM table');
$datas = $statement->fetch();

$datas will be an array of your column1 datas

ScoobyDam
  • 379
  • 3
  • 13
-2

Try reviewing the php documentation, here is a good place to start...php:mysql_fetch_row Take a look at the example below, if is not what you looking for, please edit your question and provide the approach you are taking (your code) so that we have a better understanding where you are having issues.

<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; // 42
echo $row[1]; // the email value
?>
OhDavey
  • 167
  • 1
  • 3
  • 9
  • 1
    This question uses `mysqli` and you go and answer with the deleted `mysql_query` interface? – tadman Jan 22 '18 at 19:37