logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 08-19-2012, 09:22 PM   #1
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default basic connection

Hi, I am having an issue with basic connections with mysqli. I have added my code below and below that the output. Why does it show the information twice?
If I remove the foreach loop, I just get ArrayArrayArray. If I remove the while loop I get nothing.

I have searched around on the net but cant find anything that shows how to connect to mysqli, the procedural way. I am trying not to move onto oop yet until I can work with databases correctly.

$connection = mysqli_connect('localhost', 'user', 'password', 'test');
$query = 'SELECT * FROM cars';
$result = mysqli_query($connection, $query, MYSQLI_USE_RESULT);


while($row = mysqli_fetch_array($result)){

foreach($row AS $value){
echo $value . "<br/>";
}

}

//result
1
1
SLK
SLK
MERC
MERC
CAR
CAR
2
2
GTI
GTI
MITSUBISHI
MITSUBISHI
CAR
CAR
tony09uk is offline   Reply With Quote
Old 08-19-2012, 09:53 PM   #2
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default

So this works

$connection = mysqli_connect('localhost', 'root', 'penbird098', 'test');
$query = 'SELECT * FROM cameras';
$result = mysqli_query($connection, $query, MYSQLI_USE_RESULT);


while($row = mysqli_fetch_array($result)){


echo $row[0] . $row[1] . $row[2] . "<br/>";


}

but i am aiming to achieve this result dynamically.
tony09uk is offline   Reply With Quote
Old 08-21-2012, 01:47 AM   #3
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,164
Default

here is a PDO connection to mysqli give this a try

PDO connection
Code:
<?php
/***  hostname ***/
$hostname = 'localhost';
/***  username ***/
$username = 'vvvvv';

/*** mysql password ***/
$password = 'vvvvv';
function testdb_connect ()
   {
    $dbh = new PDO("mysqli:host=$hostname;dbname=scripts", 'test' , 'edbypass');
     return ($dbh);
   }
?>
Code:
  $sql = "SELECT * FROM table; 
 
     foreach ( $dbh->query($sql) as $row) {
	 echo  $row['category'];
	 }
include file then
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 08-21-2012, 09:00 PM   #4
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default

ive found a number of tutorials that show how connect to mysqli using oo. But i dont understand oo yet so i didnt want to mix procedural and oo as intend on learning oo later once i have a stronger understanding
tony09uk is offline   Reply With Quote
Old 08-22-2012, 01:09 AM   #5
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,164
Default

pdo is not really complete OOP, as a connection it can be used for many type off databases including mysql with added sfety from injection. You can fetch array,associate or object if you want
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 09-01-2012, 07:53 AM   #6
jmichae3
 
Join Date: Dec 2010
Posts: 366
Default

one thing you need to do is use
Code:
mysqli_free_result($result);
at the end of your script. script won't work right according to docs without it.
__________________
------------
Jim Michaels
HTML Code:
improperly<strong>nested<em>elements</strong>cause</em>
browser confusion (I believe the term is 'tag soup')!
jmichae3 is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:57 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com