Quote:
Originally posted by Ghost:
Speaking of MySQL, here is a segment of code that I find extremely useful and efficient for what it does (forgive the PHP 4)

Php Code:

$Query = 'SELECT * FROM table WHERE 1=1';
$mysql_Query = mysql_query($Query);

$i = 0;
while($Query_data = mysql_fetch_assoc($mysql_Query)) {
$mysql_array[$i] = $Query_data;
$i++;
}
 


Gets all the rows for a query as opposed to just one, as is done with mysql_fetch_assoc. I find it extremely awesome.
There isn't major differences... Here, here is an example of 5 and 4 to see some differences. Basicaly you can save a few lines of code in 5. It is a bit more secure in 5.


Lets say you are processing a login from a web form.

Php Code:
 
//////////////////////////////
//		PHP 5 OOP way
//////////////////////////////

$mysqli = new mysqli("localhost", "username", "password", "database"); // php 5 connect makes you specify the db in the connect statement
//this makes for better security

$dg = "SELECT * FROM members WHERE"
	   ."member = "$login'"
	   ."and psswd = '$cpass'";// the ."and can go on and on and on
if($result = $mysqli->query($dg)){ // only do the following if the query worked

	 WHILE($result2 = $result->fetch_array(MYSQLI_ASSOC)){ //OOP way of mysql_fetch_array
		  $my_array[] = $result2; //[] will fill with the num values

	 }$result->close();//release memory used in query and while loop
$mysqli->close();//close db connection
	$my_array_count = count($my_array); // get a count of all in the array

	 // count($value, COUNT_RECURSIVE); counts the values in a multi demensional array

}else{
	 echo "I am sorry we can not process your request at this time"; //graceful failure
	  // set mail(); function here to notify admin of errors
}
for($i = 0; $i <= $my_array_count; $i++){ // why we counted the array

//do stuff with data

}
  
Now we look at php 4

Php Code:
 
//////////////////////////////
// PHP 4 Procedural style
//////////////////////////////

$dbc = mysql_connect("localhost", "username", "password"); // php 4 connect can open a connection to a default db, this is bad
$dbs = mysql_select_db('mt_database', $dbc);// use the mysql_connect values and a database name to auth a database
//this makes for better security

$dg = "SELECT * FROM members WHERE"
	   ."member = "$login'"
	   ."and psswd = '$cpass'";// nothing changes here
$result = $mysql_query($dg); // Now we have to do a second function to check
if($result){
	 WHILE($result2 = mysql_fetch_array($result)){ //Procedural style of mysql_fetch_array
		  $my_array[] = $result2; //[] will fill with the num values
	 }
	$my_array_count = count($my_array); // get a count of all in the array
	 // count($value, COUNT_RECURSIVE); counts the values in a multi demensional array
}else{
	 echo "I am sorry we can not process your request at this time";//graceful failure
	 // set mail(); function here to notify admin of errors
}
for($i = 0; $i <= $my_array_count; $i++){ // why we counted the array
//do stuff with data
}
  


Edited by §intå× (06/02/08 06:03 AM)
_________________________
My New site OpenEyes