please convert my php old fashion to mysqli or PDO

hello i try to convert my old fashion warning php to smtg proper



connect.php

<?php // Hote

$dbhost		="localhost";
// Nom d'utilisateur
$dblogin	="root"; 
// Password utilisateur
$dbpasspord	="";
// Nom de votre base de données
$database	="portfolio"; 
// Password administrateur pour la zone d'administration
$passadmin	="admin"; 

//if I put the following line without comment the aff_image.php script doesnt work anymore
// $link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link)); 



mysql_connect($dbhost,$dblogin,$dbpasspord) or die("Impossible de se connecter à la base de données !"); 
mysql_select_db("$database") or die("Impossible de selectionner la base !"); 
mysql_query("SET NAMES 'utf8'"); 

Open in new window

<?php include('connec.php');





function affiche($src,$lang,$appel_fnc,$type,$limite){



$nombre = 5;  /* on on va afficher 5 résultats par page*/

if ($lang == "fr") {$infos="plus d'infos";}

else $infos="more informations";

              



$requete = mysql_query('SELECT *  FROM image WHERE s_cat= '.$type.' ') OR die('Erreur de la requête images');

$ntotal = mysql_num_rows($requete);







$requete2 = mysql_query('SELECT *  FROM image WHERE s_cat= '.$type.'  limit '.$limite.','.$nombre.' ')OR die('Erreur de la requête images');





$iCpt = 1;



while($resultat = mysql_fetch_assoc($requete2))

     { 

    $sClassName = 'imagette';

    if($iCpt === 1) {$sClassName = 'imagette2'; }

    ++$iCpt;

    echo '<a href="#" title="'.$infos.'"><img class="'.$sClassName.'" src='.$resultat['chemin'].' onmouseover="javascript:enlarge(this)" border="0" alt="'.$resultat['chemin'].'"  /></a>';

     }



$limitesuivante = $limite + $nombre;

$limiteprecedente = $limite - $nombre;



   echo '</div> <div id="barre_defil">';

switch ($appel_fnc) {



 /* on on va afficher 5 résultats par page*/

    case 1:

    

        if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limiteprecedente.'/0/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}



if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limitesuivante.'/0/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 2:

          if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limiteprecedente.'/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limitesuivante.'/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 3:

   if($limite != 0) {

    echo '<a href="/'.$src.'/'.$limiteprecedente.'"><img src="/fnc/prev.gif" border="0" alt="precedent" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="appel8.php?limite='.$limitesuivante.'"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}   

   break;     

}   





   echo ' </div> ';

}







?>

Open in new window

jerrrrryAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Daniel WilsonConnect With a Mentor Commented:
OK, I'm working from this documentation.

$link = new mysqli("myhost","myuser","mypassw","mybd");
if ($link->connect_errno)  die("Error " . $link->connect_errno );

$link->query("SET NAMES 'utf8'"); 

Open in new window

0
 
Marco GasiFreelancerCommented:
I can suggest you to read this: http://www.sitepoint.com/migrate-from-the-mysql-extension-to-pdo/ It can help you to migrate your code to PDO, the new de facto standard layer for myql.
0
 
Daniel WilsonConnect With a Mentor Commented:
For your 2nd code block, I assume you want a parameterized query to help you avoid SQL injection.  That's one of the major draws of mysqli.

So ... lines 21-23 translate like this:

$stmt = $link->prepare('SELECT *  FROM image WHERE s_cat=  ?');
$stmt->bind_param("s", $type);
$stmt->execute();

$stmt->store_results();
$ntotal = $stmt->$stmt->num_rows;

Open in new window


http://php.net/manual/en/mysqli.prepare.php
http://php.net/manual/en/mysqli-result.num-rows.php
http://php.net/manual/en/mysqli-stmt.execute.php
http://foundationphp.com/blog/2011/09/24/using-num_rows-with-a-mysqli-prepared-statement/
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
jerrrrryAuthor Commented:
how can i translate this?
$requete2 = mysql_query('SELECT *  FROM image WHERE s_cat= '.$type.'  limit '.$limite.','.$nombre.' ')OR die('Erreur de la requête images');





$iCpt = 1;



while($resultat = mysql_fetch_assoc($requete2))

Open in new window



tks
$requete2 = mysql_query('SELECT *  FROM image WHERE s_cat= '.$type.'  limit '.$limite.','.$nombre.' ')OR die('Erreur de la requête images');





$iCpt = 1;



while($resultat = mysql_fetch_assoc($requete2))

Open in new window

$requete2 = mysql_query('SELECT *  FROM image WHERE s_cat= '.$type.'  limit '.$limite.','.$nombre.' ')OR die('Erreur de la requête images');





$iCpt = 1;



while($resultat = mysql_fetch_assoc($requete2))

Open in new window

0
 
Daniel WilsonConnect With a Mentor Commented:
http://php.net/manual/en/mysqli-stmt.bind-param.php
http://php.net/manual/en/mysqli-result.fetch-assoc.php

$requete2 = $link->prepare('SELECT *  FROM image WHERE s_cat= ? limit ?, ?');
$requete2->bind_param("sdd", $type, $limite, $nombre);
$requete2->execute();
$requete2->store_results();

$iCpt = 1;

while ($resultat = $requete2->fetch_assoc()){

}

Open in new window

0
 
Ray PaseurConnect With a Mentor Commented:
There is an article here at E-E that addresses exactly this issue.  It maps the familiar but obsolete MySQL extension to the currently supported versions.  Please read it over and post back here if you still have any questions.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
jerrrrryAuthor Commented:
hello i have little problem...

) Notice: Undefined variable: link in C:\wamp\www\aff_image.php on line 18
Call Stack
#      Time      Memory      Function      Location
1      0.0000      145552      {main}( )      ..\web.php:0
2      0.0100      166384      affiche( )      ..\web.php:229

( ! ) Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\aff_image.php on line 18
0
 
Daniel WilsonCommented:
That is a GREAT article, Ray!
0
 
Daniel WilsonCommented:
Jerrrrry, please post the code at least through line 18 that breaks.
0
 
jerrrrryAuthor Commented:
<?php include('connec.php');





function affiche($src,$lang,$appel_fnc,$type,$limite){



$nombre = 5;  /* on on va afficher 5 résultats par page*/

if ($lang == "fr") {$infos="plus d'infos";}

else $infos="more informations";

              
$stmt = $link->prepare('SELECT *  FROM image WHERE s_cat=  ?');
$stmt->bind_param("s", $type);
$stmt->execute();

$stmt->store_results();
$ntotal = $stmt->$stmt->num_rows;


 





$requete2 = $link->prepare('SELECT *  FROM image WHERE s_cat= ? limit ?, ?');
$requete2->bind_param("sdd", $type, $limite, $nombre);
$requete2->execute();
$requete2->store_results();

$iCpt = 1;

while ($resultat = $requete2->fetch_assoc()){




     

    $sClassName = 'imagette';

    if($iCpt === 1) {$sClassName = 'imagette2'; }

    ++$iCpt;

    echo '<a href="#" title="'.$infos.'"><img class="'.$sClassName.'" src='.$resultat['chemin'].' onmouseover="javascript:enlarge(this)" border="0" alt="'.$resultat['chemin'].'"  /></a>';

     }



$limitesuivante = $limite + $nombre;

$limiteprecedente = $limite - $nombre;



   echo '</div> <div id="barre_defil">';

switch ($appel_fnc) {



 /* on on va afficher 5 résultats par page*/

    case 1:

    

        if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limiteprecedente.'/0/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}



if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limitesuivante.'/0/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 2:

          if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limiteprecedente.'/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limitesuivante.'/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 3:

   if($limite != 0) {

    echo '<a href="/'.$src.'/'.$limiteprecedente.'"><img src="/fnc/prev.gif" border="0" alt="precedent" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="appel8.php?limite='.$limitesuivante.'"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}   

   break;     

}   





   echo ' </div> ';

}







?>

Open in new window

connec.php
//$dbhost		="localhost";
// Nom d'utilisateur
//$dblogin	="root"; 
// Password utilisateur
//$dbpasspord	="";
// Nom de votre base de données
//$database	="portfolio"; 
// Password administrateur pour la zone d'administration
//$passadmin	="admin"; 

//if I put the following line without comment the aff_image.php script doesnt work anymore
 $link = mysqli_connect("localhost","root","","portfolio") or die("Error " . mysqli_error($link)); 



//mysql_connect($dbhost,$dblogin,$dbpasspord) or die("Impossible de se connecter à la base de données !"); 
//mysql_select_db("$database") or die("Impossible de selectionner la base !"); 

//mysql_query("SET NAMES 'utf8'"); 

    

?>

Open in new window

thanks
0
 
Daniel WilsonCommented:
Incorporate the first block of code I gave you to get your connection.

Have you read the article Ray posted?  It really helps!
0
 
jerrrrryAuthor Commented:
hello i try with what u gave me but it doesn't work....
there is a lot to read i haven't finished
thanks
0
 
Daniel WilsonConnect With a Mentor Commented:
OK, you're trying to use $link as a global connection, right?  But in your function, $link isn't defined.  

Somewhere between lines 7 and 18 put:


global $link;

Open in new window

0
 
Ray PaseurCommented:
@Daniel, thanks!
0
 
jerrrrryAuthor Commented:
ok now i have  Fatal error: Call to undefined method mysqli_stmt::store_results() in C:\wamp\www\aff_image.php on line 22

<?php include('connec.php');





function affiche($src,$lang,$appel_fnc,$type,$limite){

global $link;

$nombre = 5;  /* on on va afficher 5 résultats par page*/

if ($lang == "fr") {$infos="plus d'infos";}

else $infos="more informations";

              
$stmt = $link->prepare('SELECT *  FROM image WHERE s_cat=  ?');
$stmt->bind_param("s", $type);
$stmt->execute();

$stmt->store_results();
$ntotal = $stmt->$stmt->num_rows;


 





$requete2 = $link->prepare('SELECT *  FROM image WHERE s_cat= ? limit ?, ?');
$requete2->bind_param("sdd", $type, $limite, $nombre);
$requete2->execute();
$requete2->store_results();

$iCpt = 1;

while ($resultat = $requete2->fetch_assoc()){




     

    $sClassName = 'imagette';

    if($iCpt === 1) {$sClassName = 'imagette2'; }

    ++$iCpt;

    echo '<a href="#" title="'.$infos.'"><img class="'.$sClassName.'" src='.$resultat['chemin'].' onmouseover="javascript:enlarge(this)" border="0" alt="'.$resultat['chemin'].'"  /></a>';

     }



$limitesuivante = $limite + $nombre;

$limiteprecedente = $limite - $nombre;



   echo '</div> <div id="barre_defil">';

switch ($appel_fnc) {



 /* on on va afficher 5 résultats par page*/

    case 1:

    

        if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limiteprecedente.'/0/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}



if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/'.$limitesuivante.'/0/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 2:

          if($limite != 0) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limiteprecedente.'/0/0"><img src="/fnc/prev.gif" border="0" alt="" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="/'.$src.'/'.$lang.'/0/'.$limitesuivante.'/0/0"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}

        break;

    case 3:

   if($limite != 0) {

    echo '<a href="/'.$src.'/'.$limiteprecedente.'"><img src="/fnc/prev.gif" border="0" alt="precedent" class="fleche"></a>';

}





if($limitesuivante < $ntotal) {

    echo '<a href="appel8.php?limite='.$limitesuivante.'"><img src="/fnc/next.gif" border="0" alt="suivant" class="fleche"></a>';

}   

   break;     

}   





   echo ' </div> ';

}







?>

Open in new window

thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.