+ 2
How to Retrieve Some Field Data from Two Different Tables and then Store it in Array?
Please help me.. give me example of code. I am using PHP and mysql database.
4 Respostas
+ 2
Well, you can use mysqli, it supports procedural style. But object oriented is more convenient. Also, i did mistake in previous example, its should be $DB->query($query), not $DB->query($q), sorry, it's my habit of coding. :)
//mysqli example:
//set up connection to database
$conn = mysqli_connect("your_host", "your_db_username", "your_db_password", "your_db_name");
mysqli_set_charset($conn, 'utf8');
mysqli_query($conn, "SET NAMES utf8");
//your query string
$query = "SELECT * FROM table1, table2";
//execute query
$qResult = mysqli_query($conn, $query);
//fetch data to array
while($row = mysqli_fetch_assoc($qResult)) {
$resultArray[] = $row;
};
//print data dump on screen
var_dump($resultArray);
+ 3
//Example using PDO connection:
$DBhost = "your_host";
$DBname = "your_db_name";
$DBusername = "your_db_username";
$DBpassword = "your_db_password";
//set up connection to database
$DB = new PDO("mysql:host=$DBhost;dbname=$DBname;charset=utf8", $DBusername, $DBpassword, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
//your query string
$query = "SELECT * FROM table1, table2";
//execute query
$qResult = $DB->query($q);
//fetch data to array
$resultArray = $qResult->fetchAll();
//print data dump on screen
var_dump($resultArray);
+ 2
Thank you Jeth for your answer. Umm.. I am using procedural code. But thank you.. I'll try it!
+ 1
thats GREAT!! Thank you very much, Jeth! :)