Hey folks,
I need a little help with how modx works with databases... I have a couple of examples of code that I would like to have converted to work with modx. I’m learning PHP and I would like to understand how I can use my code in modx. I just need some pointers with the examples I have typed as this will help me better with what I need to do as the wiki and docs haven’t really helped.
In this example I would like to know how to connect to an external database, create a table and populate it.
<?php
$connect = mysql_connect("localhost", "myDB", "username", "password")
or die("Connection not established");
//creates database
$create = mysql_query("CREATE DATABASE IF NOT EXISTS storage")
or die(mysql_error());
//recently created DB is active
mysql_select_db("storage");
//create table
$table1 = "CREATE TABLE table1 (
id int(10) NOT NULL auto_increment,
file varchar(250) NOT NULL,
)";
$results = mysql_query($storage)
or die (mysql_error());
echo "Database created";
?>
With this example, how do I connect to the external DB, go to table1 and display the id and file text?
<?php
$connect = mysql_connect("localhost", "myDB", "username", "password")
or die("Connection not established");
mysql_select_db("storage");
$query = "SELECT id, file " .
"FROM table1 ".
"ORDER BY id";
$results = mysql_query($query)
or die(mysql_error());
while ($row = mysql_fetch_array($results)) {
extract($row);
echo $id;
echo "<br />";
echo $file;
}
?>
Thanks for any help.